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); } }
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.
GetAsyncKeyStatewill tell you the state of the key right now, andGetKeyStatewill tell you the state of the key as of the last message removed from the message queue.