WH_KEYBOARD_LL不适用于常规字母和数字

时间:2019-05-27 07:45:37

标签: c++ winapi hook setwindowshookex wh-keyboard-ll

我正在使用SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0);设置用于捕获击键的全局挂钩,但是结果很奇怪。
当我按下“特殊”键(例如“ Enter”,“ Tab”,“ Shift”,“ Ctrl”)和其他具有虚拟键代码的键时,可以执行回调函数,但是当我按下时,它无法捕获键击常规字母和数字。
我对此感到困惑,有人可以告诉我原因吗?

#include <Windows.h>
#include <iostream>
using namespace std;

HHOOK keyboardHook = 0;

LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam)
{
    KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam;
    cout<< "[TEST] " << ks->vkCode << endl;

    return CallNextHookEx(0, code, wParam, lParam);
}

int main()
{
    keyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0);
    if (keyboardHook == 0)
    {
        cout << "failed" << endl;
        return -1;
    }
    cout << "ok" << endl;

    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    UnhookWindowsHookEx(keyboardHook);


    return 0;
}

0 个答案:

没有答案