前言
底层hook(钩子)相关逻辑;包含键位和各种组合键的捕获方法,及与Qt类进行交互的逻辑,特此记录
代码
源文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| #include <windows.h> #include <stdint.h>
HHOOK keyHook= nullptr;
LRESULT CALLBACK keyProc(int nCode, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
if(nCode == HC_ACTION) { if(pkbhs->vkCode == VK_ESCAPE && GetAsyncKeyState(VK_CONTROL)& 0x8000 && GetAsyncKeyState(VK_SHIFT)&0x8000) { return 1; } else if(pkbhs->vkCode == VK_ESCAPE && GetAsyncKeyState(VK_CONTROL) & 0x8000) { return 1; } else if(pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN ) { return 1; } else if(pkbhs->vkCode == VK_ESCAPE && pkbhs->flags &LLKHF_ALTDOWN) { return 1; } else if(pkbhs->vkCode == VK_F4 && pkbhs->flags & LLKHF_ALTDOWN) { return 1; } else if(wParam == WM_KEYDOWN && pkbhs->vkCode == VK_SNAPSHOT) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYUP && pkbhs->vkCode == VK_SNAPSHOT) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYDOWN && pkbhs->vkCode == VK_LWIN) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYUP && pkbhs->vkCode == VK_LWIN) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYDOWN && pkbhs->vkCode == VK_RWIN) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYUP && pkbhs->vkCode == VK_RWIN) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYDOWN && pkbhs->vkCode == VK_TAB) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYUP && pkbhs->vkCode == VK_TAB) { MainWindow::getInstance()->todo(); return 1; } else if(wParam == WM_KEYDOWN && pkbhs->vkCode == 68) { if(GetKeyState(VK_LWIN)) { MainWindow::getInstance()->todo(); return 1; } } } return CallNextHookEx(keyHook, nCode, wParam, lParam); }
void hook(bool flag) { if(flag) { keyHook = SetWindowsHookEx(WH_KEYBOARD_LL,keyProc,GetModuleHandle(nullptr),0); } else { UnhookWindowsHookEx(keyHook); keyHook= nullptr; } }
MainWindow* instance; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow) { ui->setupUi(this); instance = this; hook(true); }
MainWindow::~MainWindow() { hook(false); delete ui; }
MainWindow *MainWindow::getInstance() { return instance; }
|