Detecting a letter
document.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() === 's' && (e.ctrlKey || e.metaKey)) {
e.preventDefault(); // Ctrl/Cmd + S
save();
}
});Comparing e.key.toLowerCase() handles Shift and Caps Lock. For game controls that should stay in the same place on every layout, compare e.code ("KeyW", "KeyA", "KeyS", "KeyD") instead.
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
Is event.key upper or lower case for letter?
Letters follow Shift and Caps Lock: event.key is “a” normally and “A” with Shift. event.code stays KeyA either way.
Do key codes change with the keyboard layout?
event.key changes with the layout (on a French AZERTY keyboard the A position types Q); event.code always names the physical key.
Should I still use keyCode?
No — it’s deprecated. Use event.key for characters and event.code for positions.