---
title: "Space Key Code: keyCode 32, event.key \" \", code \"Space\""
description: "Space bar in JavaScript sends event.key \" \" (a space, not \"Space\"), event.code \"Space\" and keyCode 32. Press it to check, and stop it scrolling the page."
url: https://devicetests.com/keycode/space
language: en
---

# Space key code

The space bar sends event.key " " (one space), event.code "Space" and keyCode 32.

## Every value at a glance

| Key | event.code | event.key | keyCode | event.location |
| - | - | - | - | - |
| Space | `Space` | `" "` | 32 | 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 Space

Recommended: match event.key

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

Match the key's place: event.code

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

Old code that still uses keyCode (deprecated)

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

## Which value to use

Check `event.key === ' '`, a string with one space in it. `event.key === 'Space'` is never true: `key` holds what the key types, and the space bar types a space.

`event.code === 'Space'` names the key itself. Use it for games and shortcuts, where the key’s place matters. It’s the same on every layout.

`keyCode` is 32 in every browser, the same as the space character’s code, but it is deprecated. Internet Explorer and Firefox 36 and earlier sent `"Spacebar"` as `key`; accept it only if you still support them.

## What trips people up

### Pressing Space scrolls the page

Space scrolls down one screen and Shift + Space scrolls up, when nothing focused uses it. Stop it in keydown, where scrolling starts. Leave text fields alone, or nobody can type a space:

```
if (event.code === 'Space' && !event.target.closest('input, textarea, [contenteditable]')) {
  event.preventDefault();
}
```

### Space clicks the button that still has focus

After someone clicks a button, it keeps focus, and Space presses it again. A game that jumps on Space then also restarts or pauses. Move focus away (`button.blur()`) or call `preventDefault()` on Space while the game runs.

### The button fires on keyup, not keydown

Buttons react to Enter as soon as it goes down, but to Space only when it comes back up. Holding Space doesn’t repeat the click. If your own `role="button"` element handles keys, copy this: Enter on keydown, Space on keyup.

### Space picks a word in Japanese or Chinese typing

With an input method, Space converts the typed sounds into characters. Your handler sees it with `isComposing` true or keyCode 229. Skip those:

```
if (event.isComposing || event.keyCode === 229) return;
```

### The handler sees `key` “ “ but the text shows nothing

That’s normal: the value really is a single space, so it looks empty when you print it. Log `JSON.stringify(event.key)` to see `" "`.

## Space on phones

Android keyboards often send keyCode 229 and key `"Unidentified"` for the space bar, like most keys. To react to a typed space in a field, read `event.data === ' '` from the `input` event instead.

## Related keys

- [Enter key code](https://devicetests.com/keycode/enter)
- [Tab key code](https://devicetests.com/keycode/tab)
- [Letter key codes (A–Z)](https://devicetests.com/keycode/letter-keys)
- [Arrow key codes](https://devicetests.com/keycode/arrow-keys)
- [keyCode 229 and "Unidentified" keys](https://devicetests.com/keycode/229)
- [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)
