'MapVirtualKeyExA with different keyboard layouts

I try to use MapVirtualKeyExA with different keyboard layouts. This function always returns English letters, no matter what keyboardlayout parameter I pass.

enter image description here

I use console app and get layout by GetKeyboardLayout:

 HWND foreground = GetForegroundWindow();
 DWORD threadId;
 HKL keyboardLayout;
 if (foreground) 
 {
    threadId = GetWindowThreadProcessId(foreground, NULL);
    keyboardLayout = GetKeyboardLayout(threadId); 
 }

Convert keyCode to char :

char crrKey;
bool lower = ((GetKeyState(VK_CAPITAL) & 0x0001) != 0);
if ((GetKeyState(VK_SHIFT) & 0x1000) != 0 || (GetKeyState(VK_LSHIFT) & 0x1000) != 0 || (GetKeyState(VK_RSHIFT) & 0x1000) != 0) 
{
    lower = !lower;
}
crrKey = MapVirtualKeyExA(key, MAPVK_VK_TO_CHAR, keyboardLayout);
cout << keyboardLayout << ": " << crrKey << endl;
if (!lower) 
{
    crrKey = tolower(crrKey);
}
file << char(crrKey);

I want that when changing the language (Windows + Space) VirtualKey is mapped to the new language. What am I doing wrong?



Solution 1:[1]

I suggest you could use ToUnicodeEx API to translate the specified virtual-key code and keyboard state to the corresponding Unicode character or characters.

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 Jeaninez - MSFT