'How do I distinguish between left and right keys (CTRL and ALT)?

I started making use of Win32's raw input features to detect all keys on the keyboard. So far, everything is working great! I can distinguish between numbers on the top row and numbers in the keypad on the right. I can even detect between the left and right shift keys. However, the control and alt keys do not return unique scan codes. The control key returns 29, and the alt key returns 56.

The popular method for checking key states on these keys is GetAsyncKeyState. I have tested that function using VK_LCONTROL and VK_RCONTROL, and it works, but that only helps me for capturing key down events. I would really like to be able to capture key up events as well. It is obvious that the API is somehow aware of which key is being pressed; how do I get ahold of that information?

I am currently extracting the scan code from the RAWKEYBOARD structure's MakeCode field. That gives me information about every key (and its left/right alignment) except CTRL and ALT. How would I go about capturing the key up events (and knowing whether it is left/right)? Is it possible using just the RAWKEYBOARD structure? Or do I have to concoct some kind of workaround?



Solution 1:[1]

GetAsyncKeyState's documentation says that:

... If the most significant bit is set, the key is down ...

which also means that if the MSB it cleared, the key is up.

Solution 2:[2]

To check extended key flag for "right" key in lParam also can do this way.

If true - it's right Ctrl or Alt, based on wParam == VK_CONTROL or VK_MENU

otherwise its's left Ctrl or Alt >

(HIWORD(lParam) & KF_EXTENDED) == KF_EXTENDED

To get scan code also can do this >

BYTE scan_code = LOBYTE(HIWORD(lParam));

for use

MapVirtualKey(scan_code, MAPVK_VSC_TO_VK_EX);

for detect VK_LSHIFT or VK_RSHIFT based on wParam == VK_SHIFT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Donotalo
Solution 2