转换国际键盘的 CGKeyCode
在我的应用程序中,我需要将快捷键映射到键盘上“1”键左侧的键。在标准美国键盘上,这将是反引号字符 (" ` "),即键代码编号 50。不幸的是,国际键盘(例如法语键盘)在 1 键的左侧有一个不同的键(正斜杠键“/”),因此对该键代码进行硬编码可能会导致不使用美国键盘的用户出现意外结果。
有没有办法在运行时将美国键代码转换为国际键盘的键代码,或者有一种方法可以根据键盘上键的位置以编程方式确定键代码?
In my application, I need to map a shortcut to the key to the left of the "1" key on the keyboard. On a standard US keyboard, this would be the backtick character (" ` "), which is key code number 50. Unfortunately, international keyboards (the French keyboard, for example) has a different key to the left of the 1 key (the forward slash key "/"), so hard coding that key code would result in unexpected results for users who are not using a US keyboard.
Is there any way to convert a US key code into a key code for international keyboards at runtime, or a way to programatically determine the key code based on the position of the key on the keyboard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“1”左侧按键的字符在不同的键盘布局上是不同的,但虚拟键码应该是相同的。如果您查看
HIToolbox/Events.h
,您可以看到常量kVK_ANSI_Grave
,它代表您正在谈论的键;在常量列表上方,有一条注释表明在虚拟键代码级别,相等意味着物理键是相同的,尽管扫描代码可能不同并且发出的字母可能不同。换句话说:键盘驱动程序从扫描代码映射到虚拟键代码,键盘布局(您可以在系统偏好设置中更改)从虚拟键代码映射到字符。
这一切都可能是错误的;我没有非美国键盘来验证这些断言。
The character on the key to the left of "1" is different on different keyboard layouts, but the virtual key code should be the same. If you look at
HIToolbox/Events.h
, you can see the constantkVK_ANSI_Grave
, which represents the key you're talking about; above the list of constants, there's a comment that suggests that at the virtual key code level, equality means that the physical key is the same, though the scan code might be different and the emitted letter might be different.In other words: the keyboard driver maps from scan codes to virtual key codes, and the keyboard layout (which you can change in System Preferences) maps from virtual key codes to characters.
This is all potentially wrong; I don't have a non-US keyboard with which to verify these assertions.