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") { /* … */ } }} />