1.需要包含的头文件#include #include #include #include #include #include
2.注册HID消息void MyClass::RegisterHid(){//给设备注册通知DEV_BROADCAST_DEVICEINTERFACE Notificationfilter;ZeroMemory(&Notificationfilter,sizeof(DEV_BROADCAST_DEVICEINTERFACE));Notificationfilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);Notificationfilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;Notificationfilter.dbcc_classguid = {0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 }};hDevNotify = RegisterDeviceNotificationW((HANDLE)this->winId(),&Notificationfilter,DEVICE_NOTIFY_WINDOW_HANDLE);if(!hDevNotify){}}
3.重载naviteEvent,在里面接收WM_DEVICECHANGE消息bool MyClass::nativeEvent(const QByteArray &eventType, void *message, long *result){Q_UNUSED(eventType);Q_UNUSED(result);MSG* msg = reinterpret_cast(message);int msgType = msg->message;if(msgType == WM_DEVICECHANGE){//qDebug("Recv Event