Global Tasten abfragen
-
Hallo,
ich verzweifle grade beim Hooken vom Keyboard.
Ich benutze SetWindowsHookEx und gebe eine Funktion aus einer DLL an.
Ausserdem gebe ich der DLL über eine andere Funktion ein HWND an das das KeyboardProc eine Message senden soll.Das hooken funktioniert richtig wenn mein eigenes Fenster foreground ist.
Auch scheint der KeyboardProc immer aufgerufen zu werden. Nur leider funktioniert dann das senden von Messages an mein Fenster nicht, wenn es nicht foreground ist.Hier mein CODE für den Hook und die DLL:
int StartKeyLogger(LPCTSTR strPath, HWND hWnd) { g_hDLL = LoadLibrary(strPath); if (!g_hDLL) return -1; g_hProc = (HOOKPROC)GetProcAddress(g_hDLL, "KeyboardProc"); if (!g_hProc) return -2; g_hHook = SetWindowsHookEx(WH_KEYBOARD, g_hProc, g_hDLL, 0); if (!g_hHook) return -3; g_hSet = (SETHWND)GetProcAddress(g_hDLL, "SetWnd"); if (!g_hSet) return -4; g_hSet(hWnd); return 1; };Und das hier ist der Inhalt meiner DLL:
#include <windows.h> #define EOF (-1) #ifdef __cplusplus // If used by C++ code, extern "C" { // we need to export the C interface #endif HWND g_hWnd = NULL; __declspec(dllexport) LRESULT KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if ((nCode == HC_ACTION) && (wParam == 'I') && (GetAsyncKeyState(VK_CONTROL))) { PostMessage(g_hWnd, 501, (WPARAM)GetForegroundWindow(), 0); }; return CallNextHookEx(0, nCode, wParam, lParam); }; __declspec(dllexport) void SetWnd(HWND hWnd) { g_hWnd = hWnd; }; #ifdef __cplusplus } #endif
-
Es gibt diverse Beispiele wie man sowas (erfolgreich) macht...
Das g_hWnd ist in jedem Prozess eine lokale Variable...
Schau Dir doch mal bitte die Beispiele an (codeproject.com)