Ich erstelle eine Konsolenanwendung, in der ich Tasten drücken möchte (wie den NACH-OBEN-PFEIL). Ich habe einen Low Level Keyboard Hook erstellt, der alle Key Presses in jedem Thread erfassen soll und meine Callback-Funktion aufrufen soll, aber es funktioniert nicht. Das Programm bleibt ein bisschen stehen, wenn ich einen Schlüssel drücke, aber ruft nie den Rückruf auf. Ich habe die Dokumentation überprüft, aber nichts gefunden. Ich weiß nicht, ob ich SetWindowsHookEx() falsch benutze (meines Wissens schafft es erfolgreich den Haken) oder meine Rückruffunktion ist falsch! Ich bin mir nicht sicher, was falsch ist! Vielen Dank im Voraus für die Hilfe.C++ SetWindowsHookEx WH_KEYBOARD_LL Richtiges Setup
#include "Windows.h"
#include <iostream>
using namespace std;
HHOOK hookHandle;
LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam);
int _tmain(int argc, _TCHAR* argv[]) {
hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, NULL, 0);
if(hookHandle == NULL) {
cout << "ERROR CREATING HOOK: ";
cout << GetLastError() << endl;
getchar();
return 0;
}
MSG message;
while(GetMessage(&message, NULL, 0, 0) != 0) {
TranslateMessage(&message);
DispatchMessage(&message);
}
cout << "Press any key to quit...";
getchar();
UnhookWindowsHookEx(hookHandle);
return 0;
}
LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam) {
cout << "Hello!" << endl;
// Checks whether params contain action about keystroke
if(nCode == HC_ACTION) {
cout << ((KBDLLHOOKSTRUCT *) lParam)->vkCode << endl;
}
return CallNextHookEx(hookHandle, nCode,
wParam, lParam);
}
Bitte beachten Sie [hier] (http://stackoverflow.com/questions/2127112/global-keyboard-hook-not-working/2127292 # 2127292) für eine ähnliche Frage, die ich beantwortet habe, die Ihnen helfen wird. – t0mm13b