keypress and keyup – why is the keyCode different?

The events are for completely different purposes. Use keyup and keydown for identifying physical keys and keypress for identifying typed characters. The two are fundamentally different tasks with different events; don’t try to mix the two. In particular, keyCode on keypress events is usually redundant and shouldn’t be used (except in older IE, but see the linked document below for more on that); for printable keypresses it’s usually the same as which and charCode, although there is some variation between browsers.

Jan Wolter’s article on key events, already linked to in another answer, is the definitive word on this subject for me and has tables describing what each of the different properties returns for each type of key event and each browser.

Leave a Comment