I have a working Windows hook to detect the simple keypress combinations of both LCTRL + x, and LCTRL + v. The only problem I have is when I press LCTRL, then release, then press x or v, my program thinks they are still being pressed together. Here is my Hook Callback function:
HHOOK hookHandle;
KBDLLHOOKSTRUCT hookdata;
LRESULT __stdcall HookCallback(int code, WPARAM wparam, LPARAM lparam)
{
if (code >= 0)
{
if (wparam == WM_KEYDOWN)
{
hookdata = *((KBDLLHOOKSTRUCT*)lparam);
switch(hookdata.vkCode)
{
case(88):
if(GetAsyncKeyState(VK_LCONTROL))
{
std::cout<<"CTRL X PRESSED";
}
break;
case (86):
if(GetAsyncKeyState(VK_LCONTROL))
{
std::cout<<"CTRL V PRESSED";
}
break;
}
}
}
return CallNextHookEx(hookHandle, code, wparam, lparam);
}
Example Input
Test 1) Press LCTRL, Press X
Test 2) Press LCTRL, Release LCTRL, Press X
Expected Output
Test 1) CTRL X PRESSED
Test 2) No Output
Actual Output
Test 1) CTRL X PRESSED (WORKING)
Test 2) CTRL X PRESSED (NOT WORKING)
You're incorrectly checking
GetAsyncKeyState's return value:Change the check to
GetAsyncKeyState(...) & 0x8000to fix that by checking only the highest bit.A small thing: Microsoft made the VK values the same as the ASCII code. Since complete portability is of small concern here, you can use
'X'and'V'instead of88and86to better express intent.