JavaScript and jQuery Key Code Checker

Home Refresh Related Blog Post
This form lets you enter keystrokes or shift/ctr/alt+key combinations and displays the relevant key codes for each of the events. For best results test in all your target browsers to find out which events fire and how values are returned. This example uses jQuery's event handling features to normalize the event values.
Key Code



which (jquery)

Note: IE 8 and earlier doesn't support which natively


Event Object Support

The examples here use jQuery to normalize the keycodes somewhat, but even with jQuery normalization is not smooth.

IE8 and prior only supports keyCode

IE 8 and earlier doesn't support the doesn't support the charCode and which properties. IE 9 and later implements DOM event object and has keyCode, charCode and which.

keypress Event only fires on 'printable' characters

The keypress event only fires when a key is pressed that results in some content being placed into the input field. If you press navigation keys (arrows, home, back, tab etc.) keypress does not fire. Also, if you cancel event bubbling or return false in keydown, keypress (or keyup) will not fire.

FireFox and Opera handle keypress differently than Spec

FireFox and Opera handle keypress events differently than the spec suggests, by *always* firing the keypress event when any key is pressed. All other browsers fire keypress only when a printable character is pressed which is per W3C spec.

Only keypress returns 'translated' keyCode Values that account for shifted Keys

keypress is the only event which returns a valid keyCode that can be used with String.fromCharCode() to return the true printed character including lower case and shifted key characters (like ! or #). keyup and keydown only return the base key that was pressed plus shift codes. You can use String.fromCharCode(e.which) to extract this translated value. Realize this translation occurs only in keypress - keyup/keydown only receive the raw character codes plus shift states (ie. you get A or 1 regardless of whether you press the shift key or not).