转换国际键盘的 CGKeyCode

发布于 2024-10-21 03:56:27 字数 215 浏览 2 评论 0原文

在我的应用程序中,我需要将快捷键映射到键盘上“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-10-28 03:56:27

“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 constant kVK_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文