Search code examples
javascriptfirefoxdom-eventsarrow-keys

How can I get auto-repeated keydown events in Firefox when arrow keys are held down?


I've got several editable divs. I want to jump through them by pressing arrow keys (38 and 40).

Firefox 3 on Mac OS and Linux won't repeat the events on holding the key. Obviously only keypress events are supported for repetition. As the keys 38 and 40 are only supported on keydown I'm kind of stuck.


Solution

  • You can use keypress and check the e.keyCode == 38,40 instead of e.which or e.charCode This is consistent across Mac and Win.

    $('#test').bind($.browser.mozilla ? 'keypress' : 'keyup', function(e) {
        if ( (e.which || e.keyCode) == 40 ) { /* doSometing() */ }
    });
    

    See JavaScript Madness: Keyboard Events (3.2. Values Returned on Character Events) and event.keyCode on MDC.