About keyboard key code finder
When you handle keyboard events in JavaScript, you need to know which property identifies a key. Press any key here to see them all.
The properties
- key: the character or name produced, such as 'a' or 'Enter'. Best for most uses.
- code: the physical key position, such as 'KeyA', which does not change with the keyboard layout.
- keyCode: an older numeric value that is deprecated but still seen in old code.
- location: whether the key is the left or right one, or on the numpad.
Which to use
Use event.key for typed characters and shortcuts, and event.code for game controls where position matters.
A worked example
Pressing the letter A shows key 'a', code 'KeyA' and the old keyCode 65. Pressing Shift and A gives key 'A' but the same code 'KeyA'. Pressing Enter on the main keyboard gives code 'Enter', while the numpad Enter gives 'NumpadEnter' and location 3.
Tips for handlers
- Use key for shortcuts that depend on what is typed, such as Ctrl+S.
- Use code for game controls, where the physical position matters on any keyboard layout.
- Avoid keyCode in new code since it is deprecated.
