Here’s something that came up at Jot last week. Which event will be fired continuously, if you hold down a key: onkeydown or onkeypress?

The answer is that it depends on both platform and browser. In general, Windows-based browsers fire only onkeydown, except for Firefox/Win which also fires onkeypress. On the Mac and on Linux, only onkeypress is fired, and onkeydown is not fired on the Firefox browser for these platforms. Something to keep in mind when you bind the “iterate over items while holding arrow up or down key” behaviour to your application. See the testcase.

5 responses

  1. And it depends on the key. IE fires onkeypress repeatedly as well for keys that produce characters.

  2. Looking at your testcase with Firefox 1.5.0.4 in WinXP SP2, I get both onkeydown and onkeypress for:

    • all character keys including Tab, Enter, Esc, and Space
    • Del
    • Ins
    • Backspace
    • PageUp
    • PageDown
    • the four cursor keys
    • the Fx keys
    • Break

    I get onkeydown but not onkeypress for:

    • Shift
    • Ctrl
    • Alt
    • cAPS lOCK – ah heck, Caps Lock got me
    • Alt Gr
    • the context menu key (to the right of the spacebar)

    I get neither event for PrtScr and the Windows key.

  3. Mark Wubben says:

    Sjoerd, indeed. It gets stranger and stranger still.

    Nick, it would seem like Firefox/Win tries to emulate IE behaviour, but fails for certain keys. On the Mac, no modifier keys fire events.

  4. Paul Sowden says:

    I would guess it’s more likely that FF/win behaves differently due to the key events being different in Windows vs. OS X than trying to be different on the two platforms.

  5. Mark Wubben says:

    Yes, but it is odd that Firefox/Win does not simulate any other browser on that platform, while Firefox/Mac behaves like all other browsers.