Press any key to instantly see its keyCode, key, code, which, and location values. Full reference table and code snippets included.
| Key | keyCode | event.key | event.code |
|---|
JavaScript provides three keyboard events: keydown, keyup, and the deprecated keypress. Each fires with a KeyboardEvent object containing information about the pressed key.
| Property | Type | Example (pressing "a") | Layout-dependent? |
|---|---|---|---|
event.key | String | "a" | Yes |
event.code | String | "KeyA" | No |
event.keyCode | Number | 65 | Partially |
event.key for character input (text fields, search)event.code for keyboard shortcuts and games (layout-independent)event.keyCode and event.which in new codekeydown for shortcuts, not keypressevent.preventDefault() to stop default browser behaviordocument.addEventListener('keydown', (e) => {
// Ctrl+S to save
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
save();
}
// Escape to close
if (e.key === 'Escape') {
closeModal();
}
});
0 — General (most keys)1 — Left (left Shift, left Ctrl, left Alt)2 — Right (right Shift, right Ctrl, right Alt)3 — Numpad