How to detect Arrow Up
document.addEventListener('keydown', (event) => {
if (event.key === "ArrowUp") {
// Arrow Up 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 Arrow Up
Arrow keys scroll the page and move the caret. In custom listboxes, menus and grids, handle them to move focus between items — and call preventDefault() so the page doesn’t scroll at the same time.
In React
<input onKeyDown={(e) => { if (e.key === "ArrowUp") { /* … */ } }} />