You could use Raw Input/LowLevelKeyboard hook to get key down/up events instead of polling.
For Raw Input, you could refer to this answer,
- Use
RegisterRawInputDevices
to register for your window;
- Then your window will get the
WM_INPUT
message when there is any raw input;
- Identify the key;
- Record the status of ctrl;
- When key 'A' is pressed, check the ctrl status.
Sample in C++:
LRESULT CALLBACK WindProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
static BOOL ctrl_state = FALSE; //up
if (Msg == WM_INPUT)
{
HRAWINPUT hRawInput = (HRAWINPUT)lParam;
RAWINPUT input = { 0 };
UINT size = sizeof(input);
GetRawInputData(hRawInput, RID_INPUT, &input, &size, sizeof(RAWINPUTHEADER));
switch (input.data.keyboard.VKey)
{
case VK_CONTROL:
if(input.data.keyboard.Flags & RI_KEY_BREAK)
ctrl_state = TRUE;
else
ctrl_state = FALSE;
break;
case 0x41:
if (input.data.keyboard.Flags & RI_KEY_BREAK)
OutputDebugString(L"Ctrl + A pressed");
break;
default:
break;
}
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}