使用keyCode = 229忽略keydown事件可以吗? [英] Is it OK to ignore keydown events with keyCode = 229?

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

问题描述

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

  ... 

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

...

(+与<$ c相同的更改$ c> onKeyUp 事件)



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



我的问题是,是否可以忽略 keydown 事件与 keyCode = 229 如果 new String.fromCharCode(229)返回可打印字符å?我想到可能的情况是有一个真正的密钥生成相同的密钥代码和相应的字符。



我感谢你的任何帮助!

解决方案

简短的答案是否。你可以忽略keyCode = 229的keydown事件,但只有在按键事件之后立即关闭。 p>

如果您按住某些键,某些浏览器会发送一个keyCode值为229的重复的keydown事件,而其他浏览器再次发送原始的keydown keyCode。有些浏览器发送0作为与键盘事件关联的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例如:229 | 65) - 可以重复
keyup(event.keyCode = key-keyCode ex:65)

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

  EVENT keyCode 
keydown 219([key position)
keypress 229(å)
keydown 229(重复,表示输入监视器正忙)
keyup 219

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

要在初始的keydown事件中生成一个229 keyCode,可以按任何死锁。例如,在Mac上的瑞典键盘上,BACKSPACE键左侧的键是'(锐音)),就像déjà-vu一样使用。当您按死钥匙时,字符出现在输入字段中,但插入点不移动。当您随后键入可以与之组合的字符时,浏览器可能会使用具有自己的Unicode值的复合字符('+ e =é)替换初始死锁字符。



以下是Safari 6.1.5中用户在瑞典语键盘上按下并释放e的事件。

事件keyCode 
keydown 229(死钥)
keyup 187(锐音)
keydown 229(第二个键被处理)
keyup 69( E)

请注意,根本没有发送按键事件,因为没有é这是按键的。如果您想确定用户输入的字符,可以等到第二次按键后再从输入栏读取字符。



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



有关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天全站免登陆