I am trying to understand how to init a LowLevelKeyboardProc hook.
In my minimal approach for a key logger as shown below, why do I need the message-loop to get the hook working even if it just hangs within the first loop and never prints "MSG\r\n" ?
And how could I ever Unhook if there is no chance to recheck the while-condition?
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
LRESULT CALLBACK LLKBProc(int nCode, WPARAM wParam, LPARAM lParam) {
KBDLLHOOKSTRUCT *keyMetaP = (KBDLLHOOKSTRUCT *) lParam;
printf("\nkey = %x", keyMetaP->vkCode);
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main() {
// get handle to process
HINSTANCE hExe = GetModuleHandle(NULL);
// set hook
HHOOK keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) LLKBProc, hExe, 0);
// msg loop
MSG msg;
while ( GetMessage(&msg, 0, 0, 0) != -1 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
printf("MSG\r\n");
}
UnhookWindowsHookEx(keyHook);
return 1;
}