How to add keyboard shortcuts to your site — without breaking typing

There's a bug everyone writes into their first hotkeys. A user is typing a comment, presses the letter "d" — and your site deletes a task. Because the key listener doesn't know the person is currently in an input field. Let's build shortcuts step by step — covering that bug, non-English layouts, and the keys you simply can't intercept.
Step 1. One keydown listener
Don't attach handlers to every button. One listener on the whole window:
window.addEventListener('keydown', (e) => {
console.log(e.key, e.code);
});
Open the console and press some keys. e.key is the character ("k", "Enter"); e.code is the physical key ("KeyK"). That difference will matter in step 4.
Step 2. React to a combo, not a letter
A good shortcut uses a modifier: Cmd+K, Ctrl+S. On a Mac the main key is Cmd (metaKey), on Windows — Ctrl (ctrlKey). Support both with one line:
window.addEventListener('keydown', (e) => {
const mod = e.metaKey || e.ctrlKey;
if (mod && e.code === 'KeyK') {
e.preventDefault();
openSearch();
}
});
e.preventDefault() is mandatory: otherwise the browser performs its own action too (Ctrl+S opens the save-page dialog).
One hard limit: Cmd+W, Ctrl+T and other tab-management keys are never handed over by the browser — your handler won't even run. Don't build shortcuts on them.
Step 3. The classic bug — guard the input fields
Now that bug. "Bare" shortcuts without a modifier — "d" to delete, "?" for help — must stay silent while someone is typing:
function isTyping(e) {
const el = e.target;
return el.matches('input, textarea, select') || el.isContentEditable;
}
window.addEventListener('keydown', (e) => {
if (isTyping(e)) return;
if (e.key === '?') showHotkeyHelp();
});
e.target is the element where the keypress happened. If it's an input — bail out. Cmd/Ctrl combos don't need this guard: they don't occur accidentally while typing.
Step 4. Survive non-English layouts
A sneaky detail English-language guides never mention. On a Russian (or Greek, or Hebrew) layout, e.key for the K key returns a local letter — and the e.key === 'k' shortcut silently dies.
The fix is simple: for letter shortcuts compare e.code. "KeyK" is the physical key — it doesn't care which language is active. For symbol shortcuts ("?", "/") do the opposite and keep e.key — there you care about the character itself.
Step 5. Tell people the shortcuts exist
Hotkeys nobody knows about don't exist. The industry standard: the "?" key opens the shortcut list — GitHub and Gmail both do this. Add a small overlay with the list — it's two lines of markup, and the feature comes alive.
Step 6. Start with the right set
Don't invent exotic combos — take the shortcuts hands are already trained on in other apps:
- Cmd/Ctrl+K — search or a command palette. It's become the shared standard: Slack, Notion, GitHub.
- Esc — close a modal or cancel. Users press it instinctively; a modal that ignores Esc makes the app feel broken.
- "?" — show the shortcut list.
- Enter in a list — open the selected item.
- j / k — down and up through a list, like Gmail and GitHub. These are "bare" letters — they require the guard from step 3 and
e.codefrom step 4.
Four or five are enough to start. Add exotic key sequences once habits and user requests appear.
What you get: Cmd+K opens search, Esc closes windows, "?" shows the help, typing in fields breaks nothing — and it all works on any keyboard layout. Power users will notice: for that "this app is carefully made" feeling, hotkeys punch above most big features. A dark mode toggle on "t" lands nicely too.
Why does the shortcut fail for some users?
Most often — the layout: you compared e.key === 'k', and the user has a non-English layout active. Compare e.code === 'KeyK' — and the shortcut survives any language.
Do I need a library for keyboard shortcuts?
For two or three shortcuts — no: the listener from this guide covers it. Libraries earn their keep on key sequences like "g then i" à la Gmail — but inside it's the same keydown principle.
Do keyboard shortcuts work on phones?
No keyboard — no hotkeys. On touch devices, hide the "press ?" hint — it only confuses.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





