Low Level Keyboard Hook called intermittently, GetMessage never returns

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 02:49:36

问题


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 );
}

来源:https://stackoverflow.com/questions/22937950/low-level-keyboard-hook-called-intermittently-getmessage-never-returns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!