Arrow key codes
The arrows send event.key "ArrowLeft", "ArrowUp", "ArrowRight" and "ArrowDown", and keyCode 37, 38, 39 and 40.
Every value at a glance
| Key | event.code | event.key | keyCode | event.location |
|---|---|---|---|---|
| Left Arrow | ArrowLeft | ArrowLeft | 37 | 0 (Standard) |
| Up Arrow | ArrowUp | ArrowUp | 38 | 0 (Standard) |
| Right Arrow | ArrowRight | ArrowRight | 39 | 0 (Standard) |
| Down Arrow | ArrowDown | ArrowDown | 40 | 0 (Standard) |
keyCode as Chrome and Edge send it on Windows. The Mac column (Chrome and Safari on a Mac) and the Firefox column show only numbers that differ; some keys also differ by system, as the text below says. event.which is always the same number as keyCode. event.key is for a US layout without Shift.
Press it here
Click here, then press an arrow key
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Left Arrow
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'ArrowLeft') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 37) {
// Your code here
}
});Which value to use
Check event.key. The arrows don’t change with the keyboard layout, so key and code have the same names: ArrowLeft, ArrowUp, ArrowRight and ArrowDown.
Use event.code when the number pad must not count. With Num Lock off, the 4, 8, 6 and 2 keys on the number pad also send key “ArrowLeft”, “ArrowUp”, “ArrowRight” and “ArrowDown” and keyCode 37 to 40. Only their code (Numpad4, Numpad8, Numpad6, Numpad2) and location 3 give them away.
The old numbers go round from left: 37 Left, 38 Up, 39 Right, 40 Down. Every browser still sends them, but they are deprecated.
What trips people up
The page scrolls when the player presses an arrow
Scrolling is the arrows’ default action. Call event.preventDefault() in keydown. Listen on the game itself (give it tabindex="0") rather than on window, so the rest of the page still scrolls and text fields still move the cursor:
game.addEventListener('keydown', (event) => {
if (event.key.startsWith('Arrow')) event.preventDefault();
});Movement stutters, or a direction gets stuck
A held key sends one keydown, then a pause, then repeats at the user’s repeat speed. Moving on each keydown stutters. Keep a set of held keys instead: add event.code on keydown, delete it on keyup, and move in your game loop. Empty the set on the window’s blur event: if the user switches windows while holding a key, its keyup never reaches your page.
Old code checks for “Left” or “Up”
Internet Explorer, Edge 16 and earlier, and Firefox 36 and earlier sent "Left", "Up", "Right" and "Down". Current browsers send the Arrow names. Check both only if you still support those browsers.
Your arrow handler fights a slider, list or text field
Many elements use the arrows themselves: text fields move the cursor, sliders change their value, radio buttons and <select> change the choice. Skip events whose event.target is one of these, or let them handle the key first and check event.defaultPrevented.
Mac and laptop keyboards
Mac laptops and many small keyboards have no Home, End, Page Up or Page Down keys. On a Mac, hold Fn with an arrow instead: Fn + Left sends Home, Fn + Right End, Fn + Up Page Up and Fn + Down Page Down. The page gets those keys, not an arrow.
On a Mac, the keyup of an arrow pressed while Cmd is held may not arrive, in any browser. Don’t rely on it for held-key state.