# Kbd

> Keyboard shortcut keycap (kbd) for React and shadcn that presses down while the real key is held.

A keycap that presses down when the real key does: the face drops 1px onto its base and springs back on release. Glyphs are announced by name; useKeysHeld lights up whatever a chord belongs to.

Also known as a keyboard shortcut or a keycap.

- Tier: Free (MIT)
- Interaction: Type
- Page: https://eased.dev/components/kbd
- Registry: https://eased.dev/r/kbd.json

## Install

```bash
npx shadcn add https://eased.dev/r/kbd.json
```

A keycap that presses down when the real key does, with a `useKeysHeld` hook for lighting up whatever a shortcut belongs to.

## When to use

- Showing a keyboard shortcut beside the action it triggers: menu items, tooltips, command palette rows, empty states ("Press / to search").
- Teaching a shortcut, where seeing the cap press as you hold the key confirms you found it.
- Replaying a shortcut in a tutorial or onboarding step with `pressed`.

## When not to use

- A clickable control that looks like a key: use a [Button](/components/button).
- Inline code or a command to type: use a `code` element or a [Snippet](/components/snippet).

## Usage

```tsx search-hint.tsx
import { Kbd } from "@/components/eased/kbd/kbd";

export function SearchHint() {
  return (
    <p className="text-ui-sm text-text-muted">
      Press <Kbd>/</Kbd> to search, or <Kbd keys={["⌘", "K"]} /> for commands.
    </p>
  );
}
```

## Examples

### Lighting up a row

`useKeysHeld` is true while every key in the chord is down, from the same listener the caps use.

```tsx
import { Kbd, useKeysHeld } from "@/components/eased/kbd/kbd";

function ShortcutRow({ label, keys }: { label: string; keys: string[] }) {
  const held = useKeysHeld(keys);
  return (
    <div className={`flex justify-between rounded-control-sm px-2 py-1 ${held ? "bg-surface-hover" : ""}`}>
      {label}
      <Kbd keys={keys} />
    </div>
  );
}
```

### A replayed shortcut

`pressed` forces the look and stops the cap listening.

```tsx
<Kbd keys={["⇧", "⌘", "P"]} pressed={step === "press"} size="md" />
```

## API reference

### Kbd

| Prop | Type | Default | Description |
|---|---|---|---|
| `children` | `string` | | One key: `"K"`, `"⌘"`, `"Esc"`, `"↑"`. |
| `keys` | `string[]` | | A chord, one cap per key. Takes precedence over `children`. |
| `listen` | `boolean` | `true` | Press the cap while the real key is held. |
| `pressed` | `boolean` | | Force the pressed look. Overrides `listen`. |
| `size` | `"sm" \| "md"` | `"sm"` | 20px or 24px caps. |
| `className` | `string` | `""` | On the cap, or on the wrapper of a chord. |

Glyphs map to keys: `⌘`/`cmd` Meta, `⌥`/`opt`/`alt` Alt, `⇧`/`shift` Shift, `⌃`/`ctrl` Control, `↵`/`⏎`/`enter`/`return` Enter, `⎋`/`esc` Escape, `⌫` Backspace, `⌦` Delete, `⇥`/`tab` Tab, `␣`/`space` Space, the four arrows, and `F1` to `F12`. Letters and digits match by physical key, so `⌥K` still presses the K cap; any other glyph matches the key it types.

### useKeysHeld

`useKeysHeld(keys: string[], enabled = true): boolean`. True while every key in `keys` is down. `false` on the server.

## Accessibility

- Each cap is a `kbd` element; a chord is a `kbd` of `kbd`s.
- Symbols and abbreviations are read by name: `⌘` is announced as "Command", `Esc` as "Escape", `←` as "Left arrow". Letters and digits are read as written.
- The caps listen only; they never take focus or handle a key, so they cannot clash with the shortcut they show.

## Motion

When the key goes down the face drops 1px onto its base, and its fill and text shift to the active colours, in 100ms. When it comes up the face springs back on the snappy spring and the colours return in 150ms. The press never moves the layout: the base shows 1px below the face at rest and the face covers it. Reduced motion keeps only the colour change.

## Notes for AI

Use Kbd for every displayed shortcut, with `keys` for chords and the platform glyphs (`⌘` on macOS, `ctrl` elsewhere). Use `useKeysHeld` to highlight the row, menu item or button a shortcut belongs to; it shares the caps' single window listener, so it is cheap to call many times.

Do not add your own `keydown` listeners to animate the caps, and do not use Kbd to handle the shortcut itself: it only displays it. One listener serves every cap; it clears all keys when `⌘` is released, the window loses focus or the tab is hidden, because macOS drops key-ups while `⌘` is held.

## Related

- [Command Palette](/components/command-palette): a list of actions with their shortcuts.
- [Dropdown Menu](/components/dropdown-menu): menu items that show a shortcut.
- [Tooltip](/components/tooltip): a hint that can carry a shortcut.

## Example

`components/eased/kbd/kbd-demo.tsx`

```tsx
"use client";

import { DemoFrame } from "../lib/demo-frame";
import { modKey, useIsMac } from "../lib/platform";
import { Kbd, useKeysHeld } from "./kbd";

type Shortcut = { action: string; keys: string[]; then?: string[] };


const shortcuts = (mac: boolean): { group: string; items: Shortcut[] }[] => [
  {
    group: "General",
    items: [
      { action: "Open command menu", keys: [modKey(mac), "K"] },
      { action: "Search", keys: ["/"] },
      { action: "Show shortcuts", keys: ["⇧", "?"] },
    ],
  },
  {
    group: "Issues",
    items: [
      { action: "New issue", keys: ["C"] },
      { action: "Move up", keys: [mac ? "⌥" : "Alt", "↑"] },
      { action: "Go to inbox", keys: ["G"], then: ["I"] },
      { action: "Close panel", keys: ["Esc"] },
    ],
  },
];

export function KbdDemo() {
  const mac = useIsMac();
  return (
    <DemoFrame title="Keyboard shortcuts" meta="Try the keys">
      <div className="min-h-0 flex-1 overflow-y-auto p-1.5 [scrollbar-width:thin]">
        {shortcuts(mac).map(({ group, items }) => (
          <section key={group} aria-label={group} className="pb-1.5">
            <h3 className="px-2.5 pt-2 pb-1 text-ui-xs font-medium tracking-caps text-text-muted uppercase">{group}</h3>
            <ul>
              {items.map((s) => (
                <Row key={s.action} shortcut={s} />
              ))}
            </ul>
          </section>
        ))}
      </div>
    </DemoFrame>
  );
}

/** The row lights up while its whole chord is held, the way a shortcuts overlay confirms a match. */
function Row({ shortcut }: { shortcut: Shortcut }) {
  const held = useKeysHeld(shortcut.keys);
  return (
    <li
      data-held={held || undefined}
      className="flex h-9 items-center justify-between gap-3 rounded-control px-2.5 text-ui-sm text-text-secondary transition-colors duration-150 ease-standard data-held:bg-surface-hover data-held:text-text"
    >
      <span className="min-w-0 truncate">{shortcut.action}</span>
      <span className="flex shrink-0 items-center gap-1.5">
        <Kbd keys={shortcut.keys} />
        {shortcut.then && (
          <>
            <span className="text-ui-xs text-text-muted">then</span>
            <Kbd keys={shortcut.then} />
          </>
        )}
      </span>
    </li>
  );
}
```
