# Shortcuts

> A searchable list of key bindings, grouped. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/shortcuts/

:::tip[In plain words]
A searchable list of all the keyboard shortcuts of an app.
:::

A searchable, grouped list of key bindings for a Keyboard shortcuts dialog. `groups` are `(title, [(action, keys), ...])` pairs.

`ui.shortcuts` · Category: [Code and tools](https://albakore.github.io/pyzed/library/#code-and-tools) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#shortcuts)

**Code:**

```python
pz.container(ui.shortcuts([
    ("File", [("Save", "ctrl-s"), ("Open file", "ctrl-p")]),
    ("View", [("Command palette", "ctrl-shift-p"), ("Toggle left dock", "ctrl-b")]),
], on_query=on_query, height=240), width=420)
```

## When to use

- Inside a `ui.modal` opened from a Help menu or with Ctrl+K Ctrl+S.

## Usage

```python
import pyzed as pz
from pyzed import ui

pz.container(ui.shortcuts([
    ("File", [("Save", "ctrl-s"), ("Open file", "ctrl-p")]),
    ("View", [("Command palette", "ctrl-shift-p"), ("Toggle left dock", "ctrl-b")]),
], on_query=on_query, height=240), width=420)
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Options

```python
pz.container(ui.shortcuts([("File", [("Save", "ctrl-s"), ("Open", "ctrl-o")])], on_query=on_query, height=130),
             width=360)
```

```python
pz.container(ui.shortcuts([("View", [("Command palette", "ctrl-shift-p"), ("Zoom in", "ctrl-=")])],
                          query="pal", on_query=on_query, height=110), width=360)
```

## Events

Shortcuts has **1 event of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| Event | Handler gets | When |
| --- | --- | --- |
| `on_query` | `(text)` | Adds a search box. |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Help(pz.App):
    def __init__(self):
        self.query = ""

    def view(self):
        return ui.shortcuts([("File", [("Save", "ctrl-s"), ("Open file", "ctrl-p")])],
                            query=self.query, on_query=lambda q: setattr(self, "query", q))  # adds search
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Search:
    query: str

class Help(pz.App):
    query = ""

    def update(self, message):
        match message:
            case Search(query):
                self.query = query

    def view(self):
        return ui.shortcuts([("File", [("Save", "ctrl-s")])], query=self.query, on_query=Search)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    query: str = ""

def view(state, set_state):
    return ui.shortcuts([("File", [("Save", "ctrl-s"), ("Open file", "ctrl-p")])],
                        query=state.query, on_query=lambda q: set_state(query=q))
```

## Properties and API

### `ui.shortcuts`

```python
shortcuts(groups, *, query='', on_query=None, height=380, theme=None)
```

A searchable list of key bindings, grouped (for a Keyboard shortcuts dialog).

| Property | Default | Description |
| --- | --- | --- |
| `groups` | *required* | `[(title, [(label, keys), ...]), ...]`, keys like `"ctrl-s"`. |
| `query` | `""` | Filter text. |
| `height` | `380` | Height of the list. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_query` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#shortcuts)

## Related

- [Kbd](https://albakore.github.io/pyzed/library/kbd/): Keyboard shortcuts drawn as keys, with symbols on macOS.
- [Keybinding hint](https://albakore.github.io/pyzed/library/keybinding-hint/): A muted “Open file Ctrl P” hint.
- [About](https://albakore.github.io/pyzed/library/about/): An About box with icon, version, details and links.
