---
title: "Tab Key Code: keyCode 9, event.key \"Tab\" (JavaScript)"
description: "The Tab key in JavaScript sends event.key \"Tab\", event.code \"Tab\" and keyCode 9. Press it to check, and catch Shift+Tab without trapping keyboard users."
url: https://devicetests.com/keycode/tab
language: en
---

# Tab key code

Tab sends event.key "Tab", event.code "Tab" and keyCode 9, with or without Shift.

## Every value at a glance

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

Recommended: match event.key

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

Match the key's place: event.code

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

Old code that still uses keyCode (deprecated)

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

## Which value to use

Check `event.key === 'Tab'`. There is one Tab key, and it sends the same value on every layout. `event.code` is also `"Tab"`.

Shift + Tab still sends `"Tab"`: Shift doesn’t change the key’s name. Check `event.shiftKey` to tell the two apart.

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

## What trips people up

### keyup arrives on a different element

Tab moves focus on keydown. So the keydown goes to the field you left and the keyup goes to the field you arrived at. A keyup handler on a field sees Tab every time someone tabs into it. Handle Tab on keydown.

### Keyboard users get stuck in my widget

If you call `preventDefault()` on Tab, focus never moves, and keyboard users can’t leave. Without another way out, that fails WCAG 2.1.2, No Keyboard Trap. Only stop Tab for a good reason, such as a modal dialog that keeps focus inside while it’s open. Then give a clear way out: Esc, or a close button.

### Tab in my textarea leaves the field instead of indenting

That is the default, and most users want it. In a code editor you can insert a tab instead:

```
if (event.key === 'Tab' && !event.shiftKey) {
  event.preventDefault();
  textarea.setRangeText('\t', textarea.selectionStart, textarea.selectionEnd, 'end');
}
```

Pair it with a way out, as the finder does: press Esc first, then Tab moves on. `setRangeText()` doesn’t add to the undo history; `document.execCommand('insertText', false, '\t')` does, though it’s deprecated.

### Ctrl+Tab and Alt+Tab never reach the page

The browser and the computer keep them: Ctrl + Tab switches browser tabs, Alt + Tab (Cmd + Tab on a Mac) switches apps. Your page may see the Ctrl or Alt keydown, then nothing. When the user comes back, a modifier may look stuck because its keyup went to another window. Reset modifier state on the window’s `blur` event.

### In Safari, Tab skips links and buttons

By default Safari on a Mac moves only between form fields. Option + Tab reaches every item, or turn on “Press Tab to highlight each item on a webpage” in Safari’s settings. Test your tab order with that on, and don’t assume Mac users reach a button by Tab alone.

## Tab order

Tab follows the order of elements in the HTML. `tabindex="0"` adds an element to that order and `tabindex="-1"` takes it out. Avoid positive numbers: they jump ahead of everything else and confuse people.

## Related keys

- [Shift key code](https://devicetests.com/keycode/shift)
- [Escape key code](https://devicetests.com/keycode/escape)
- [Enter key code](https://devicetests.com/keycode/enter)
- [Space key code](https://devicetests.com/keycode/space)
- [Control key code](https://devicetests.com/keycode/control)
- [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)
