c++winapikeyboard-shortcuts

GetAsyncKeyState() returns wrong value for VK_LCONTROL parameter when right alt is held down


I wonder if I did something incorrectly, or if this is a Windows bug. Here is my code:

#include <iostream>
#include <Windows.h>
    
using namespace std;
    
int main()
{
    bool quit = false;
    while (!quit)
    {
        bool rightAltMod = GetAsyncKeyState(VK_RMENU);
        bool leftControlMod = GetAsyncKeyState(VK_LCONTROL);
        //press and hold right alt to see the bug
        cout << "rAlt pressed " << rightAltMod << ", lCtrl pressed " << leftControlMod << "\n";

        quit = GetAsyncKeyState(VK_ESCAPE);
    }
    
    return 0;
}

The bug(?) is when I press and hold Right-Alt, GetAsyncKeyState() also detects it as Left-Ctrl.

If this is a bug, is there any workaround for it?

I have no ideas except direct access to keyboard buffer using assembler.

I'm developing on Windows 10 x64 21H1.


Solution

  • I have found what causes the behavior - it is related to keyboard layout. The bug occurrs on Polish-programmers layout, after switching to EN-US layout it works fine. Well, still I'll need to solve the problem, but for that I'll create a separate question.