我正在尝试在他们的 Windows 机器上的不同安装语言之间翻译用户的击键。我找到了这篇关于虚拟键码的文章,以及它们如何映射到字符,以及执行映射的函数。但它似乎不像我预期的那样工作。
这是我尝试发送“A”的虚拟键代码(即 0x41),并将其转换为希伯来语键盘中的字符“ש”(这是按下该键输出到屏幕的内容,而用户是在希伯来语键盘布局上)。无论我当前的活动布局如何,它仍然只打印“A”。
#include
#include
#include
#include
int main()
{
HKL lpList[2];
GetKeyboardLayoutList(2, lpList); // returns {0x04090409 , 0xf03d040d} on my machine, which is {en-US, he-IL}
HKL hkl = lpList[1]; // sets to he-IL
char ch = MapVirtualKeyEx(0x41, MAPVK_VK_TO_CHAR, hkl); //0x41 is the Virtual Key of the keyboard button 'A'
std::cout << "ch: " << ch << std::endl; //prints "ch: A", I want it to print "ch: ש"
}
我错过了什么?还有其他方法可以实现我想要做的事情吗?
我刚试过
UINT VKCode = LOBYTE(VkKeyScan('ש')); // returns 0xbf
UINT ScanCode = MapVirtualKeyEx(VKCode, MAPVK_VK_TO_VSC, hkl); // returns 0x35
UINT VKCode2 = MapVirtualKeyEx(ScanCode, MAPVK_VSC_TO_VK, hkl); // once again 0xbf - unsurprisingly
TCHAR ch = MapVirtualKeyEx(VKCode2, MAPVK_VK_TO_CHAR, hkl); // now it returns '.'
所以我转换了 char -> vk -> sc -> vk -> char,最后得到的字符与我开始使用的字符不同。也许有另一种方法可以将“虚拟键代码*”转换为字符?