keydown, keypress, keyup

Test page.

Events overview page.

The keydown, keypress and keyup events fire when the user presses a key.

keydown
Fires when the user depresses a key. It repeats while the user keeps the key depressed.
keypress
Fires when an actual character is being inserted in, for instance, a text input. It repeats while the user keeps the key depressed. Not supported by Safari iPhone.
keyup
Fires when the user releases a key, after the default action of that key has been performed.

The keydown/keypress scheme outlined above is originally a Microsoft invention that Safari 3.1 copied for reasons explained by Alexey Proskuryakov of the Safari team.

The great advantage of this theory is that it clearly explains why we need keydown and keypress to be two separate events; without some sort of difference between the two events it's rather pointless to keep both of them around.

In contrast, Firefox and Opera just fire a bunch of key events without plan or purpose. There are historical reasons for this (everybody expects the keydown and keypress events to be available), but they aren't very strong because IE has always supported the scheme outlined above, and therefore web developers were forced to do the same.

Therefore I consider the IE/Safari theory to be the correct implementation, which Firefox and Opera will have to conform to. (The W3C spec is no help here; it doesn't even mention the keypress event.)

Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
On the window
No Yes Yes Minimal Yes

Are these events available on the window?

  • Opera allows registration on the window only when you use attachEvent.
On the document
Yes Yes Yes Yes Yes

Are these events available on the document?

On a form
Yes Yes Yes Yes Yes

Are these events available on a form?

On focusable elements
Yes Yes Yes Yes Yes

An important point of these events is monitoring user input in text inputs.

Key events on other focusable elements such as links and radio buttons and such don't make much sense to me, but all browsers support them.

Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
Repeats

When the user keeps a key depressed, both the keydown and (if it's a character key) the keypress event should fire continuously.

Yes Yes Yes Untestable Incorrect Incorrect
  • Opera and Konqueror only repeat the keypress event.
Special keys

When the user presses special keys such as the arrow keys, the browser should NOT fire keypress events.

Yes Too many Too many Yes Untestable Incorrect Incorrect
  • Firefox and Safari 3.0 also fire the keypress event.
  • Opera and Konqueror fire only the keypress event.
Event bubbling

The event should bubble.

Yes Yes Yes Yes Yes
  • When using attachEvent registration, Opera fires the event on the window before the event on the document. (The event isn’t available on the window in any other registration.)
Prevent default keydown
Yes Yes Yes No Yes

This should prevent the default of any key.

Prevent default keypress

Default of special keys should NOT be prevented

Yes Incorrect Incorrect Yes Untestable No Incorrect Yes

This should prevent the default of keys that lead to a character being inserted.

  • Firefox, Safari 3.0 and Opera allow you to prevent the default of the special keys onkeypress.
Prevent default keyup
“No” is the correct answer
No No No No No
Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7

iPhone

Safari iPhone doesn’t support keypress. Furthermore, it’s not possible to repeat a character by keeping a key depressed.

Related events

Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
textInput

Equivalent of keypress.

Test page.

No No Yes No No

This is a DOM event, despite the "DOM" bit missing in the name. Therefore it does not work with traditional event registration.