是否可以忽略keyCode = 229的keydown事件? [英] Is it OK to ignore keydown events with keyCode = 229?

查看:3279
本文介绍了是否可以忽略keyCode = 229的keydown事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始时我想实时监视对< input type =text> 的更改(例如,当用户按下一个键)。 onChange 事件无效,因为它只有在用户按Enter键或从输入元素中删除焦点时才会触发。然后,我在StackOverflow上看到了此问题。我试过从那个答案的代码,但问题是,我不想被通知的按键,不表示可打印字符,所以我不得不修改它,以这种方式,使它验证在事件中有可打印的字符:

  ... 

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

...

$ c> onKeyUp event)



当我在Dartium中测试这个时,我发现通过集中输入元素然后按任意键, code> ev.keyCode = ev.which = 229 和 ev.charCode = 触发code> 0 。在该事件之后,紧接着按下的键的正确 ev.keyCode = ev.which 触发另一键下 ev.charCode = 0 。我不明白这个229键来自哪里,但我看到它是一个可打印的字符,å。我在互联网上搜索,我发现其他人有这个问题,有时他们正在使用其他编程语言和技术。一个相关链接是,所选修订位于这个非常小的提交 - 他们选择忽略所有具有 keyCode = 229 的事件,并说明最新版本的Chrome / Chromium / WebKit在每个标准键盘事件之前开始发送这些keydown事件,并且它们的含义是用户按下了一些按钮。但输入法仍在处理或输入法编辑器正在处理键输入



我的问题是,忽略 keydown keyCode = 229 if new String.fromCharCode(229)返回可打印字符å?我想到了一个真正的键产生相同的键代码及其相应的字符的可能情况。



我感谢你的任何帮助!

解决方案

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

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



在所有情况下,测试,事件的顺序总是可以预测的:

  keydown(event.keyCode = key-keyCode ex:65 =A )
keypress(event.keyCode = 0 | character-keyCode ex:97 =a) - conflated模型
event.charCode = character-keyCode - 拆分模型
keydown(event.keyCode = 229 | key-keyCode ex:229 | 65) - 可能重复
keyup(event.keyCode = key-keyCode ex:65)

字母å用于一些斯堪的纳维亚语言。以下是当键盘设置为瑞典语或芬兰语且按下å字符(P键左侧)时在Safari 6.1.5中收到的事件:

  EVENT keyCode 
键下降219([键位置)
键按下229(å)
键重置229(重复,表示输入监视器正忙)
keyup 219

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

要在初始keydown事件中生成229 keyCode,可以按任意dead key。例如,在Mac上的瑞典键盘上,紧接在BACKSPACE键左侧的键是(尖锐的口音),如déjà-vu这样的词。当按下死点时,字符会出现在输入字段中,但插入点不会移动。随后键入可与其组合的字符时,浏览器可能会使用具有自己的Unicode值的复合字符('+ e =é)替换初始死角字符。



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

  EVENT keyCode 
keydown 229(dead key)
keyup 187(急性口音)
keydown 229(正在处理第二个键)
keyup 69 E)

注意,根本没有发送按键事件,因为没有é键这样被按下。如果您想确定用户输入的字符,您可以等到第二个键后面的字符,然后从输入字段中读取字符。



换句话说,在按键事件后忽略任何按键事件,但如果忽略所有229个键码,您可能会阻止用户添加各种变音字符。



有关keyCode 229的详细信息,请参阅w3.org网站:键事件的keyCode属性


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 onChange event did not work because it is triggered only when the user presses Enter or removes focus from the input element. Then I saw this question on 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:

...

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

...

(+ the same change for the onKeyUp event)

When I tested this in Dartium I saw that by focusing the input element and then pressing any key, a keydown event is triggered with ev.keyCode = ev.which = 229 and ev.charCode = 0. Immediately after this event, another keydown event is triggered with the correct ev.keyCode = ev.which of 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 this and the chosen fix is in this very small commit - they chose to ignore all events which have keyCode = 229 with 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 that or input method editor is processing key input.

My question is, is it OK to ignore keydown events with keyCode = 229 if 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.

I thank you for any kind of help!

解决方案

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

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.

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:

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.

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.

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:

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 after a keypress event, but if you ignore all 229 keyCodes, you may prevent users from adding various diacritical characters.

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

这篇关于是否可以忽略keyCode = 229的keydown事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆