KeyPress malfunction in Opera

天大地大妈咪最大 提交于 2019-12-11 16:13:42

问题


I'm using the following code to detect users' key pressing, in JavaScript:

$(document).bind('keydown', function (event) {
    'use strict';
    var keyCode = event.keyCode;

        switch (keyCode) {
        case '{N}':
             doSomething();
             break;

        default:
             break;
        }
});

Where doSomething is a previously defined function and {N} is any of the JavaScript Char Codes.

It works properly in every major browser, but in Opera even if a key remains pressed, it only calls doSomething once, instead of doing it until the key is released. What can I do to fix this?


Edit

I solved it using the keypress event instead of keydown (which is not well handled by Opera).


回答1:


this is a known bug which should (finally!) get fixed soon. In short, keydown events are not repeated while keypress events are. Listening to keypress instead if you want repetition (and don't care about keys that do NOT fire keypress in all browsers like most function keys) should be a reasonable cross-browser solution.




回答2:


Opera makes a mess, the keydown event does not repeat, and you cannot prevent the default for keydown in opera. For more http://quirksmode.org/dom/events/



来源:https://stackoverflow.com/questions/9200589/keypress-malfunction-in-opera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!