F1 key code in JavaScript

For F1, event.key is F1, event.code is F1 and the deprecated keyCode is 112. Press any key in the Key Code Finder to see its values live.

Updated

PropertyValue
event.keyF1
event.codeF1
event.keyCode / which (deprecated)112

How to detect F1

document.addEventListener('keydown', (event) => {
  if (event.key === "F1") {
    // F1 was pressed
  }
});

Use event.key to react to what the key means (it follows the keyboard layout) and event.code for its physical position, for example WASD in games. keyCode and which are deprecated: they still work, but their values differ between browsers and layouts.

How browsers treat F1

Browsers open help on F1 in some systems. Function keys F1–F12 send keyCode 112–123; on laptops many need Fn, and the operating system may use them for brightness or volume.

In React

<input onKeyDown={(e) => { if (e.key === "F1") { /* … */ } }} />
JavaScript Keycode FinderPress any key and instantly see every property of the keyboard event, with a copy-ready keydown snippet for shortcuts.
Open the tool

Frequently asked questions

What is the keyCode for F1?
112. It’s deprecated, though: use event.key (“F1”) or event.code (F1) in new code.
What is the difference between event.key and event.code?
event.key is the character or action the key produces with the current layout; event.code is the physical key position, the same on every layout.
Should I use keydown or keypress?
keydown. The keypress event is deprecated and doesn’t fire for keys that don’t produce characters, such as Escape or the arrow keys.