Meta key code (Windows key and Command)
The Windows key and Cmd send event.key "Meta". keyCode is 91 for the left key, but the right key and Firefox on a Mac differ.
Every value at a glance
| Key | event.code | event.key | keyCode | keyCode on a Mac | event.location |
|---|---|---|---|---|---|
| Left Windows / Left Command | MetaLeft | Meta | 91 | 1 (Left side) | |
| Right Windows / Right Command | MetaRight | Meta | 92 | 93 | 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.
Press it here
Click here, then press the Windows key or Cmd
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Left Windows
document.addEventListener('keydown', (event) => {
if (event.key === 'Meta') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'MetaLeft') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 91) {
// Your code here
}
});Which value to use
For Mac shortcuts, check event.metaKey on the other key’s keydown. It is true while Cmd (or the Windows key) is held. Combine it with ctrlKey for shortcuts that should work on both systems.
To react to the key itself, check event.key === 'Meta', or event.code: MetaLeft or MetaRight. The same names cover the Windows key, the Mac command key and the Super key on Linux.
Don’t use keyCode for this key. The left key is 91, but Firefox on a Mac sends 224 for both keys, Firefox on Windows and Linux sends 91 for both, and the right key is 92 on Windows but 93 in Chrome and Safari on a Mac.
What trips people up
On a Mac, keyup never comes for keys pressed with Cmd held
macOS doesn’t send the keyup of keys pressed while Cmd is held: press Cmd + K, release K, and the page gets no keyup for K. Browsers can’t fully fix it, because macOS doesn’t report the release. Don’t track held keys through Cmd shortcuts. When the Meta keyup arrives, mark the keys pressed while Cmd was held as released.
keyCode 93 means two different keys
On Windows, 93 is the menu key (event.code “ContextMenu”). In Chrome and Safari on a Mac, 93 is the right Cmd key. Use event.code to know which one you got.
The Windows key opens the Start menu instead
The Windows key on its own opens Start, and Windows shortcuts such as Windows + D, Windows + L or Windows + Tab go to the system. The page can’t stop them. Start takes the focus, so keyups can go missing: reset held keys on the window’s blur event. Don’t build shortcuts on the Windows key.
Old Firefox sends “OS” and “OSLeft”
Firefox before version 118 sent event.key “OS” for the Windows key and event.code “OSLeft” and “OSRight”. Chrome used “OSLeft” and “OSRight” before version 52. Current browsers send "Meta", "MetaLeft" and "MetaRight". Check the old names only if you support those versions.
Cmd shortcuts on a Mac
Mac users expect Cmd where Windows uses Ctrl: Cmd + S to save, Cmd + Z to undo, Cmd + Shift + Z to redo. Accept event.ctrlKey || event.metaKey, and label the shortcut Cmd + S or ⌘S on a Mac. Cmd + N, Cmd + T, Cmd + W and Cmd + Q belong to the browser and never reach the page.