javascript jQuery keypress 事件在按住键时重复触发 - 但不是在所有键上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9098168/
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-26 05:35:08  来源:igfitidea点击:

jQuery keypress event fires repeatedly when key is held - but not on all keys

javascriptjquerykeypress

提问by totallyNotLizards

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.

这可能是预期的行为,或者至少不是 jQuery/js 问题,但我想澄清一下是否有任何问题。

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/

您还可以使用 jQuery 文档中的示例: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.

当按下左或右光标(或许多其他键,例如A,E,[,等)时,事件会触发,您会在控制台中收到一条不错的日志消息。一切都很好,正如预期的那样。但是,现在尝试按住键 - 短暂暂停后,您会看到当您按住键时 keydown 事件会触发多次,但是如果您尝试点击 a p(或者,例如, a j),它只会触发一次。

I'm testing this using FF 9.0.1 and mac OSX 10.7.1 and jQuery 1.7.1.

我正在使用 FF 9.0.1 和 mac OSX 10.7.1 以及 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.

就用例而言,实际上并没有 - 这只是在我将动画绑定到光标按下并在按下键时开始看到奇怪的行为时突然出现的。我的解决方法是改用该keyup()事件,并preventDefault()keydown()我感兴趣的键的事件上停止光标滚动屏幕。

UPDATE: Seems that on the keypress event, keyCode is always 0for 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/

更新:似乎在 keypress 事件中,keyCode 总是0用于大多数字母,这可能与我认为处理程序只触发一次的原因有关。经过更多测试后,我看到重复的日志条目,例如游标。如果您检查 jQuery API 页面,并在该页面上使用演示,它会显示出我所描述的行为:http: //api.jquery.com/keypress/

Still can't explain that myself :/

我自己仍然无法解释:/

采纳答案by Tim Down

Behaviour varies between browsers and operating systems. The following page covers the topic of auto-repeated key events in detail in section 2.2:

行为因浏览器和操作系统而异。下一页详细介绍了第 2.2 节中的自动重复键事件的主题:

http://unixpapa.com/js/key.html

http://unixpapa.com/js/key.html

回答by Daniel Lee

This problem has been around for ages, but i think i have discovered a fix just now. This seems to work fine

这个问题已经存在很长时间了,但我想我现在已经找到了解决方法。这似乎工作正常

$("#search_query").keyup(function(event) {

    //this will void fake keypresses
    if(event.charCode == 0 && event.keyCode == 0) {
        return false;
    }

    //place the rest of you code here
});

回答by Rebecca

Also - for the benefit of all people who view this post: try the "keydown" method

另外 - 为了所有查看这篇文章的人的利益:尝试“keydown”方法

Keypress will get fired multiple times... keydown won't

Keypress 会被多次触发... keydown 不会

http://jquerymobile.com/demos/1.0a2/experiments/api-viewer/docs/keypress/index.html

http://jquerymobile.com/demos/1.0a2/experiments/api-viewer/docs/keypress/index.html