---
title: "Escape Key Code: keyCode 27, event.key \"Escape\" (JavaScript)"
description: "Esc in JavaScript sends event.key \"Escape\", event.code \"Escape\" and keyCode 27. Press it to check, and see why fullscreen and dialogs take it first."
url: https://devicetests.com/keycode/escape
language: en
---

# Escape key code

Esc sends event.key "Escape", event.code "Escape" and keyCode 27.

## Every value at a glance

| Key | event.code | event.key | keyCode | event.location |
| - | - | - | - | - |
| Escape | `Escape` | `Escape` | 27 | 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 Escape

Recommended: match event.key

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

Match the key's place: event.code

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

Old code that still uses keyCode (deprecated)

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

## Which value to use

Check `event.key === 'Escape'`. There is only one Esc key, and it sends the same value on every layout, so `event.key` and `event.code` say the same thing here. `key` reads better.

`keyCode` 27 still arrives in every browser, but it is deprecated.

Very old browsers sent `"Esc"` instead: Internet Explorer and Firefox 36 and earlier. Accept both only if you still support them.

## What trips people up

### Esc leaves fullscreen and my handler can’t stop it

In fullscreen (and pointer lock), Esc belongs to the browser. It always exits, and `preventDefault()` doesn’t help; your handler may not even see that Esc. Listen for `fullscreenchange` to know when it happened. In Chromium, a game can call `navigator.keyboard.lock(['Escape'])` while in fullscreen; the user then holds Esc to exit.

### The dialog closes even though I cancelled it

A modal `<dialog>` closes on Esc by itself. It first fires a `cancel` event, which you can stop:

```
dialog.addEventListener('cancel', (event) => event.preventDefault());
```

Chrome lets you stop it only once: press Esc again without clicking anything in between, and it closes anyway. To keep a dialog open on purpose, set `closedby="none"` where it’s supported, and save the user’s work in the `close` event rather than relying on `cancel`.

### Esc closes two things at once

When a menu is open inside a dialog, both hear the same Esc. Close only the innermost thing: handle Esc on the menu, then call `event.stopPropagation()` so your dialog code doesn’t run, and `event.preventDefault()` so a native `<dialog>` doesn’t close too: closing it is the browser’s default action, not a handler.

### Esc in a search field clears it

In Chrome and some other browsers, Esc empties an `<input type="search">`. If Esc should close your search box instead, call `preventDefault()` in keydown, or clear and close in one step.

### There is no Esc key on an iPad keyboard

Some iPad keyboards have no Esc key, and phones never have one. Always give a visible close button too. Esc is a shortcut, not the only way out.

## Esc as the way out

Keyboard users expect Esc to close menus, popups and dialogs, and to put focus back on the button that opened them. If a widget catches every key, like a code editor or the box on the keycode finder, let Esc release it, so Tab moves on again. That keeps keyboard users from getting stuck.

## Related keys

- [Enter key code](https://devicetests.com/keycode/enter)
- [Tab key code](https://devicetests.com/keycode/tab)
- [Function key codes (F1–F24)](https://devicetests.com/keycode/function-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)
