C++ keyboard hook CTRL key gets stuck

Deadly 提交于 2020-03-25 18:41:40

问题


I'm looking to rewrite ctrl+c and ctrl+v on my Windows 10 machine to add some additional functionality.

I'm able to copy and paste correctly and have successfully created a keyboard hook to execute my code after these keys are pressed but I'm having an issue after I press ctrl while my program is running, ctrl continuously acts as if it's held down. Even after I terminate the program entirely, ctrl continues to act as if it's being held down until I logout of the computer entirely. What can I do to correct this?

Thanks!

Edit: after doing a bit of messing around, I can conclude any key gets stuck. Shift and caps lock get stuck as well.

#include <Windows.h>
#include <stdio.h>
#include <queue>

using namespace std;

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
    if (wParam == WM_KEYDOWN) {
        if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
            WM_COPY;
        }
        else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
            OpenClipboard(NULL);
            char* buffer;
            buffer = (char*)GetClipboardData(CF_TEXT);
            CloseClipboard();
            cout << buffer;
        }
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
}

int main()
{
    HHOOK keyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL);
    MSG msg;

    while (!GetMessage(&msg, NULL, NULL, NULL)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    UnhookWindowsHookEx(keyBoard);
}

回答1:


Do not put return CallNextHookEx(NULL, nCode, wParam, lParam) in if (wParam == WM_KEYDOWN).

Modify:

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
    if (wParam == WM_KEYDOWN) {
        if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
            WM_COPY;
        }
        else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
            OpenClipboard(NULL);
            char* buffer;
            buffer = (char*)GetClipboardData(CF_TEXT);
            CloseClipboard();
            cout << buffer;
        }

    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}


来源:https://stackoverflow.com/questions/59836869/c-keyboard-hook-ctrl-key-gets-stuck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!