Control key code

Both Ctrl keys send event.key "Control" and keyCode 17. Only event.code and event.location tell left from right.

Every value at a glance

Values for Control
Keyevent.codeevent.keykeyCodeevent.location
Left ControlControlLeftControl171 (Left side)
Right ControlControlRightControl172 (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 Control

Using only a keyboard? Press Esc, then Tab, to leave the key box.

Code to react to Left Control

Recommended: match event.key
document.addEventListener('keydown', (event) => {
  if (event.key === 'Control') {
    // Your code here
  }
});
Match the key's place: event.code
document.addEventListener('keydown', (event) => {
  if (event.code === 'ControlLeft') {
    // Your code here
  }
});
Old code that still uses keyCode (deprecated)
document.addEventListener('keydown', (event) => {
  if (event.keyCode === 17) {
    // Your code here
  }
});

Which value to use

For shortcuts, check event.ctrlKey on the other key’s keydown. It is true while either Ctrl key is held, so you don’t track Ctrl yourself.

To react to Ctrl itself, check event.key === 'Control'. Both keys send it, with keyCode 17. Use event.code (ControlLeft or ControlRight) or event.location (1 left, 2 right) to tell them apart. Many laptops have no right Ctrl at all.

On a Mac, the key labelled control sends the same values. But Mac shortcuts use Cmd, which is metaKey, not ctrlKey.

What trips people up

The shortcut works on Windows but not on a Mac

Mac users press Cmd + S, not Ctrl + S. Accept both:

if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 's') {
  event.preventDefault();
  save();
}

Show the right label too: Ctrl + S on Windows and Linux, Cmd + S or ⌘S on a Mac.

Ctrl fires when the user types @ or € with AltGr

On Windows, AltGr works as Ctrl and Alt together. Chrome and Edge send a Left Control keydown first, then Right Alt, and ctrlKey and altKey are both true on the typed key. A Ctrl + Alt + letter shortcut can then fire while a Polish user types “ą” or a German user types “@”. Avoid Ctrl + Alt shortcuts, or skip keys whose event.key is a character other than the letter you expect.

Ctrl+W, Ctrl+T, Ctrl+N or Ctrl+Tab can’t be stopped

The browser keeps them: Ctrl + N, Ctrl + T, Ctrl + W, Ctrl + Tab, Ctrl + Page Up and Ctrl + Page Down on Windows. In Chrome and Edge the page gets no keydown; Firefox may send the keydown, but preventDefault() can’t stop the browser in either. Pick other shortcuts. Ctrl + S, Ctrl + P and Ctrl + F do arrive, and preventDefault() stops the browser’s own action.

Ctrl+click opens a menu on a Mac

On a Mac, Control + click is a right-click: the browser fires contextmenu. Don’t use Ctrl + click for “add to selection” on a Mac; use Cmd + click (metaKey) there, as Mac apps do.

Ctrl+scroll zooms the page

Ctrl with the mouse wheel zooms, and so does a trackpad pinch, which Chrome, Edge and Firefox send as a wheel event with Ctrl. A wheel listener sees ctrlKey true; call preventDefault() on it only if your map or canvas does its own zoom, and register it with { passive: false }.