# Hotkey Recorder

> A field that records a keyboard shortcut when pressed. It stores one string that means ⌘ on a Mac and Ctrl elsewhere, draws the keys for the viewer's keyboard, and reports shortcuts that are already taken.

Source: https://www.honestui.com/docs/product/hotkey-recorder

```tsx
"use client"

import * as React from "react"

import { Button } from "@/components/honest-ui/ui/button"
import {
  HotkeyRecorder,
  HotkeyRecorderControl,
  HotkeyRecorderLabel,
  HotkeyRecorderStatus,
  type HotkeyBinding,
} from "@/registry/default/product/hotkey-recorder/hotkey-recorder"

const defaults: HotkeyBinding[] = [
  { id: "search", label: "Open search", hotkey: "Mod+K" },
  { id: "new-issue", label: "New issue", hotkey: "Mod+Shift+I" },
  { id: "sidebar", label: "Toggle sidebar", hotkey: "Mod+B" },
  { id: "inbox", label: "Go to inbox", hotkey: "Alt+1" },
]

/**
 * Every shortcut in an app, each checked against the others. Click one and
 * press a new combination; set two to the same keys to see the conflict.
 */
export function HotkeyRecorderDemo() {
  const [bindings, setBindings] = React.useState(defaults)
  const changed = bindings.some((binding, index) => binding.hotkey !== defaults[index].hotkey)

  return (
    <div className="flex w-full max-w-lg min-w-0 flex-col gap-[var(--hui-space-3)]">
      <ul className="m-0 flex list-none flex-col overflow-hidden rounded-[var(--hui-radius-2)] border-[0.5px] border-[var(--hui-color-border-base-primary)] bg-[var(--hui-color-background-base-primary)] p-0">
        {bindings.map((binding, index) => (
          <li
            key={binding.id}
            className="border-t-[0.5px] border-t-[var(--hui-color-border-base-primary)] px-[var(--hui-space-5)] py-[var(--hui-space-4)] first:border-t-0"
          >
            <HotkeyRecorder
              value={binding.hotkey || null}
              onValueChange={(hotkey) =>
                setBindings((current) =>
                  current.map((item, i) => (i === index ? { ...item, hotkey: hotkey ?? "" } : item))
                )
              }
              bindings={bindings.filter((item) => item.hotkey)}
              bindingId={binding.id}
              className="grid w-full grid-cols-[minmax(0,1fr)_minmax(9rem,12rem)] items-center gap-x-[var(--hui-space-5)]"
            >
              <HotkeyRecorderLabel className="mb-0">{binding.label}</HotkeyRecorderLabel>
              <HotkeyRecorderControl />
              <HotkeyRecorderStatus className="col-span-2" />
            </HotkeyRecorder>
          </li>
        ))}
      </ul>
      <div>
        <Button size="sm" variant="ghost" disabled={!changed} onClick={() => setBindings(defaults)}>
          Restore defaults
        </Button>
      </div>
    </div>
  )
}

```

## Overview [#overview]

Hotkey Recorder lets people choose their own keyboard shortcuts. Activate the field, press a combination, and the field shows it as keycaps. The value is a string such as `Mod+Shift+K`.

`Mod` is the platform's command key: ⌘ on a Mac, Ctrl on Windows and Linux. One stored shortcut works on both, and the field draws it as ⇧ ⌘ K or Ctrl Shift K for whoever is looking. Keys are read by their physical position, so Option+K on a Mac is recorded as `Alt+K` and not as the character Option makes.

Pass the other shortcuts in your app as `bindings` and the field reports a combination that is already used. Pass `browserReservedHotkeys` to warn about, or refuse, shortcuts the browser keeps for itself.

The field records shortcuts. It does not listen for them while your app runs. Match key events with `hotkeyFromEvent` and `hotkeysEqual`, which use the same rules.

## Anatomy [#anatomy]

<Anatomy title="HotkeyRecorder">
  <AnatomyItem name="HotkeyRecorder" description="Holds the shortcut, the recording state, and the bindings it is checked against">
    <AnatomyItem name="HotkeyRecorderLabel" description="Names the field" />

    <AnatomyItem name="HotkeyRecorderControl" description="The shortcut as keycaps, the keys being held while recording, and a clear button" />

    <AnatomyItem name="HotkeyRecorderStatus" description="How to record, why a shortcut was refused, and which binding it conflicts with" />
  </AnatomyItem>
</Anatomy>

`HotkeyKeys` draws any shortcut string as keycaps, for menus and help screens. Installing `hotkey-recorder` brings `Label` along as a registry dependency.

## Behavior [#behavior]

### Recording [#recording]

Clicking the field, or pressing Enter or Space on it, starts recording, and the field shows "Press keys…". While modifiers are held, they appear as keycaps. The first key pressed with them completes the shortcut. Recording ends, and `onValueChange` receives the new value.

While recording:

* **Escape** cancels and keeps the previous shortcut.
* **Backspace or Delete**, pressed on their own, clear the shortcut. With modifiers, they can be recorded, as in `Mod+Backspace`.
* **Tab** leaves the field and cancels.
* **Leaving the field any other way** also cancels.

When the field is not recording, Backspace clears the shortcut, and so does the clear button.

### Rules [#rules]

With `requireModifier`, which is on by default, a shortcut needs ⌘, Ctrl, Option, or Alt. A bare `K` or `Shift+K` would fire while someone types in a text field. The field refuses it, says why, and keeps recording. Function keys are allowed on their own.

### Conflicts [#conflicts]

A shortcut that matches one of `bindings` or `reserved` is a conflict. Comparison follows the viewer's platform: outside a Mac, `Mod+K` and `Ctrl+K` are the same keys. The binding being edited is skipped when its id is passed as `bindingId`.

With `conflicts="warn"`, the default, the shortcut is saved and the status names the other binding. `details.conflict` tells your app, so it can unassign the other shortcut or ask. With `conflicts="block"`, the shortcut is refused and recording continues.

Browsers handle some shortcuts, such as ⌘W and Ctrl+T, before the page sees them. Others, such as ⌘L, reach the page only sometimes. `browserReservedHotkeys` lists the common ones.

### Platform [#platform]

The viewer's platform is read after hydration. The server renders the Windows and Linux spelling, and the field switches to Mac keycaps if needed. Pass `platform` to fix it, for example to preview shortcuts for another keyboard.

## Accessibility [#accessibility]

The field is a button labelled by `HotkeyRecorderLabel`. Its accessible name includes the shortcut in words, such as "Open search, Shift Command K, press to change". The keycaps are hidden from screen readers. While recording, `aria-pressed` is true and the name reads "Press keys…".

`HotkeyRecorderStatus` is a polite status region and describes the field. It reads the recording instructions, refusals, and conflicts as they happen. Keep it mounted. Refusals add an icon and an invalid outline, and conflicts add a warning icon, so neither depends on color alone.

The clear button is a separate tab stop named "Clear shortcut".

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;hotkey-recorder&#x22;]" />
  

  
    
      
        Add the 

        `label`

         component.
      

      
        Copy and paste these files into your project.
      

      <ComponentSource name="hotkey-recorder" title="components/ui/hotkey-recorder/hotkey-recorder-types.ts" file="hotkey-recorder-types.ts" />

      <ComponentSource name="hotkey-recorder" title="components/ui/hotkey-recorder/hotkey-recorder-utils.ts" file="hotkey-recorder-utils.ts" />

      <ComponentSource name="hotkey-recorder" title="components/ui/hotkey-recorder/hotkey-recorder.tsx" file="hotkey-recorder.tsx" />

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import {
  HotkeyRecorder,
  HotkeyRecorderControl,
  HotkeyRecorderLabel,
  HotkeyRecorderStatus,
} from "@/components/ui/hotkey-recorder/hotkey-recorder"

const [hotkey, setHotkey] = React.useState<string | null>("Mod+K")

<HotkeyRecorder value={hotkey} onValueChange={setHotkey} bindings={bindings} bindingId="search">
  <HotkeyRecorderLabel>Open search</HotkeyRecorderLabel>
  <HotkeyRecorderControl />
  <HotkeyRecorderStatus />
</HotkeyRecorder>
```

### Listening for the shortcut [#listening-for-the-shortcut]

```tsx
import { hotkeyFromEvent, hotkeysEqual, stringifyHotkey, useHotkeyPlatform } from "@/components/ui/hotkey-recorder/hotkey-recorder"

const platform = useHotkeyPlatform()

React.useEffect(() => {
  const onKeyDown = (event: KeyboardEvent) => {
    const pressed = hotkeyFromEvent(event, platform)
    if (pressed && hotkey && hotkeysEqual(stringifyHotkey(pressed), hotkey, platform)) {
      event.preventDefault()
      openSearch()
    }
  }
  window.addEventListener("keydown", onKeyDown)
  return () => window.removeEventListener("keydown", onKeyDown)
}, [hotkey, platform])
```

### Refusing browser shortcuts [#refusing-browser-shortcuts]

```tsx
<HotkeyRecorder reserved={browserReservedHotkeys} conflicts="block">
```

### Showing a shortcut [#showing-a-shortcut]

```tsx
<HotkeyKeys hotkey="Mod+Shift+P" />
```

## Don't do this [#dont-do-this]

### Storing what the event says [#storing-what-the-event-says]

```tsx
// Bad. "Meta+K" on a Mac and "Control+K" on Windows are two settings for one shortcut,
// and event.key turns Option+K into "˚".
save(`${event.metaKey ? "Meta+" : ""}${event.key}`)
```

Store the value the field gives you. It uses `Mod` and physical keys.

### Recording inside a form field's keydown handler [#recording-inside-a-form-fields-keydown-handler]

A form that submits on Enter, or a global shortcut handler on `window`, can act on keys meant for the recorder. Check `event.defaultPrevented` in your own handlers; the field prevents the default for every key it records.

### Letting conflicts pass silently [#letting-conflicts-pass-silently]

If two actions share keys, both fire. Use `details.conflict` to unassign the other action, or block the shortcut.

## Examples [#examples]

### Browser shortcuts and keyboards [#browser-shortcuts-and-keyboards]

Browser shortcuts are refused here instead of saved. The keyboard toggle changes the keycaps, and the stored value stays `Mod+Shift+Space`.

```tsx
"use client"

import * as React from "react"

import { ToggleGroup, Toggle } from "@/components/honest-ui/ui/toggle"
import {
  HotkeyRecorder,
  HotkeyRecorderControl,
  HotkeyRecorderLabel,
  HotkeyRecorderStatus,
  browserReservedHotkeys,
  type HotkeyPlatform,
} from "@/registry/default/product/hotkey-recorder/hotkey-recorder"

/**
 * Browser shortcuts are refused instead of saved, so try ⌘T or Ctrl+T. The
 * stored string stays the same when the keyboard changes; only the keycaps do.
 */
export function HotkeyRecorderReserved() {
  const [hotkey, setHotkey] = React.useState<string | null>("Mod+Shift+Space")
  const [platform, setPlatform] = React.useState<HotkeyPlatform>("mac")

  return (
    <div className="flex w-full max-w-sm min-w-0 flex-col gap-[var(--hui-space-5)]">
      <ToggleGroup
        aria-label="Keyboard"
        value={[platform]}
        onValueChange={(next: unknown[]) => next[0] && setPlatform(next[0] as HotkeyPlatform)}
        size="sm"
      >
        <Toggle value="mac">Mac</Toggle>
        <Toggle value="other">Windows and Linux</Toggle>
      </ToggleGroup>
      <HotkeyRecorder
        className="w-full"
        value={hotkey}
        onValueChange={setHotkey}
        reserved={browserReservedHotkeys}
        conflicts="block"
        platform={platform}
      >
        <HotkeyRecorderLabel>Quick capture</HotkeyRecorderLabel>
        <HotkeyRecorderControl />
        <HotkeyRecorderStatus />
      </HotkeyRecorder>
      <p className="m-0 flex items-center justify-between gap-[var(--hui-space-4)] border-t-[0.5px] border-t-[var(--hui-color-border-base-primary)] pt-[var(--hui-space-4)] text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-small)] [line-height:var(--hui-line-height-small)]">
        <span>Stored value</span>
        <code className="font-mono text-[var(--hui-color-foreground-base-primary)]">{hotkey ?? "null"}</code>
      </p>
    </div>
  )
}

```

## API reference [#api-reference]

### HotkeyRecorder [#hotkeyrecorder]

Accepts native div props beyond those listed.

| Prop              | Type                            | Default               | Description                                                                                              |
| ----------------- | ------------------------------- | --------------------- | -------------------------------------------------------------------------------------------------------- |
| `value`           | `string \| null`                | None                  | The shortcut, such as `Mod+Shift+K`. `null` is no shortcut.                                              |
| `defaultValue`    | `string \| null`                | `null`                | The initial shortcut when uncontrolled.                                                                  |
| `onValueChange`   | `(value, details) => void`      | None                  | Runs when a shortcut is recorded or cleared. `details.conflict` names a binding that uses the same keys. |
| `bindings`        | `HotkeyBinding[]`               | `[]`                  | Shortcuts already in use.                                                                                |
| `bindingId`       | string                          | None                  | The binding this field edits, so it does not conflict with itself.                                       |
| `reserved`        | `HotkeyBinding[]`               | `[]`                  | Shortcuts the app should not use, such as `browserReservedHotkeys`.                                      |
| `conflicts`       | `"warn" \| "block"`             | `"warn"`              | Save a conflicting shortcut with a warning, or refuse it.                                                |
| `requireModifier` | boolean                         | `true`                | Refuse shortcuts without ⌘, Ctrl, or Alt. Function keys are allowed.                                     |
| `platform`        | `"mac" \| "other"`              | The viewer's platform | The keyboard to read and draw for.                                                                       |
| `disabled`        | boolean                         | `false`               | Disables recording and the clear button.                                                                 |
| `labels`          | `Partial<HotkeyRecorderLabels>` | English               | Every word the field writes and the spoken key names.                                                    |

### HotkeyRecorderLabel [#hotkeyrecorderlabel]

Accepts every `Label` prop. It is connected to the field for you.

### HotkeyRecorderControl [#hotkeyrecordercontrol]

Accepts native div props. `data-recording` is set while recording and `data-invalid` after a refusal.

### HotkeyRecorderStatus [#hotkeyrecorderstatus]

Accepts native paragraph props. `data-state` is `empty`, `hint`, `warning`, or `error`.

### HotkeyKeys [#hotkeykeys]

| Prop       | Type                              | Default               | Description               |
| ---------- | --------------------------------- | --------------------- | ------------------------- |
| `hotkey`   | `string \| Partial<ParsedHotkey>` | Required              | The shortcut to draw.     |
| `platform` | `"mac" \| "other"`                | The viewer's platform | The keyboard to draw for. |

### Helpers [#helpers]

| Export                                | Shape                                                                                     |
| ------------------------------------- | ----------------------------------------------------------------------------------------- |
| `parseHotkey`                         | `(hotkey) => ParsedHotkey \| null`                                                        |
| `stringifyHotkey` / `normalizeHotkey` | Write the canonical spelling: `Mod`, `Ctrl`, `Meta`, `Alt`, `Shift`, then the key         |
| `hotkeyFromEvent`                     | `(event, platform) => ParsedHotkey \| null`, from physical keys                           |
| `hotkeysEqual`                        | `(a, b, platform) => boolean`                                                             |
| `findHotkeyConflict`                  | `(hotkey, { bindings?, excludeId?, reserved?, platform }) => HotkeyConflict \| null`      |
| `describeHotkey`                      | `(parsed, platform) => string`, such as "Shift Command K"                                 |
| `useHotkeyPlatform`                   | `(override?) => "mac" \| "other"`                                                         |
| `browserReservedHotkeys`              | New tab, close tab, new window, private window, reopen tab, address bar, reload, and quit |
