Keyboard Event Properties

JavaScript FAQ | Keyboard & Mouse Events FAQ  

Question: How do I analyze keyboard events in JavaScript?

Answer: When a user types a letter on the keyboard, this usually triggers the keyboard events*

keydown
,
keypress
, and
keyup
(in this order). To analyze keyboard events, your JavaScript code may use event-handler functions, which can access the event properties. Some of these properties are the same across browsers (e.g.
event.type
). Other properties may be platform-dependent (e.g.
event.which
is available in Firefox, Opera, Safari, Chrome, but not in Internet Explorer). Arguably, the most useful properties are
event.which
,
event.keyCode
, and
event.charCode
, because these properties allow your script to answer the questions: