当下面的代码片段运行时,每次敲击一个键时,都会调用函数 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;
}
答案 0 :(得分: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
}
}