How to detect Numpad keys
document.addEventListener('keydown', (event) => {
if (event.code.startsWith('Numpad')) {
// Numpad keys 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 Numpad keys
Numpad digits have the same event.key as the top-row digits but different event.code values (Numpad0–Numpad9) and keyCodes 96–105. NumpadAdd is 107, NumpadSubtract 109, NumpadMultiply 106, NumpadDivide 111 and NumpadDecimal 110.
In React
<input onKeyDown={(e) => { if (e.code.startsWith('Numpad')) { /* … */ } }} />