如何修复"Ignored keystrokes"

How to fix"Ignored keystrokes"

我的问题是添加捕获键后,我的代码开始无法正常工作。

更详细地说:按下组合键(在我的例子中是 CTRL + q 和 CTRL + e)后程序的任务是在某些程序中模拟击键。如果你在程序中使用按钮,一切正常,但在添加 catch 后,箭头停止工作(Enter 和 Esc 工作,我没有检查其余的键)。

我需要这个在某些程序中进行快速搜索,在这些程序中你必须多次按键盘上的箭头。

__declspec(dllexport) LRESULT CALLBACK KeyboardEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
    DWORD CTRL_key = 0;

    if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
    {
        KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
        DWORD dwMsg = 1;
        dwMsg += hooked_key.scanCode << 16;
        dwMsg += hooked_key.flags << 24;
        char lpszKeyName[1024] = { 0 };

        int i = GetKeyNameText(dwMsg, (lpszKeyName + 1), 0xFF) + 1;

        int key = hooked_key.vkCode;

        CTRL_key = GetAsyncKeyState(VK_CONTROL);

        if (key >= 'A' && key <= 'Z')
        {

            if (CTRL_key != 0 && key == 'q')
            {
                //MessageBox(NULL, "Pressed", "H O T K E Y", MB_OK);
                CTRL_key = 0;
                //
                Sleep(1000);
                keybd_event(VK_RETURN, 0x9C, 0, 0);
                Sleep(50);
                keybd_event(VK_RETURN, 0x9C, KEYEVENTF_KEYUP, 0);
                Sleep(100);
                for (int i = 0; i < 6; i++) {
                    keybd_event(VK_UP, 0x26, 0, 0);
                    Sleep(50);
                    keybd_event(VK_UP, 0x26, KEYEVENTF_KEYUP, 0);
                    Sleep(50);
                }
                for (int i = 0; i < 5; i++) {
                    keybd_event(VK_RIGHT, 0x27, 0, 0);
                    Sleep(50);
                    keybd_event(VK_RIGHT, 0x27, KEYEVENTF_KEYUP, 0);
                    Sleep(50);
                }
                keybd_event(VK_DOWN, 0x28, 0, 0);
                Sleep(50);
                keybd_event(VK_DOWN, 0x28, KEYEVENTF_KEYUP, 0);
                keybd_event(VK_RETURN, 0x9C, 0, 0);
                Sleep(50);
                keybd_event(VK_RETURN, 0x9C, KEYEVENTF_KEYUP, 0);
                Sleep(50);
                Sleep(6000);
                keybd_event(VK_ESCAPE, 0x1B, 0, 0);
                Sleep(500);
                keybd_event(VK_ESCAPE, 0x1B, KEYEVENTF_KEYUP, 0);
                Sleep(1000);
            }

            if (CTRL_key != 0 && key == 'e')
            {
                MessageBox(NULL, "Shutting down", "H O T K E Y", MB_OK);
                PostQuitMessage(0);
            }

            CTRL_key = 0;
        }

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

如果您想查看完整代码,请点击here! 提前致谢!

解决方法很简单。在函数中,当您单击箭头时,需要另一个参数。

                    keybd_event(VK_UP, 0x26, KEYEVENTF_EXTENDEDKEY, 0);
                    Sleep(50);
                    keybd_event(VK_UP, 0x26, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);