独立于操作系统的键盘事件到字符的映射

发布于 2024-12-02 05:11:18 字数 567 浏览 2 评论 0原文

显然,Web 应用程序需要调整用户的键盘设置,对吧?有没有办法告诉Dojo 连接到实际的KeyPress 事件而不是KeyDown,这样我们就可以从event.charCode 获取输入的字符?

由于我们生活在一个国际化的世界,有多种操作系统等等,这些信息不足以找出用户实际输入的字符,除非我在浏览器中内置了一些功能来询问操作系统。

例如,在 Linux 上的德语键盘上,[ 通过 Alt Gr-8 到达,它发送 Alt 的按键,然后发送 [ 的按键。好吧,忽略第一部分。在具有德语键盘的 Windows 系统上,第二个事件是 ctrlKey 和 altKey 设置为 true 的 8。我认为 JavaScript 代码不应该解释硬编码,因为使用其他键盘设置,这个组合键实际上意味着不同的字符。

另一个例子(可能没有连接到 Dojo,而是另一个程序员的故障,抱歉咆哮……),在 mac 上使用美式键盘时,您无法在 Outlook Web 界面中键入德文字符 ß – 因为 Outlook 是假的( !) 劫持 alt 键(在 mac 上专门用于修改键入的字符)来触发操作,并且 alt-s 因此被重新映射为发送的意思。当然,通常是在单词的中间。

Obviously, a web application needs to adjust to the user's keyboard settings, right? Is there a way to tell Dojo to make a connection to the actual KeyPress event instead of KeyDown, so we can get the character typed from event.charCode?

Since we live in an international world, with multiple operating systems and what not, this information is not sufficient to find out what character the user actually typed, unless I have some function built into the browser to ask the operating system.

As an example, on a German keyboard on Linux, [ is reached via Alt Gr-8, which sends a keydown for Alt and then a keydown with [. Fine, just ignore the first part. On a windows system with a German keyboard, the second event is for an 8 with ctrlKey and altKey set true. Which I don't think JavaScript code should interpret hard-coded, because with other keyboard settings, this key combo will actually mean a different character.

As another example (probably not connected to Dojo, but rather a different programmer's glitch, sorry for the ranting …), with a US keyboard on mac, you can't type the German character ß within the outlook web interface – because outlook bogusly(!) hijacks the alt key (which on the mac is exclusively used to modify the characters typed) to trigger actions and alt-s is thus remapped to mean send. Typically in the middle of a word, of course.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

染柒℉ 2024-12-09 05:11:18

event.charCode 包含按下键盘时生成的字符,而不是按下的实际按键。

https://developer.mozilla.org/en/DOM/event.charCode#Notes

在按键事件中,按下的键的 Unicode 值存储在 keyCode 或 charCode 属性中,而不是同时存储在两者中。如果按下的键生成一个字符(例如“a”),则 charCode 将设置为该字符的代码,并考虑字母大小写

编辑: 另请参阅 https://developer.mozilla.org/en/Gecko_Keypress_Event,它包含关于 charCode 如何工作的更深入的解释(特别是在 Gecko 中,但其中一些也适用于其他浏览器)。您可能会发现这很有趣:

...当当前选择的键盘布局生成 Unicode 字符时(根据 CapsLock 和 NumLock 的当前状态),charCode 属性包含该字符

event.charCode contains the character that was produced from the keyboard press, not the actual key that was pressed.

https://developer.mozilla.org/en/DOM/event.charCode#Notes

In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case

edit: Also see https://developer.mozilla.org/en/Gecko_Keypress_Event, it contains a more insightful explanation as to how charCode works (specifically in Gecko, but some of it applies to other browsers too). You might find this interesting:

... when the currently selected keyboard layout produces a Unicode character (according to the current state of CapsLock and NumLock), the charCode property contains that character

半岛未凉 2024-12-09 05:11:18

您可以将传递给 dojo.connect() 的第 5 个 dontFix 参数设置为 true,这告诉它按原样传递,无需特殊处理。请参阅 https://github.com/dojo/dojo/blob/ master/_base/connect.js#L32

You can set the 5th dontFix argument passed to dojo.connect() to true, which tells it to let it pass thru as-is without the special handling. See https://github.com/dojo/dojo/blob/master/_base/connect.js#L32

他是夢罘是命 2024-12-09 05:11:18

使用 keypress 事件,其目的是为您提供有关用户键入的字符的信息。 (奇怪的是)IE 中需要 keyCode 属性,而其他浏览器中则需要 which 属性;这些给你输入的字符代码。

JavaScript 关键事件的权威页面:http://unixpapa.com/js/key.html

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    alert(String.fromCharCode(charCode));
};

Use the keypress event, whose purpose is to give you information about the character typed by the user. You'll need (bizarrely) the keyCode property in IE and the which property in other browsers; these give you the character code typed.

The definitive page for JavaScript key events: http://unixpapa.com/js/key.html

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    alert(String.fromCharCode(charCode));
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文