Novemberborn, Straight lines circle sometime

Leaky Abstractions in DOM Programming

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.

link | javascript | 12 July 2006, 06:45


Comments

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

    Sjoerd Visscher | 12 July 2006, 09:44 | link

  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.

    Nick Fitzsimons | 12 July 2006, 21:36 | link

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

    Mark Wubben | 13 July 2006, 00:34 | link

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

    Paul Sowden | 18 July 2006, 00:11 | link

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

    Mark Wubben | 18 July 2006, 00:26 | link

Leave your comment

Please keep it polite and on topic. Yes, your e-mail address is required, but it's kept private. HTML is not allowed in the comments but you can use Markdown. Non-contributing comments run the risk of being removed. Especially if the website seem “fishy”. Spammers, beware.

(required)
(required, kept private)
(optional, but let's share it!)
(required)

Remember my details


Novemberborn: Extra

About the author

Mark Wubben is a hacker/writer in Enschede, the Netherlands.

Read more about Mark...

Go to

Jobs (NL)

Xopus zoekt programmeurs! Verbeter de code en win!

Subscribe