(Qt)windows下鼠标键盘热插拔监测 原创

windows下鼠标键盘热插拔监测(Qt)

更多精彩内容
👉个人内容分类汇总 👈

1、实现方式

  • 在QWidget类中重写nativeEvent

2、源码地址

3、适用系统

4、实现效果

在这里插入图片描述

5、实现代码

  • mousekeytest.h
/******************************************************************************
 * @文件名       mousekeytest.h
 * @功能        鼠标、键盘热插拔监测
 *
 * @开发者     (作者)
 * @时间       2021/12/04
 * @备注
 *****************************************************************************/
#ifndef MOUSEKEYTEST_H
#define MOUSEKEYTEST_H

#include <QWidget>

namespace Ui {
class MouseKeyTest;
}

class MouseKeyTest : public QWidget
{
    Q_OBJECT

public:
    explicit MouseKeyTest(QWidget *parent = nullptr);
    ~MouseKeyTest();

private:
    bool nativeEvent(const QByteArray &eventType, void *message, long *result);
    void registerGUID();
private:
    Ui::MouseKeyTest *ui;
};

#endif // MOUSEKEYTEST_H

  • mousekeytest.cpp
#include "mousekeytest.h"
#include "ui_mousekeytest.h"

#include <qdebug.h>
#include <windows.h>
#include "dbt.h"
#include "initguid.h"
#include "usbiodef.h"
#include "hidclass.h"
#include "ntddkbd.h"
#include "ntddmou.h"

MouseKeyTest::MouseKeyTest(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MouseKeyTest)
{
    ui->setupUi(this);

    this->setWindowTitle("鼠标、键盘热插拔监测。");
    registerGUID();
}

MouseKeyTest::~MouseKeyTest()
{
    delete ui;
}

/**
 * @brief  给设备注册通知
 */
void MouseKeyTest::registerGUID()
{
    DEV_BROADCAST_DEVICEINTERFACE mouseInterface;
    ZeroMemory(&mouseInterface, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
    mouseInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    mouseInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    mouseInterface.dbcc_classguid = GUID_DEVINTERFACE_MOUSE ;
    if(!RegisterDeviceNotificationW((HANDLE)this->winId(), &mouseInterface, DEVICE_NOTIFY_WINDOW_HANDLE))
    {
        qDebug() << "鼠标GUID注册失败";
    }

    DEV_BROADCAST_DEVICEINTERFACE keyboardInterface;
    ZeroMemory(&keyboardInterface, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
    keyboardInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    keyboardInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    keyboardInterface.dbcc_classguid = GUID_DEVINTERFACE_KEYBOARD;
    if(!RegisterDeviceNotificationW((HANDLE)this->winId(), &keyboardInterface, DEVICE_NOTIFY_WINDOW_HANDLE))
    {
        qDebug() << "键盘GUID注册失败";
    }
}

/**
 * @brief
 * @param eventType
 * @param message
 * @param result
 * @return
 */
bool MouseKeyTest::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    MSG* msg = reinterpret_cast<MSG*>(message);
    if(msg->message == WM_DEVICECHANGE)                // 通知应用程序设备或计算机的硬件配置发生更改。
    {
        PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
        PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
        switch (msg->wParam)
        {
        case DBT_DEVICEARRIVAL:             // 插入
        {
            if(lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
            {
                if(lpdbv->dbcc_classguid == GUID_DEVINTERFACE_KEYBOARD)
                {
                    ui->textEdit->append("键盘插入:" + QString::fromWCharArray(lpdbv->dbcc_name));
                }
                else if(lpdbv->dbcc_classguid == GUID_DEVINTERFACE_MOUSE)
                {
                    ui->textEdit->append("鼠标插入:" + QString::fromWCharArray(lpdbv->dbcc_name));
                }
            }
            break;
        }
        case DBT_DEVICEREMOVECOMPLETE:      // 拔出
        {
            if(lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
            {
                if(lpdbv->dbcc_classguid == GUID_DEVINTERFACE_KEYBOARD)
                {
                    ui->textEdit->append("键盘移除:" + QString::fromWCharArray(lpdbv->dbcc_name));
                }
                else if(lpdbv->dbcc_classguid == GUID_DEVINTERFACE_MOUSE)
                {
                    ui->textEdit->append("鼠标移除:" + QString::fromWCharArray(lpdbv->dbcc_name));
                }
            }
            break;
        }
        default:
            break;
        }
    }

    return false;
}

posted @ 2021-12-05 00:09  mahuifa  阅读(0)  评论(0编辑  收藏  举报  来源