As is known, in most keyboard national layouts, the right Alt key functions as AltGr. However, AltGr is not a “real” key; it doesn’t have a specific VirtualKeyCode/ScanCode, and programmatically its depression appears as two separate sequential presses:
- Left Ctrl
- Right Alt
Multiple discussions on Stack Overflow pertain to this, but so far, no one has proposed a way to programmatically distinguish whether AltGr is currently being pressed or if it’s a combination of LCtrl+RAlt.
At the same time, there is reason to believe that it is possible, given that the OSK (On-Screen Keyboard, C:WindowsSystem32osk.exe
) application differentiates between them. This can be easily verified by experimenting with key presses and observing corresponding key changes on the screen.
So, the question remains open: How can one differentiate between AltGr and LCtrl+RAlt programmatically?
I attempted to observe the sequence of events triggered by pressing AltGr, looking for any characteristic features in the structure of INPUT_RECORD.
I also tried to ascertain the status of the VK_LCONTROL
, VK_RCONTROL
, VK_LMENU
, VK_RMENU
keys through GetKeyState. However, I did not find anything specific to AltGr that could distinguish it from pressing LCtrl+RAlt.
Nevertheless, the On-Screen Keyboard (OSK) somehow resolves this issue. For instance, you can press (and hold) Ctrl+AltGr and it will show that the keys have been pressed individually.
P.S.
Please note that there is a similar question at Windows: Detect if right alt generates Ctrl+Alt (AltGr) in current layout.
However, that question is slightly different: it inquires about how to discern if a keyboard layout treats RAlt as AltGr. Here, on the other hand, I am trying to figure out something slightly different.