---
title: "Alt Key Code: keyCode 18, event.key \"Alt\" (JavaScript)"
description: "Alt (Option on a Mac) in JavaScript: event.key \"Alt\", keyCode 18, event.code \"AltLeft\" or \"AltRight\". Handle AltGr, Option characters and the Windows menu."
url: https://devicetests.com/keycode/alt
language: en
---

# Alt key code

Both Alt keys send keyCode 18 and usually event.key "Alt", but Right Alt is "AltGraph" on many European layouts.

## Every value at a glance

| Key | event.code | event.key | keyCode | event.location |
| - | - | - | - | - |
| Left Alt / Left Option | `AltLeft` | `Alt` | 18 | 1 (Left side) |
| Right Alt / Right Option | `AltRight` | `Alt` | 18 | 2 (Right side) |

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 Alt

Recommended: match event.key

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

Match the key's place: event.code

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

Old code that still uses keyCode (deprecated)

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

## Which value to use

For shortcuts, check `event.altKey` on the other key’s keydown. It is true while either Alt key is held (on Linux, Firefox may report a Right Alt that works as AltGr as AltGraph instead). On a Mac, the option key is Alt: same values, same `altKey`.

To react to Alt itself, check `event.code`: `AltLeft` or `AltRight`. On Windows and a Mac both send keyCode 18 (on Linux an AltGr key can send 225), but `event.key` isn’t always `"Alt"`: on layouts with an AltGr key, such as German, French or Polish, Right Alt sends `"AltGraph"`.

For the letter in an Alt shortcut, match `event.code` (`KeyK`), not `event.key`. On a Mac, Option changes the character the key types.

## What trips people up

### Option+A gives event.key “å” on a Mac

On a Mac, Option types extra characters: Option + A is “å” and Option + S is “ß” on a US layout, and Option + E is a dead key (`event.key` “Dead”). So `event.key === 'a'` never matches with Option held. Check `event.code === 'KeyA'` together with `event.altKey`.

### Right Alt doesn’t count as Alt

On Windows and Linux with a European layout, Right Alt is AltGr. It sends `event.key` “AltGraph”, and on Windows Chrome and Edge send a Left Control keydown before it, with `ctrlKey` and `altKey` both true. Treat AltGr as a typing key, not a shortcut key: AltGr + Q types “@” on a German keyboard.

### Pressing Alt on its own moves focus out of the page

On Windows, pressing and releasing Alt alone (or F10) is the menu key: Chrome and Edge focus their menu button, Firefox shows its menu bar. The next keys go to the browser, not your page. If your app uses Alt alone, call `event.preventDefault()` on its keydown and keyup; that usually keeps the focus in the page.

### Alt+Tab and Alt+F4 never arrive

The system keeps them, so the page gets no keydown, and nothing can stop them. Your page may see the Alt keydown and then lose focus without its keyup: reset held keys on the window’s `blur` event.

### Alt+letter opens a browser menu

Some Alt + letter shortcuts belong to the browser on Windows and Linux, such as Alt + D for the address bar, Alt + F for the menu, and Alt + Left for Back. Avoid them for your own shortcuts.

## Alt codes on Windows

Holding Alt and typing digits on the number pad (Alt + 0233 for “é”) types a character when Alt is released. The page usually sees each digit as a keydown with `altKey` true. Don’t bind Alt + number pad digits to shortcuts.

## Related keys

- [Control key code](https://devicetests.com/keycode/control)
- [Meta key code (Windows key and Command)](https://devicetests.com/keycode/meta)
- [Shift key code](https://devicetests.com/keycode/shift)
- [Letter key codes (A–Z)](https://devicetests.com/keycode/letter-keys)
- [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)
