设备变化通知函数
1、为监听设备事件创建一个DeviceNotifier实例
2、当有键值按下后显示设备
using System;
using LibUsbDotNet.DeviceNotify;
namespace LibUsbDotNet_Learn
{
class Program
{
public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();
static void Main(string[] args)
{
//设备的监听事件
UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;
//键盘有任意键按下将结束
Console.Clear();
Console.WriteLine();
Console.WriteLine("Waiting for system level device events..");
Console.Write("[Press any key to exit]");
while(!Console.KeyAvailable)
{
// Application.DoEvents();
}
UsbDeviceNotifier.Enabled = false;
UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent;
}
private static void OnDeviceNotifyEvent(object sender,DeviceNotifyEventArgs e)
{
//事件发生
//设置光标的位置
Console.SetCursorPosition(0, Console.CursorTop);
Console.WriteLine(e.ToString());//将事件输出
Console.WriteLine();
Console.Write("[Press any key to exit]");
}
}
}