# Keyboard Shortcut

- **Section:** Components
- **URL:** https://refast.fastapicloud.dev/docs/components/keyboard-shortcut
- **Markdown:** https://refast.fastapicloud.dev/docs/components/keyboard-shortcut.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/components/utility/keyboard_shortcut.py
- **Live examples:** the widgets on this page need a browser; open the URL above to try them.
- **Applies to:** refast 0.12.2

An invisible component that captures keyboard shortcuts globally and fires
server callbacks.  It renders nothing to the DOM — place it anywhere in the
component tree and it will listen on the `document` for the registered key
combinations.

```python
from refast.components import KeyboardShortcut

KeyboardShortcut(
    shortcuts={
        "ctrl+k": ctx.callback(open_search),
        "ctrl+shift+n": ctx.callback(new_item),
        "escape": ctx.callback(close_panel),
    }
)
```

```python
KeyboardShortcut(
    shortcuts={
        "ctrl+alt+1": ctx.callback(handle_shortcut),
        "ctrl+alt+2": ctx.callback(handle_shortcut),
    },
    enabled=True,
    bubble=False,
)
```

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `shortcuts` | `dict[str, Callback]` | **required** | Mapping of shortcut string → server callback |
| `priority` | `int` | `0` | Higher values are handled first when multiple components claim the same combo |
| `bubble` | `bool` | `False` | When `True`, propagation continues to lower-priority handlers after this one fires |
| `prevent_default` | `bool` | `True` | Suppress the browser's native action for the combo (e.g. prevents Ctrl+S from opening the save dialog) |
| `enabled` | `bool` | `True` | Disable the shortcuts without removing the component from the tree |
| `id` | `str \| None` | `None` | Unique element ID |

## Shortcut format

Shortcut strings use the form `"modifier+modifier+key"`.  Modifiers are
case-insensitive and can appear in any order:

| Modifier token | Aliases |
|----------------|---------|
| `ctrl` | `control` |
| `shift` | — |
| `alt` | — |
| `meta` | `cmd`, `command`, `win` |

The key should be a single character (`a`–`z`, `0`–`9`) or a named key
(`enter`, `escape`, `space`, `arrowup`, `arrowdown`, `arrowleft`, `arrowright`, `tab`, `f1`–`f12`, …).

```python
# Examples
"ctrl+k"
"ctrl+shift+n"
"alt+f4"
"meta+k"          # ⌘K on macOS
"escape"
"f5"
```

## Multiple shortcuts in one component

All shortcuts in a single `KeyboardShortcut` share the same `priority`,
`bubble`, and `enabled` settings.  If you need different settings per
shortcut, use multiple components:

```python
# High-priority, no bubbling — captures Ctrl+K exclusively
KeyboardShortcut(
    shortcuts={"ctrl+k": ctx.callback(open_search)},
    priority=10,
    bubble=False,
)

# Lower priority — only fires if no higher-priority handler claims the combo
KeyboardShortcut(
    shortcuts={"ctrl+k": ctx.callback(log_search_attempt)},
    priority=0,
    bubble=False,
)
```

## Priority & bubbling

When two or more `KeyboardShortcut` components register the same combo:

1. Handlers run in **descending priority** order (highest first).
2. After a handler fires, propagation **stops** unless `bubble=True` on
   that component.
3. If `bubble=True`, the next handler in priority order also runs.

```python
# Both handlers fire for Ctrl+Z because the first sets bubble=True
KeyboardShortcut(
    shortcuts={"ctrl+z": ctx.callback(analytics_undo_track)},
    priority=20,
    bubble=True,   # keep going after this
)
KeyboardShortcut(
    shortcuts={"ctrl+z": ctx.callback(do_undo)},
    priority=10,
    bubble=False,
)
```

## Input field guard

Shortcuts that have **no** `Ctrl`, `Alt`, or `Meta` modifier are silently
ignored while a text `<input>`, `<textarea>`, or `contenteditable` element is
focused — so normal typing is never intercepted.

Shortcuts *with* a global modifier (Ctrl, Alt, Meta) fire regardless of focus.

## Dynamic enable / disable

Toggle shortcuts at runtime without unmounting the component:

```python
KeyboardShortcut(
    shortcuts={"ctrl+s": ctx.callback(save)},
    enabled=ctx.state.get("editing", False),
)
```

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
