独立于操作系统的键盘事件到字符的映射
显然,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
event.charCode
包含按下键盘时生成的字符,而不是按下的实际按键。https://developer.mozilla.org/en/DOM/event.charCode#Notes
编辑: 另请参阅 https://developer.mozilla.org/en/Gecko_Keypress_Event,它包含关于 charCode 如何工作的更深入的解释(特别是在 Gecko 中,但其中一些也适用于其他浏览器)。您可能会发现这很有趣:
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
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:
您可以将传递给
dojo.connect()
的第 5 个dontFix
参数设置为 true,这告诉它按原样传递,无需特殊处理。请参阅 https://github.com/dojo/dojo/blob/ master/_base/connect.js#L32You can set the 5th
dontFix
argument passed todojo.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使用
keypress
事件,其目的是为您提供有关用户键入的字符的信息。 (奇怪的是)IE 中需要keyCode
属性,而其他浏览器中则需要which
属性;这些给你输入的字符代码。JavaScript 关键事件的权威页面:http://unixpapa.com/js/key.html
Use the
keypress
event, whose purpose is to give you information about the character typed by the user. You'll need (bizarrely) thekeyCode
property in IE and thewhich
property in other browsers; these give you the character code typed.The definitive page for JavaScript key events: http://unixpapa.com/js/key.html