I set up a low level keyboard hook in a worker thread that also runs a message loop. About 1/3 of my key strokes trigger the hook function and none release the GetMessage function in my message loop. Because of something related to the latter, messages aren't queued either (I just started working with Windows' message loop). What would cause only some keystroke to trigger a hook? And is there some setting/function call I'm missing to have the GetMessage to work correctly (according to this I'm not missing anything)?
This is my hook setup and message loop:
MIL_UINT32 MFTYPE MessageThread( void *v_DataEx )
{
MSG msg;
// Setup key listener
keyEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
HINSTANCE instance = GetModuleHandle(NULL);
hookRes = SetWindowsHookEx( WH_KEYBOARD_LL, &KeyStrokeHook, instance, 0 );
HWND h = FindWindow( NULL, NULL );
while(GetMessage( &msg, h, 0, 0 ) > 0) // also tried with h = 0
{
printf("Received message\n");
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx( hookRes );
return M_TRUE;
}
And my hook function:
LRESULT CALLBACK KeyStrokeHook( _In_ int code, _In_ WPARAM wParam, _In_ LPARAM lParam )
{
if( code < 0 )
return CallNextHookEx( hookRes, code, wParam, lParam );
if( wParam == WM_KEYDOWN ) // If key pressed, not released
{
keyStroke = ((KBDLLHOOKSTRUCT *)lParam)->vkCode;
SetEvent( keyEvent );
}
return CallNextHookEx( hookRes, code, wParam, lParam );
}