# Keyboard Shortcuts

> React keyboard shortcuts cheat sheet: a searchable dialog of grouped shortcuts opened with the ? key or a button, with Command glyphs on Mac and Ctrl elsewhere.

- Kind: Component (Overlays)
- Docs: https://ui.minidev.pro/docs/keyboard-shortcuts
- Source (registry JSON with the full file): https://ui.minidev.pro/r/keyboard-shortcuts.json
- License: MIT. One file, yours to edit after install.

## When to use

Shortcuts are grouped, filtered as you type, and written once with portable names (mod, shift, alt, enter) that render as ⌘ ⇧ ⌥ ↵ on a Mac and Ctrl, Shift, Alt, Enter elsewhere; screen readers hear the key names. The platform is read after mount, so server and client markup match. For a static, always-visible list use ShortcutCheatSheet; for one key inline use Kbd.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/keyboard-shortcuts
```

Writes `components/ui/keyboard-shortcuts.tsx` plus the MiniDev files it is built from (button, dialog, input, kbd) and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/keyboard-shortcuts.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { KeyboardShortcuts } from "@/components/ui/keyboard-shortcuts"
```

From the npm package:

```tsx
import { KeyboardShortcuts } from "minidev-ui-kit/ui/keyboard-shortcuts"
```

## Usage

```tsx
import { KeyboardShortcuts } from "@/components/ui/keyboard-shortcuts"

export function Example() {
  return (
    <KeyboardShortcuts />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `groups` | `KeyboardShortcutGroup[]` | `SAMPLE_GROUPS` | no | Shortcut groups, shown as titled columns. Defaults to a sample invoicing app. |
| `title` | `string` | `"Keyboard shortcuts"` | no | Dialog title. |
| `hotkey` | `string` | `"?"` | no | Key that opens the sheet from anywhere on the page, outside text fields. Empty disables it. |
| `open` | `boolean` | - | no | Controlled open state. Leave undefined for internal state. |
| `onOpenChange` | `(open: boolean) => void` | - | no |  |
| `showTrigger` | `boolean` | `true` | no | Render the built-in trigger button. |
| `triggerLabel` | `string` | `"Keyboard shortcuts"` | no | Text of the built-in trigger button. |
| `className` | `string` | - | no |  |

## Examples

### Custom groups

groups is any list of sections; keys use portable names like mod, shift and enter so the sheet shows the viewer's modifier glyphs.

```tsx
const boardGroups: KeyboardShortcutGroup[] = [
  {
    title: "Board",
    shortcuts: [
      { keys: ["C"], label: "New card" },
      { keys: ["F"], label: "Filter cards" },
      { keys: ["mod", "shift", "F"], label: "Clear filters" },
      { keys: ["["], label: "Collapse column" },
      { keys: ["]"], label: "Expand column" },
    ],
  },
  {
    title: "Card",
    shortcuts: [
      { keys: ["A"], label: "Assign to me" },
      { keys: ["L"], label: "Add label" },
      { keys: ["D"], label: "Set due date" },
      { keys: ["mod", "enter"], label: "Move to done" },
      { keys: ["esc"], label: "Close card" },
    ],
  },
]

<KeyboardShortcuts groups={boardGroups} title="Board shortcuts" triggerLabel="Shortcuts" hotkey="" />
```

### Controlled from your own trigger

showTrigger false and open plus onOpenChange let a toolbar icon open the sheet; an empty hotkey leaves the global key to your app.

```tsx
function ControlledDemo() {
  const [open, setOpen] = React.useState(false)
  return (
    <div className="flex items-center gap-3 rounded-xl border border-border bg-surface px-3 py-2 shadow-raised">
      <span className="text-sm font-medium text-fg">Lumen</span>
      <span className="text-xs text-fg-subtle">Invoices</span>
      <Button variant="ghost" size="icon-sm" aria-label="Keyboard shortcuts" className="ml-6" onClick={() => setOpen(true)}>
        <KeyboardIcon />
      </Button>
      <KeyboardShortcuts open={open} onOpenChange={setOpen} showTrigger={false} hotkey="" />
    </div>
  )
}
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `?` | Anywhere outside a text field, opens the sheet (or whatever key `hotkey` is). Shift+/ also works on layouts that report it as "/". |
| `Tab` or `Shift+Tab` | Moves between the search field and the close button; focus stays in the dialog. |
| `Escape` | Closes the sheet and clears the search. |

## Accessibility

- Each row has screen-reader-only key names, such as "Command K" on a Mac or "Control K" elsewhere, while the glyphs are `aria-hidden`.
- The search field gets focus when the sheet opens and is labelled "Search shortcuts". Searching "command" also finds ⌘ shortcuts.
- Each group is a `<section>` labelled by its `<h3>`. The trigger has `aria-haspopup="dialog"` and `aria-expanded`.

## FAQ

### How do I change or turn off the ? hotkey?

Pass `hotkey` with another key, or `hotkey=""` to turn it off. The listener ignores presses with ⌘, Ctrl or Alt held, and presses inside inputs, textareas, selects and contenteditable elements, so typing a question mark in a field does not open it.

### How do I show ⌘ on Mac and Ctrl on Windows?

Write `mod` in `keys`: `{ keys: ["mod", "K"], label: "Command palette" }` shows ⌘ K on a Mac and Ctrl K elsewhere. `shift`, `alt`, `ctrl`, `enter`, `backspace`, `esc`, `tab`, `space`, `up`, `down`, `left` and `right` are mapped too.

### Does KeyboardShortcuts make the shortcuts work?

No. It only lists them. Register each shortcut in your app; the sheet is the reference people read.

### Why does a Mac show Ctrl for a moment?

The platform is read with `useSyncExternalStore`, and the server snapshot is non-Mac, so the server HTML and the first client render show Ctrl. After hydration the client switches to ⌘, which avoids a hydration mismatch.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`
- Built from: [button](https://ui.minidev.pro/docs/button/md), [dialog](https://ui.minidev.pro/docs/dialog/md), [input](https://ui.minidev.pro/docs/input/md), [kbd](https://ui.minidev.pro/docs/kbd/md)

## Related

- [Shortcut Cheat Sheet](https://ui.minidev.pro/docs/shortcut-cheat-sheet/md): Keyboard shortcut cheat sheet listing actions next to their key combinations rendered as Kbd keycaps. Pass your own rows or use the default command palette set.
- [Kbd](https://ui.minidev.pro/docs/kbd/md): A keyboard key cap for showing shortcuts, with a raised edge and room for icons, plus a KbdGroup wrapper for combos like Cmd K. Renders a semantic kbd element.
- [Alert Dialog](https://ui.minidev.pro/docs/alert-dialog/md): Shadcn style alert dialog on Base UI for confirmations that need an explicit answer. Modal backdrop, focus trap, title, description and an actions row.
- [Command Dialog](https://ui.minidev.pro/docs/command-dialog/md): Command dialog that opens the command palette in a modal from a list of commands with labels and handlers. Wire it to a Cmd K shortcut in your React app.
- [Confirm Destructive](https://ui.minidev.pro/docs/confirm-destructive/md): Destructive action confirm: a red trigger button opens a dialog with a title, warning text, Cancel and Confirm. Labels are configurable for deletes and resets.
- [Context Menu](https://ui.minidev.pro/docs/context-menu/md): Right click context menu that opens at the cursor with menu items, including destructive items in red. Closes on selection or click outside, in React.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
