Skip to content

Shortcut Recorder Field

Records a keyboard chord by physical key position, and refuses the ones the browser owns.

Prominimalbrutalist
Category
Components
Added
2026-09-23
Deps
1
Updated
2026-09-23

This is a viberdy Pro component

The live preview, the props panel and the write-up are open to everyone. The full source, the AI prompt and CLI install are part of Pro.

Preview
Open command palette

Bound by physical key position, so the chord survives a layout change.

Props

Require a modifier
No runtime dependencies
"use client";

import { useCallback, useEffect, useState, useSyncExternalStore } from "react";

export type Chord = {
  ctrl: boolean;
  alt: boolean;
  shift: boolean;
  meta: boolean;
  /** KeyboardEvent.code — a PHYSICAL key, not the character it produced. */
  code: string;
};

const MOD_CODES = new Set([
  "ControlLeft",
  "ControlRight",
  "AltLeft",
  "AltRight",

  // ---------------------------------------------------------------
  // Full source available with a viberdy Pro subscription.
  // https://viberdy.dev/pricing
  // ---------------------------------------------------------------
}
Notes

About this component

Any app with a shortcuts panel needs this field, and almost every implementation gets the same thing wrong: it stores the character the key produced rather than the key itself. KeyboardEvent.key is layout-dependent, so binding Alt+K on macOS records a degree sign, and the same physical key on an AZERTY keyboard records a different letter entirely. KeyboardEvent.code is the physical position, which is what a shortcut actually means. The second omission is more damaging. A handful of chords belong to the browser or the operating system, and preventDefault cannot hold them — the event often never reaches the page. Capturing Cmd+W and displaying it as bound hands the user a shortcut that closes their tab on every press, so those chords are refused with a stated reason instead. Recording listens in the capture phase and stops propagation, which keeps the browser find bar closed and the app own hotkeys quiet while a chord is being chosen. Modifier keys are shown as they are held rather than ending the capture, and the platform-specific glyphs are resolved through useSyncExternalStore because reading navigator during render is a hydration mismatch waiting to happen.

Curator’s note

The reserved-chord list is the part people leave out. A recorder that happily accepts Cmd+W has not captured a shortcut, it has built a trap.

Related

Pairs well with

Matched on shared tags and category — the entries most likely to be used alongside this one.

Pro

A recursive, collapsible file tree with real roving-tabindex arrow-key navigation.

treefile explorersidebar
Pro

A keyboard-driven spreadsheet grid with roving-cell navigation and Excel-style Enter/Tab commits.

gridspreadsheeteditable
Pro

A Cmd+K menu with real submenu drilling, a breadcrumb trail, and scoped per-level search.

command menucmdknavigation