Virtual Key Codes mittels Tastaturlayout umwandeln
-
Hi,
Ich schreibe grad einen Low Level Keyboard Hook.
Mein Programm sieht bis jetzt so aus:#include <iostream> #include <windows.h> HHOOK hKeyHook; KBDLLHOOKSTRUCT kbdStruct; UINT KeyCode; char kbdName[KL_NAMELENGTH]; LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam) { if( (nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)) ) { switch(nCode) { case WM_KEYDOWN: break; case WM_KEYUP: break; case WM_SYSKEYDOWN: break; case WM_SYSKEYUP: break; } kbdStruct = *((KBDLLHOOKSTRUCT*)lParam); GetKeyboardLayoutName(kbdName); KeyCode = MapVirtualKeyEx(kbdStruct.vkCode, 2, (HKL)kbdName); std::cout << "Layout: " << kbdName << std::endl; std::cout << (unsigned int)kbdStruct.vkCode << " " << (char)kbdStruct.vkCode << std::endl; std::cout << "Key: " << (char)KeyCode << std::endl; } return CallNextHookEx(hKeyHook, nCode, wParam, lParam); } int getKeyboardLayoutFile(BYTE* layoutFile, DWORD bufferSize) { HKEY hKey; DWORD varType = REG_SZ; char kbdName[KL_NAMELENGTH]; GetKeyboardLayoutName(kbdName); char kbdKeyPath[51 + KL_NAMELENGTH]; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)kbdKeyPath, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) return -1; if(RegQueryValueEx(hKey, "Layout File", NULL, &varType, layoutFile, &bufferSize) != ERROR_SUCCESS) return -1; RegCloseKey(hKey); return 1; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow) { hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyEvent, GetModuleHandle(NULL), 0); MSG message; while(GetMessage(&message, NULL, 0, 0)) { TranslateMessage(&message); DispatchMessage(&message); } UnhookWindowsHookEx(hKeyHook); return 0; }Ich versuche da über das Keyboard Layout mit der Funktion MapVirtualKeyEx die Virtual Key Codes zu konvertieren, wobei es nicht funktioniert.
Könnte mir jemand bei diesem Ansatz weiterhelfen und ein paar Tipps geben.
Schon mal danke im Voraus.LG
HKEY
-
Erste Frage: Wie genau äußert sich denn dieses "funktioniert nicht"? Und hast du mal versucht, das ganze per Debugger zu beobachten.
Zweite Frage: Welchen Sinn soll denn die switch-Anweisung ab Zeile 13 haben?
Drittens: Laut MSDN erwartet MapVirtualKeyEx() ein Handle, das von LoadKeyboardLayout() angelegt worden ist, keinen fehlgecasteten String.
Viertens: deine Funktion getKeyboardLayoutFile wird erstens nie aufgerufen und beeinflusst zweitens das globale
char kbdName[]nicht (weil sie eine gleichnamige lokale Variable definiert).
-
Zu 1:
Das Problem ist, dass Umlaute oder Punkt und Komma nicht richtig angezeigt werden. Mit Debuggen hab ich noch nicht so viel Erfahrung gemacht.Zu 2:
Da kommt später noch Code rein. Hätte ich hier aber auch weglassen können.Zu 3:
Ok, hab ich geändert.Zu 4:
Die Funktion brauch ich auch erst mal nicht.Mein Programm sieht jetzt so aus:
//#include <stdio.h> #include <iostream> #include <windows.h> HHOOK hKeyHook; KBDLLHOOKSTRUCT kbdStruct; UINT KeyCode; char kbdName[KL_NAMELENGTH]; LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam) { if( (nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)) ) { switch(nCode) { case WM_KEYDOWN: break; case WM_KEYUP: break; case WM_SYSKEYDOWN: break; case WM_SYSKEYUP: break; } kbdStruct = *((KBDLLHOOKSTRUCT*)lParam); GetKeyboardLayoutName(kbdName); KeyCode = MapVirtualKeyEx(kbdStruct.vkCode, 2, LoadKeyboardLayout(kbdName, 1)); std::cout << "Layout: " << kbdName << std::endl; std::cout << (unsigned int)kbdStruct.vkCode << " " << (char)kbdStruct.vkCode << std::endl; std::cout << "Key: " << (char)KeyCode << std::endl; } return CallNextHookEx(hKeyHook, nCode, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow) { hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyEvent, GetModuleHandle(NULL), 0); MSG message; while(GetMessage(&message, NULL, 0, 0)) { TranslateMessage(&message); DispatchMessage(&message); } UnhookWindowsHookEx(hKeyHook); return 0; } Das Problem ist jetzt immer noch, dass es die Umlaute, ß und ´ nicht richtig konvertiert.