# Keyboard

A laptop keyboard drawn from above with every legend. Keys travel down as a line types along, shift held for capitals, or light from real key presses; a shortcut can be ringed in the accent.

Example: Keyboard typing along. A request typed into a field while the keys press under it, shift held for the capital. Loops. Preview: https://chatcn.dev/preview/keyboard-demo

## Installation

```bash
npx shadcn@latest add https://chatcn.dev/r/keyboard.json
```

## Usage

```tsx
import { Keyboard, useTypeAlong } from "@/components/ui/chatcn/keyboard"

function Demo() {
  const t = useTypeAlong("Summarize the last five support tickets", { loop: true })
  return (
    <div ref={t.ref}>
      <MyField value={t.typed} />
      <Keyboard pressed={t.pressed} />
    </div>
  )
}

// Teach a shortcut
<Keyboard highlight={["MetaLeft", "KeyK"]} />
```

Keys are named by `KeyboardEvent.code` (`KeyA`, `ShiftLeft`, `MetaLeft`, `Space`), so `pressed`, `highlight` and real key events need no table. The layout and legends come from `chatcn-keyboard`: US ANSI, 14.5 keys wide, with letters centred on their cap height, shifted pairs stacked, words on their own side, and modifier and function glyphs placed by their ink, not their box.

A press is physical: the cap slides down over its own skirt in 100 ms, so its drop shortens to nothing, and comes back up in 160 ms. Legend strength follows the rendered cap size: under about 500 px wide the words and function labels step aside and the letters and glyphs stay. Under reduced motion a pressed cap only brightens.

`useTypeAlong(text, { cps, startDelay, loop, paused })` types at 11 characters a second, each gap 35% faster or slower from a seeded generator, with a breath after commas and full stops, a longer one after a new line and now and then a pause between words. Capitals hold the shift on the other hand from just before the letter to just after, and a run of capitals keeps it down. It returns `typed`, `pressed`, `done`, `restart` and a `ref` that pauses it off screen. Under reduced motion the text is complete at once.

## Examples

A shortcut ringed, keys pressed, backlit, pale caps, PC modifiers, angled, without the function row, and at 320 px.

Example: Keyboard states. A shortcut ringed, keys pressed, backlit, pale caps, PC modifiers, angled, without the function row and at 320 px. Preview: https://chatcn.dev/preview/keyboard-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `width` | `number` | Default 640; capped at the container width |
| `variant` | `dark | light` | Dark caps on silver, or pale caps; default `dark` |
| `view` | `flat | angled` | Angled leans it back 38 degrees with side walls |
| `modifiers` | `mac | pc` | The bottom row; default `mac` |
| `fnRow` | `full | half | none` | Default `full` |
| `pressed` | `string[]` | Keys drawn pressed, by code |
| `highlight` | `string[]` | Keys ringed in the accent; also names them for screen readers |
| `listen` | `boolean` | Mirror the real keyboard; never prevents default |
| `backlight` | `number` | 0 to 1; legends glow and light shows in the gaps |
| `onKeyPress` | `(code) => void` | A pointer press on a key |
| `label` | `string` | Accessible name; default Keyboard |

The keyboard is a picture (`role="img"`): its keys are not focusable. It never mirrors on a right to left page, and in forced colors each cap keeps an outline.

Source: https://chatcn.dev/docs/components/keyboard