---
title: "Arrow Key Codes: keyCode 37, 38, 39, 40 and event.key"
description: "Arrow keys in JavaScript: event.key \"ArrowLeft\", \"ArrowUp\", \"ArrowRight\", \"ArrowDown\", keyCode 37–40. Stop scrolling, handle held keys and numpad arrows."
url: https://devicetests.com/keycode/arrow-keys
language: en
---

# 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.

## Code to react to Left Arrow

Recommended: match event.key

```
document.addEventListener('keydown', (event) => {
  if (event.key === 'ArrowLeft') {
    // Your code here
  }
});
```

Match the key's place: event.code

```
document.addEventListener('keydown', (event) => {
  if (event.code === 'ArrowLeft') {
    // Your code here
  }
});
```

Old code that still uses keyCode (deprecated)

```
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.

## Related keys

- [Home, End, Page Up and Page Down key codes](https://devicetests.com/keycode/home-end-page-up-page-down)
- [Numpad key codes](https://devicetests.com/keycode/numpad)
- [Space key code](https://devicetests.com/keycode/space)
- [Letter key codes (A–Z)](https://devicetests.com/keycode/letter-keys)
- [Meta key code (Windows key and Command)](https://devicetests.com/keycode/meta)
- [Keycode finder: press any key to see its codes](https://devicetests.com/keycode)
- [All key codes in one table](https://devicetests.com/keycode#key-codes)
