<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-keyboard/usage/ in the magma monorepo.
-->

# mds-keyboard - examples

> The API of this component: [`AGENTS.md`](AGENTS.md).

Correct and idiomatic ways to use the `<mds-keyboard>` component, ordered from most common to most specialized. The examples assume a working knowledge of the shared component rules in [`agents/conventions.md`](../../../../agents/conventions.md).

#### Single Key Display

The simplest use: one `<mds-keyboard-key>` child inside `<mds-keyboard>`. The `name` attribute selects a key from the built-in key dictionary; the component renders the physical-looking keycap with the key's alias (e.g. `F1`, `Ctrl`).

```html
<mds-keyboard>
  <mds-keyboard-key name="f1"></mds-keyboard-key>
</mds-keyboard>
```

#### Multi-Key Shortcut Combination

Slot two or more `<mds-keyboard-key>` elements. The component automatically inserts a `+` separator between consecutive keys, so you never need to add it manually.

```html
<mds-keyboard>
  <mds-keyboard-key name="control"></mds-keyboard-key>
  <mds-keyboard-key name="s"></mds-keyboard-key>
</mds-keyboard>
```

#### Specifying Key Side (Left / Right Modifier)

Use the side-specific `name` values (`controlleft`, `controlright`, `shiftleft`, `shiftright`, `commandleft`, `commandright`, etc.) when the shortcut must be typed with a specific hand. The keycap renders a small positional indicator.

```html
<!-- Only the left Shift key is accepted during a test -->
<mds-keyboard try>
  <mds-keyboard-key name="shiftleft"></mds-keyboard-key>
  <mds-keyboard-key name="d"></mds-keyboard-key>
</mds-keyboard>
```

#### Interactive Verification Mode (`try`)

Add the `try` attribute to let the user confirm the shortcut by typing it. A trigger button appears; clicking it puts the component into capture mode. The keys can be pressed in any order, all held down together: at the first key release the component reflects the outcome (`pass` / `fail`) on the `test` attribute and shows a localized tooltip.

```html
<mds-keyboard try>
  <mds-keyboard-key name="command"></mds-keyboard-key>
  <mds-keyboard-key name="z"></mds-keyboard-key>
</mds-keyboard>
```

#### Shortcut Reference Table

Embed `<mds-keyboard>` inside a `<mds-table>` to build a keyboard-shortcut reference sheet. Each row pairs the visual shortcut with its description.

```html
<mds-table>
  <mds-table-header>
    <mds-table-header-cell label="Combinazione"></mds-table-header-cell>
    <mds-table-header-cell label="Azione"></mds-table-header-cell>
  </mds-table-header>
  <mds-table-body>
    <mds-table-row>
      <mds-table-cell>
        <mds-keyboard>
          <mds-keyboard-key name="control"></mds-keyboard-key>
          <mds-keyboard-key name="c"></mds-keyboard-key>
        </mds-keyboard>
      </mds-table-cell>
      <mds-table-cell><mds-text>Copia</mds-text></mds-table-cell>
    </mds-table-row>
    <mds-table-row>
      <mds-table-cell>
        <mds-keyboard>
          <mds-keyboard-key name="control"></mds-keyboard-key>
          <mds-keyboard-key name="v"></mds-keyboard-key>
        </mds-keyboard>
      </mds-table-cell>
      <mds-table-cell><mds-text>Incolla</mds-text></mds-table-cell>
    </mds-table-row>
    <mds-table-row>
      <mds-table-cell>
        <mds-keyboard>
          <mds-keyboard-key name="control"></mds-keyboard-key>
          <mds-keyboard-key name="z"></mds-keyboard-key>
        </mds-keyboard>
      </mds-table-cell>
      <mds-table-cell><mds-text>Annulla</mds-text></mds-table-cell>
    </mds-table-row>
  </mds-table-body>
</mds-table>
```

#### Reading the `test` Outcome Attribute

When `try` is set the `test` attribute is reflected onto the host as `"pass"` or `"fail"` after each attempt. Use it as a CSS attribute selector, or observe the attribute from JavaScript to drive follow-on UI (the component emits no event).

```html
<mds-keyboard id="shortcut-ctrl-s" try>
  <mds-keyboard-key name="control"></mds-keyboard-key>
  <mds-keyboard-key name="s"></mds-keyboard-key>
</mds-keyboard>
```

```javascript
const keyboard = document.querySelector('#shortcut-ctrl-s');
new MutationObserver(() => {
  if (keyboard.getAttribute('test') === 'pass') {
    console.log('Combinazione corretta!');
  }
}).observe(keyboard, { attributes: true, attributeFilter: ['test'] });
```

#### Styling Customization

Adjust colors and padding only through the public `--mds-keyboard-*` CSS custom properties; the keys take their colours from the theme. Use the semantic color roles via `rgb(var(--magma-<role>))` ([`agents/color.md`](../../../../agents/color.md)) so dark mode continues to work.

```css
.custom-keyboard mds-keyboard {
  --mds-keyboard-background: rgb(var(--magma-surface-inverse));
  --mds-keyboard-color: rgb(var(--magma-on-inverse));
  --mds-keyboard-padding: calc(var(--spacing) * 300);
}
```
