I would like to enumerate the list of all possible key combinations supported by the current keyboard layout (with virtual key codes, scan codes and their Unicode values). To map the remote user inputs to keys in order to simulate them.
I was expecting an API like UCKeyTranslate(ObjectiveC) for VC++ that can accept the virtual key codes and modifiers(ALT, SHIFT, CTRL) and provide me the scan codes, but couldn't find anything similar to that.
After a lot of research and spending 2 whole days, I had no other option to go with except for MapVirtualKeyEx.
I came with the following code but that has a lot of problems,
BOOL PopulateKeyMap()
{
TCHAR Buff[120 * sizeof(TCHAR)] = { 0 };
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, Buff, sizeof(Buff));
HKL hKeyboardLayout = ::LoadKeyboardLayout(Buff, KLF_ACTIVATE);
{
lock_guard<recursive_mutex> lockHolder(cs_populate_key);
if (hCurrentKeyboardLayout)
{
UnloadKeyboardLayout(hCurrentKeyboardLayout);
}
hCurrentKeyboardLayout = hKeyboardLayout;
// Prepopulate keyCodeDictionary with common key combinations.
for (int keyIndex = 0; keyIndex < KEY_CODES_DICT_SIZE; ++keyIndex)
{
{
unsigned int Vk;
tstring key_name = GetKeyName(keyIndex, Vk);
if (key_name.compare(_T("")) != 0)
{
SmartPtr<KeyCodeInfo> key = (new KeyCodeInfo);
if (key)
{
key->nIndex = keyIndex;
key->sVKCode = keyIndex;
key->nScanCode = Vk;
keyboard_map[key_name] = key;
}
}// End if
}
}// End for
bKeyMapInitialized = TRUE;
}
return TRUE;
}
tstring GetKeyName(unsigned int virtualKey, unsigned int &scanCode)
{
if (!hCurrentKeyboardLayout)
{
PopulateKeyMap();
}
scanCode = MapVirtualKeyEx(virtualKey, MAPVK_VK_TO_VSC, hCurrentKeyboardLayout);
// because MapVirtualKey strips the extended bit for some keys
switch (virtualKey)
{
case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: // arrow keys
case VK_PRIOR: case VK_NEXT: // page up and page down
case VK_END: case VK_HOME:
case VK_INSERT: case VK_DELETE:
case VK_DIVIDE: // numpad slash
case VK_NUMLOCK:
{
scanCode |= KF_EXTENDED; // set extended bit
break;
}
}
TCHAR keyName[256];
if (GetKeyNameText(scanCode << 16, keyName, sizeof(keyName)) != 0)
{
return keyName;
}
else
{
return _T("");
}
}
The MapVirtualKeyEx provides me only the list of basic scan codes and not the scan codes of the keys with the combinations of the modifiers (ALT, CTRL, SHIFT). Is there any way I can provide the combinations of the modifiers as input to the function so that I can generate the required key combinations?.
Any help would be appreciated. Thanks in advance.