挂钩keydown事件时如何防止循环?

时间:2018-05-05 18:16:33

标签: c++ winapi hook

我希望挂钩键盘,但如果我挂钩,

当我按下并按住它时,

它一直打电话直到我发布它

但我只想像键盘一样打电话

我该怎么办?

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (wParam == WM_KEYDOWN)
    {
        PKBDLLHOOKSTRUCT pKey = (PKBDLLHOOKSTRUCT)lParam;
        int key = pKey->vkCode;
        cout << key << endl;

    }
    if (wParam == WM_KEYUP)
    {
        PKBDLLHOOKSTRUCT pKey = (PKBDLLHOOKSTRUCT)lParam;
        int key = pKey->vkCode;
        cout << key << endl;
    }
    CallNextHookEx(hKBHook, nCode, wParam, lParam);
    return 0;
}

int _tmain() {
    HMODULE hInstance = GetModuleHandle(NULL);
    hKBHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInstance, NULL);

    MSG Msg;

    while (GetMessage(&Msg, NULL, 0, 0)) { DispatchMessage(&Msg); }

    return 0;
}

0 个答案:

没有答案