1

下面的函数正在记录“0”、“z”和“1” ok...但它没有捕获“Z”(shift-z)...任何帮助将不胜感激...

__declspec(dllexport)
LRESULT CALLBACK HookProc (UINT nCode, WPARAM wParam, LPARAM lParam)
{
    if ((nCode == HC_ACTION) && (wParam == WM_KEYUP))
    {
        // This Struct gets infos on typed key
        KBDLLHOOKSTRUCT hookstruct = *((KBDLLHOOKSTRUCT*)lParam);

        // Bytes written counter for WriteFile()
        DWORD Counter;

        wchar_t Logger[1];

        switch (hookstruct.vkCode)
        {
        case 060: Logger[0] = L'0'; break;
        case 061: Logger[0] = L'1'; break;
        case 90: Logger[0] = L'z'; break;
        case 116: Logger[0] = L'Z'; break;
        }

        // Opening of a logfile. Creating it if it does not exists
        HANDLE  hFile = CreateFile(L"C:\\logfile.txt", GENERIC_WRITE,
            FILE_SHARE_READ, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

        // put the file pointer to the end
        SetFilePointer(hFile,NULL,NULL,FILE_END);

        // Write the hFile typed in logfile
        WriteFile(hFile,&Logger,sizeof(Logger),&Counter,NULL);

        //WriteFile(hFile,&hookstruct.vkCode,sizeof(hookstruct.vkCode),&Counter,NULL);
        // Close the file
        CloseHandle(hFile);
    }
}
4

2 回答 2

10

键盘不发送字符。它发送密钥。无论您是输入z还是Z,您仍然按同一个键,并且该键两次具有相同的 VK 代码。

您还应该在按下或释放 Shift 键时收到通知。您可以使用这些通知将击键转换为字符。大写锁定状态也与此相关。您可能还担心死键。

您可以检查是否按下了 Shift 键。GetAsyncKeyState立即告诉您密钥的状态,GetKeyState并将告诉您从消息队列中删除的最后一条消息时的密钥状态。

于 2008-12-04T22:02:47.637 回答
6

Z 没有虚拟键码。试试这样的方法:

            case 90:
                 if(GetKeyState(VK_LSHIFT|VK_RSHIFT)
                     Logger[0] = L'Z'; break;
                 else
                     Logger[0] = L'z'; break;
于 2008-12-04T21:49:25.430 回答