Qt-Qt使用鼠标钩子Hook(支持判断按下、弹起、滚轮方向)
相关资源:
https://download.csdn.net/download/zhujianqiangqq/85398152 csdn代码包下载
.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ Hook.cpp \ main.cpp \ mainwindow.cpp HEADERS += \ Hook.h \ mainwindow.h FORMS += \ mainwindow.ui LIBS += -luser32 # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
main.cpp
#include "mainwindow.h" #includeint main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include#include #include #include #include #include #include "Hook.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); void UpdateCapslockTip(int keyCode, int x, int y, bool downOrUp); private: Ui::MainWindow *ui; Hook *m_pHook; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle(QStringLiteral("Qt使用鼠标钩子Hook(支持判断按下、弹起、滚轮方向)")); #ifdef Q_OS_WIN m_pHook = new Hook(); m_pHook->installHook(); m_pHook->SetKeyboardCall( std::bind(&MainWindow::UpdateCapslockTip, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); #endif } MainWindow::~MainWindow() { m_pHook->unInstallHook(); delete m_pHook; delete ui; } void MainWindow::on_pushButton_clicked() { ui->textEdit->clear(); } void MainWindow::UpdateCapslockTip(int keyCode, int x, int y, bool downOrUp) { QString s = "key pressed:--------:%1 x:%2 y:%3 downOrUp:%4"; s = s.arg(keyCode).arg(x).arg(y).arg(downOrUp); ui->textEdit->append(s); }
hook.h
#ifndef HOOK_H #define HOOK_H #include#include #ifdef Q_OS_WIN // 代表只有WIN系统才会启用 #include"Windows.h" #include // 回调函数引入 #include // 时间函数引入 class Hook { public: Hook(); void installHook(); void unInstallHook(); static LRESULT CALLBACK mouseProc(int nCode,WPARAM wParam,LPARAM lParam); void SetKeyboardCall(const std::function<void (int, int, int, bool)> & func){ m_func = func; } private: static std::function<void(int, int, int, bool)> m_func; }; #endif #endif // HOOK_H
hook.cpp
#include "Hook.h" #ifdef Q_OS_WIN Hook::Hook() { } static HHOOK mouseHook = nullptr; std::function<void(int, int, int, bool)> Hook::m_func = nullptr; //钩子处理函数, LRESULT CALLBACK Hook::mouseProc(int nCode,WPARAM wParam,LPARAM lParam) //钩子消息函数,系统消息队列信息会返回到该函数中 { // MOUSEHOOKSTRUCT *mhookstruct = (MOUSEHOOKSTRUCT*)lParam; //鼠标HOOK结构体 MSLLHOOKSTRUCT *mhookstruct = (MSLLHOOKSTRUCT*)lParam; //鼠标HOOK结构体 POINT pt = mhookstruct->pt; //将当前鼠标坐标点的x,y坐标作为参数向主程序窗口发送消息 // 中间滚轮上下滚动 if (WM_MOUSEWHEEL == wParam) { m_func(4, mhookstruct->mouseData, 0, false); } // 中间滚轮按下 if (WM_MBUTTONDOWN == wParam) { m_func(3, pt.x, pt.y, true); } if (WM_MBUTTONUP == wParam) { m_func(3, pt.x, pt.y, false); } // 鼠标移动 if (WM_MOUSEMOVE == wParam) { // m_func(0, pt.x, pt.y, false); } // 鼠标按下左键 if (WM_LBUTTONDOWN == wParam) { m_func(1, pt.x, pt.y, true); } if (WM_LBUTTONUP == wParam) { m_func(1, pt.x, pt.y, false); } // 鼠标按下右键 if (WM_RBUTTONDOWN == wParam) { m_func(2, pt.x, pt.y, true); } if (WM_RBUTTONUP == wParam) { m_func(2, pt.x, pt.y, false); } return CallNextHookEx(mouseHook, nCode, wParam, lParam); //继续原有的事件队列 } void Hook::unInstallHook() { if(mouseHook != nullptr)// 原作者说是担心一次释放不稳定,才释放二次的。 { UnhookWindowsHookEx(mouseHook);//键盘钩子句不为空时销毁掉 mouseHook = nullptr; } if(mouseHook != nullptr) { UnhookWindowsHookEx(mouseHook);//键盘钩子句不为空时销毁掉 mouseHook = nullptr; } } void Hook::installHook() { mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, nullptr, 0); } #endif
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>"4.0"> <class>MainWindowclass> class="QMainWindow" name="MainWindow"> "geometry"> 0 0 462 407 "windowTitle"> <string>MainWindowstring> class="QWidget" name="centralwidget"> class="QWidget" name="widget" native="true"> "geometry"> 0 0 471 401 class="QLabel" name="label"> "geometry"> 10 20 54 12 "text"> <string>按键:string> class="QLabel" name="label_2"> "geometry"> 10 40 71 16 "text"> <string>输入Key值:string> class="QTextEdit" name="textEdit"> "geometry"> 10 60 441 271 "focusPolicy"> <enum>Qt::ClickFocusenum> class="QPushButton" name="pushButton"> "geometry"> 20 340 80 20 "focusPolicy"> <enum>Qt::ClickFocusenum> "text"> <string>清除string> class="QPushButton" name="pushButton_2"> "geometry"> 110 340 80 20 "focusPolicy"> <enum>Qt::ClickFocusenum> "text"> <string>PushButtonstring> class="QPushButton" name="pushButton_3"> "geometry"> 200 340 80 20 "focusPolicy"> <enum>Qt::ClickFocusenum> "text"> <string>PushButtonstring> class="QPushButton" name="pushButton_4"> "geometry"> 300 340 80 20 "focusPolicy"> <enum>Qt::ClickFocusenum> "text"> <string>PushButtonstring> class="QLineEdit" name="lineEdit"> "geometry"> 60 10 113 20 class="QLineEdit" name="lineEdit_2"> "geometry"> 190 10 113 20 class="QLineEdit" name="lineEdit_3"> "geometry"> 320 10 113 20 class="QSlider" name="horizontalSlider"> "geometry"> 20 380 160 16 "orientation"> <enum>Qt::Horizontalenum> class="QRadioButton" name="radioButton"> "geometry"> 210 370 91 18 "text"> <string>RadioButtonstring> class="QCheckBox" name="checkBox"> "geometry"> 320 370 73 18 "text"> <string>CheckBoxstring>
搜索
复制