How to detect Alt / Option
document.addEventListener('keydown', (event) => {
if (event.key === 'Alt') {
// Alt / Option 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 Alt / Option
Check event.altKey. On many European keyboards the right Alt key is AltGr (event.key === "AltGraph") and types characters such as @ or €, so avoid Ctrl+Alt shortcuts that clash with it. On a Mac, Option changes event.key to special characters — use event.code for Option shortcuts.
In React
<input onKeyDown={(e) => { if (e.key === 'Alt') { /* … */ } }} />