当key持有时,jQuery keypress事件会反复触发 - 但不会在所有键上触发 [英] jQuery keypress event fires repeatedly when key is held - but not on all keys
问题描述
这可能是预期的行为,或者至少不是jQuery / js问题,但是如果有任何问题我想澄清一下。
采取以下措施:
$(document).bind('keypress',function(e){
switch(e.keyCode)
{
案例37:
console.log('左游标keydown,将暂停');
break;
case 39:
console。 log('右光标keydown,将暂停');
break;
case 80:
console.log(每次按下'p只会触发一次!');
break;
}
});
您还可以在jQuery的文档中使用示例: http://api.jquery.com/keypress/
按下左或右光标时(或许多其他键,如 A,E,[,
等),事件触发,你在控制台中得到一个很好的日志消息。一切都很好如预期。但是,现在尝试按住键 - 在短暂停顿后,您将看到按住键时keydown事件会多次触发,但是如果您尝试按 p
(或者,例如, j
),它只会触发一次。
我正在使用FF 9.0.1和mac OSX 10.7.1以及jQuery 1.7.1对此进行测试。
<这是设计,是依赖于浏览器的功能,还是与操作系统,甚至键盘本身有关?还有人有一个会重复的键列表和不会的键吗?
就用例而言,没有一个真的 - 这只是裁剪当我将动画绑定到光标按下并在按下按键时开始看到奇怪的行为时。我的解决方法是改为使用 keyup()
事件,并在上使用
我感兴趣的键的事件,停止游标滚动屏幕。 preventDefault()
keydown()
更新:
似乎是在keypress事件中,对于大多数字母,keyCode总是 0
,这可能与我认为处理程序只触发一次的原因有关。经过一些测试后,我看到了游标的重复日志条目。如果你检查jQuery API页面,并使用它上面的演示,它展示了我描述的行为: http://api.jquery .com / keypress /
仍然无法自己解释:/
浏览器和操作系统之间的行为有所不同。以下页面详细介绍了2.2节中自动重复键事件的主题:
http://unixpapa.com/js/key.html
This is probably intended behaviour or atleast not a jQuery / js issue but I'd like some clarification if there's any to be had.
Take the following:
$(document).bind('keypress', function(e){
switch(e.keyCode)
{
case 37:
console.log('left cursor keydown, will fire on hold');
break;
case 39:
console.log('right cursor keydown, will fire on hold');
break;
case 80:
console.log('p will only fire once per press!');
break;
}
});
You can also play with the example at jQuery's docs: http://api.jquery.com/keypress/
When a left or right cursor is pressed (or many other keys such as A,E,[,
etc), the event fires and you get a nice log message in the console. All fine & as intended. However, now try holding the key down - after a brief pause you will see that the keydown event fires multiple times when you hold the key, however if you try hitting a p
(or, for instance, a j
), it will only fire once.
I'm testing this using FF 9.0.1 and mac OSX 10.7.1 and jQuery 1.7.1.
Is this by design, is it a browser-dependant feature, or is it to do with the OS, or even the keyboard itself? Also has anyone got a list of keys that will repeat and keys that will not?
As far as the use case goes, there isn't one really - this just cropped up when I was binding an animation to a cursor press and started seeing wierd behaviour when the key was pressed. My workaround was to use the keyup()
event instead, and preventDefault()
on the keydown()
event for the keys I was insterested in, to stop the cursors scrolling the screen.
UPDATE:
Seems that on the keypress event, keyCode is always 0
for most letters, which might have something to do with why I thought the handler only fired once. After some more testing I see the repeated log entries like for cursors. If you check the jQuery API page though, and use the demo on that, it exhibits the behaviour I was describing: http://api.jquery.com/keypress/
Still can't explain that myself :/
Behaviour varies between browsers and operating systems. The following page covers the topic of auto-repeated key events in detail in section 2.2:
http://unixpapa.com/js/key.html
这篇关于当key持有时,jQuery keypress事件会反复触发 - 但不会在所有键上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!