gpt4 book ai didi

c++ - 为什么 VkKeyScanW 对 unicode 字符返回 -1?

转载 作者:可可西里 更新时间:2023-11-01 11:51:06 25 4
gpt4 key购买 nike

我正在尝试使用 VkKeyScanW 函数将字符转换为相应的虚拟键代码。它适用于 ASCII 字符,但在传递 unicode 字符(中文/俄语字符)时失败。

short vcode = VkKeyScanW(ch); //ch is of wchar_t type;
cout<<"key code:"<<vcode<<endl; //always prints -1 for unicode characters.

我在这里做错了什么?还有其他方法可以实现吗?我想将虚拟键代码传递给 SendInput 函数。

VkKeyScanW function documentation

最佳答案

VkKeyScan() documentation说:

Translates a character to the corresponding virtual-key code and shift state for the current keyboard.

...

Return value

...

If the function finds no key that translates to the passed character code, both the low-order and high-order bytes contain –1.

很明显,它无法在当前键盘布局中为您传递给它的 Unicode 字符找到匹配的键码。

尝试使用 LoadKeyboardLayout()VkKeyScanEx()相反,请确保选择中文/俄语键盘布局。

还要记住 VkKeyScanW()VkKeyScanExW() 只接受 1 个 WCHAR 作为输入,但像中文这样的语言通常需要 2 个WCHAR 值作为代理对一起工作以形成 Unicode 字符。此类字符不能用于 VkKeyScan/Ex()

也就是说,您无需将 Unicode 字符转换为虚拟键码即可使用 SendInput()。它可以使用其 KEYEVENTF_UNICODE 标志直接接受 Unicode 字符。在代理对的情况下,您只需指定两个输入事件,一个用于每个 WCHAR

关于c++ - 为什么 VkKeyScanW 对 unicode 字符返回 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com