Search code examples
javascriptbrowserkeypressoperadom-events

KeyPress malfunction in Opera


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).


Solution

  • 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.