0

当下面的代码片段运行时,每次点击一个键时,都会调用函数LowLevelKeyboardProc。但问题是它在按下一个键时和释放一个键时都被调用。这意味着,每次点击该函数被调用两次。但是我希望该函数在键被调用时只被调用一次按下而不是释放键时。

即使我可以在函数LowLevelKeyboardProc中过滤按键的按下和释放,也可以。有什么办法可以做到这一点。

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  // call the function only if the key is pressed,else ( i.e key released) do nothing
}

BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved){
  handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstDLL, 0);
  MSG msg;

  while(GetMessage(&msg, NULL, 0, 0)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) { // ENTRY POINT
 if(hookThreadHandle==NULL) { 
    LPTHREAD_START_ROUTINE lpStartAddress = &installHook;
    hookThreadHandle = CreateThread(NULL, 0, lpStartAddress, NULL, 0, &hookThreadId);
 }
 return TRUE;
}
4

1 回答 1

1

您无法更改调用 LowLevelKeyboardProc 的方式,但在函数内部您可以知道调用的原因:

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{
     if ( nCode < 0 )
     {
         return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
     }

     if ( wParam == WM_KEYDOWN )  // possibly you want also WM_SYSKEYDOWN
     {
         // do what you need
     }
}
于 2012-10-14T08:06:21.250 回答