javascript 可以忽略 keyCode = 229 的 keydown 事件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25043934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:47:28  来源:igfitidea点击:

Is it OK to ignore keydown events with keyCode = 229?

javascripteventsdartkeyboard-eventsdartium

提问by silviubogan

At the beginning I wanted to monitor changes to a <input type="text">in real time (for example, exactly when the user presses a key). The onChangeevent did not work because it is triggered only when the user presses Enter or removes focus from the input element. Then I saw this questionon StackOverflow. I tried the code from that answer but the problem is that I do not want to be notified for key presses that do not represent printable characters, so I had to modify it in this way to make it verify that there are printable characters in the events:

一开始我想<input type="text">实时监控 a 的变化(例如,当用户按下某个键时)。该onChange事件不起作用,因为它仅在用户按下 Enter 键或从 input 元素中移除焦点时触发。然后我在 StackOverflow 上看到了这个问题。我尝试了那个答案中的代码,但问题是我不想收到不代表可打印字符的按键的通知,所以我不得不以这种方式修改它以验证事件中是否有可打印字符:

...

textInputElement.onKeyDown.listen((KeyboardEvent ev) {
  if (new String.fromCharCode(ev.keyCode).length > 0) {
    callAFunction();
  }
});

...

(+ the same change for the onKeyUpevent)

(+onKeyUp事件的相同更改)

When I tested this in Dartium I saw that by focusing the input element and then pressing any key, a keydownevent is triggered with ev.keyCode = ev.which = 229and ev.charCode = 0. Immediately after this event, another keydownevent is triggered with the correct ev.keyCode = ev.whichof the pressed key and ev.charCode = 0. I did not understand where this 229 key was coming from but I saw that it is a printable character, ?. I searched the Internet and I have found that others have this issue, and sometimes they are using other programming languages and technologies. One relevant link is thisand the chosen fix is in this very small commit- they chose to ignore all events which have keyCode = 229with the explanation that recent versions of Chrome/Chromium/WebKit started to send these keydown events before every standard keyboard events, and that their meaning is that the user pressed some button. but input method is still processing thator input method editor is processing key input.

当我在 Dartium 中对此进行测试时,我看到通过聚焦输入元素然后按任意键,keydown会使用ev.keyCode = ev.which = 229和触发一个事件ev.charCode = 0。在此事件之后,会立即keydown触发另一个事件ev.keyCode = ev.which,同时按下正确的键 和ev.charCode = 0。我不明白这个 229 键是从哪里来的,但我看到它是一个可打印的字符?. 我在网上搜了一下,发现其他人也有这个问题,有时他们使用的是其他编程语言和技术。一个相关的链接是这个,选择的修复是在这个非常小的提交中- 他们选择忽略所有事件keyCode = 229解释说最近版本的 Chrome/Chromium/WebKit 开始在每个标准键盘事件之前发送这些 keydown 事件,它们的意思是用户按下了某个按钮。但输入法仍在处理那个输入法编辑器正在处理键输入

My question is, is it OK to ignore keydownevents with keyCode = 229if new String.fromCharCode(229)returns the printable character "?"? I thought about the possible situation of having a real key that produces the same key code and its corresponding character.

我的问题是,如果返回可打印字符“ ”,可以忽略keydown事件吗?我想过有一个真正的钥匙产生相同的钥匙代码及其相应字符的可能情况。keyCode = 229new String.fromCharCode(229)?

I thank you for any kind of help!

我感谢您的任何帮助!

回答by James Newton

The short answer is No. You can ignore keydown events with keyCode = 229, but only if they follow immediately after a keypress event.

简短的回答是否定的。您可以忽略 keyCode = 229 的 keydown 事件,但前提是它们紧跟在 keypress 事件之后。

If you hold certain keys down, some browsers send a repeated keydown event with a keyCode value of 229, while others send the original keydown keyCode again. Some browsers send 0 as the keyCode associated with the keypress event, and place the character code in a charCode property.

如果您按住某些键,某些浏览器会发送重复的 keydown 事件,keyCode 值为 229,而其他浏览器会再次发送原始 keydown keyCode。一些浏览器发送 0 作为与 keypress 事件关联的 keyCode,并将字符代码放在 charCode 属性中。

In all cases, as far as I can tell from my tests, the order of events is always predictable:

在所有情况下,据我从测试中可以看出,事件的顺序始终是可预测的:

keydown  (event.keyCode  = key-keyCode            ex: 65 = "A")
keypress (event.keyCode  = 0 | character-keyCode  ex: 97 = "a")  - conflated model
          event.charCode =     character-keyCode                 - split model
keydown  (event.keyCode  = 229 | key-keyCode      ex: 229 | 65)  - may be repeated
keyup    (event.keyCode  = key-keyCode            ex: 65)

The letter ?is used in a number of Scandinavian languages. Here are the events received in Safari 6.1.5 when the keyboard is set to Swedish or Finnish, and the ? character (to the left of the P key) is pressed:

信?用于多种斯堪的纳维亚语言。以下是当键盘设置为瑞典语或芬兰语时 Safari 6.1.5 中接收到的事件,以及 ? 字符(在 P 键的左侧)被按下:

EVENT    keyCode
keydown  219     ("[" key position)
keypress 229     (?)
keydown  229     (repeatedly, indicating that the Input Monitor is busy)
keyup    219

Notice that the initial keydown keyCode is 219, not 229.

请注意,初始 keydown keyCode 是 219,而不是 229。

To generate a 229 keyCode on the initial keydown event, you can press any "dead key". On the Swedish keyboard on Mac, for example, the key immediately to the left of of the BACKSPACE key is ′ (acute accent), as used in words like déjà-vu. When you press a dead key, the character appears in the input field, but the insertion point does not move. When you subsequently type a character that can be combined with it, the browser may replace the initial dead key character with a composite character (′ + e = é) which has its own Unicode value.

要在初始 keydown 事件上生成 229 keyCode,您可以按任意“死键”。例如,在 Mac 上的瑞典语键盘上,BACKSPACE 键左侧的键是 ′(重音符号),如 déjà-vu 之类的词所用。当您按下死键时,字符会出现在输入字段中,但插入点不会移动。当您随后键入可以与其组合的字符时,浏览器可能会将初始死键字符替换为具有自己的 Unicode 值的复合字符 (′ + e = é)。

Here are the events that you will see in Safari 6.1.5 when the user presses and releases ′ followed by e on a Swedish keyboard:

以下是在 Safari 6.1.5 中当用户在瑞典语键盘上按下并释放 ' 后跟 e 时您将看到的事件:

EVENT    keyCode
keydown  229 (dead key)
keyup    187 (acute accent)
keydown  229 (second key is being treated)
keyup     69 ("E")

Note that there are no keypress events sent at all, because there is no "é" key as such which has been pressed. If you want to determine which character the user entered, you can wait until after the second keyup, then read the character from the input field.

请注意,根本没有发送按键事件,因为没有按下“é”键。如果要确定用户输入的是哪个字符,可以等到第二次按键之后,然后从输入字段中读取该字符。

In other words, you can ignore any keydown events with a 229 keyCode aftera keypress event, but if you ignore all 229 keyCodes, you may prevent users from adding various diacritical characters.

换句话说,您可以按键事件之后忽略任何带有 229 keyCode 的 keydown事件,但是如果您忽略所有 229 keyCode,您可能会阻止用户添加各种变音字符。

For more information on keyCode 229, from the w3.org site: keyCode property of key events

有关 keyCode 229 的更多信息,请访问 w3.org 站点:keyCode property of key events