webdevpuneet.com

Shortcut Recorder — Capture Keyboard Hotkeys in a Field

Shortcut Recorder · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

Copy & paste

Source Code

<div class="sk-card">
  <h3>Keyboard shortcuts</h3>
  <ul class="sk-list" id="skList">
    <li class="sk-row" data-action="Save"><span>Save</span><button type="button" class="sk-field" data-combo="Ctrl+S"></button></li>
    <li class="sk-row" data-action="Command palette"><span>Command palette</span><button type="button" class="sk-field" data-combo="Ctrl+K"></button></li>
    <li class="sk-row" data-action="New item"><span>New item</span><button type="button" class="sk-field" data-combo=""></button></li>
  </ul>
  <p class="sk-hint" id="skHint">Click a shortcut, then press a key combination.</p>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Click-to-record
Click a field, press a combo — the native shortcut-capture flow.
Chord capture
Waits for a non-modifier key, then reads all active modifiers.
Platform-aware labels
Meta shows Cmd on macOS, Win elsewhere; arrows and Esc get symbols.
kbd chip rendering
Shows the combo as individual key chips joined by +.
Duplicate detection
Flags conflicts when two actions share a combination.
Esc to cancel
Escape leaves the existing binding untouched.
preventDefault while recording
Browser combos like Ctrl+S do not fire during capture.
No library
Pure HTML/CSS/JS — no hotkey or keybinding dependency.

About this UI Snippet

Shortcut Recorder — Capture and Edit Keyboard Hotkeys with Conflict Detection

Screenshot of the Shortcut Recorder snippet rendered live

A shortcut recorder lets users rebind a keyboard hotkey by simply pressing the combination they want — the control behind every "customise keyboard shortcuts" settings screen in editors, IDEs, and power-user apps. This snippet captures a live key chord, renders it as kbd chips, normalises modifiers per platform, and flags duplicates, in plain HTML, CSS, and vanilla JavaScript.

Click to record, press to set

Each shortcut is a button showing its current binding. Click it and it enters recording mode (a glowing focus ring and "Press keys…" prompt); the next key combination you press is captured and saved. Pressing Escape cancels without changing anything. Clicking the field again toggles recording off. This click-then-press flow is exactly how native preference panes capture shortcuts.

Reading a chord correctly

The trick to capturing chords is ignoring lone modifier presses. When keydown fires for Control, Shift, Alt, or Meta by itself, the recorder waits — a shortcut isn't complete until a non-modifier key arrives. When a real key lands, it reads the modifier flags (ctrlKey, metaKey, altKey, shiftKey) and assembles them in a consistent order, then appends the key. preventDefault() stops the browser acting on combos like Ctrl+S while you're recording.

Platform-aware labels

Modifiers are prettified for humans: Control becomes "Ctrl", and Meta becomes "Cmd" on macOS or "Win" elsewhere via a quick platform check. Special keys map to symbols — arrows to ↑↓←→, Escape to "Esc", space to "Space" — and single character keys are upper-cased so "k" displays as "K". The result renders as individual kbd chips joined by +, the familiar way shortcuts appear in menus and docs.

Duplicate detection

After a binding is set, the recorder checks whether any other shortcut already uses the same combination and flags both with a red ring and a hint ("That combination is already used"). Catching conflicts at capture time is what separates a real shortcut editor from a text field — two actions bound to the same chord is a bug users can't diagnose themselves.

Portable and minimal

The captured value is a plain string like Ctrl+K stored in a data attribute, trivial to persist or feed into a hotkey library. The whole recorder is one keydown handler and a small render function with no dependencies — a clean reference for building keyboard-shortcut customisation into any settings screen.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA list of actions renders, each with its current shortcut.
  2. 2
    Click a shortcutThe field starts recording and prompts you to press keys.
  3. 3
    Press a combinationHold modifiers and press a key — it captures as Ctrl+K style chips.
  4. 4
    Cancel if neededPress Esc while recording to leave the binding unchanged.
  5. 5
    Watch for conflictsA red ring warns when a combo is already used by another action.
  6. 6
    Read the valueEach field stores its combo string in data-combo — persist it as you like.

Real-world uses

Common Use Cases

Keyboard shortcut settings
Let users rebind hotkeys by clicking a field and pressing a combination, with each key shown as an individual chip in a settings panel.
Editor and IDE preferences
Customise commands in an editor or IDE preferences screen, detecting duplicates so two actions never share the very same key combination.
Power-user apps
Offer remappable actions beside a command palette and a keyboard shortcuts cheat sheet in power-user apps.
Accessibility customisation
Allow people to set combinations that suit their hands and assistive tools, with platform-aware labels showing Cmd on macOS and Win elsewhere.
Game key bindings
Capture control bindings in a game or tool, using a chord capture that waits for a non-modifier key before reading all active modifiers.

Got questions?

Frequently Asked Questions

On keydown while recording, it ignores presses that are only a modifier (Control, Shift, Alt, Meta) and waits for a real key. When one arrives, it reads ctrlKey/metaKey/altKey/shiftKey, assembles them in a fixed order, appends the key, and saves the result. It also calls preventDefault so the browser does not act on combos like Ctrl+S during capture.

Yes. The Meta key renders as "Cmd" on macOS and "Win" on other platforms via a navigator.platform check, and Control always shows as "Ctrl". You can extend the PRETTY map to localise further or to display the actual ⌘/⌥/⇧/⌃ glyphs if you prefer the macOS style.

After a binding is set, the recorder scans the other fields for the same combo string. If it finds one, it adds a red ring to the field and shows a hint that the combination is already used. You decide the policy — warn only (as here), block the save, or swap the binding away from the other action.

A plain string such as "Ctrl+K" or "Cmd+Shift+P", held in the field's data-combo attribute. It is easy to persist to storage or a backend, render in menus, and feed into a hotkey matcher at runtime by comparing it against the same modifier flags on keydown.

Hold each binding in state and render the chips from it. Track a "recording" id in state, attach a window keydown listener while recording (in an effect that cleans up), and update the binding when a non-modifier key arrives. Compute the duplicate flag as derived state. Tailwind users replace the classes with utilities; the keydown logic is identical across frameworks.