> For the complete documentation index, see [llms.txt](https://jscula.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jscula.gitbook.io/docs/events/onkeycombo.md).

# onKeyCombo()

Triggers callback when a keyboard combination is pressed.

```javascript
onKeyCombo('ctrl+s', (e) => { e.preventDefault(); saveDraft(); });
```

## `onKeyCombo(combo, callback)`

#### Description

Triggers callback when a **keyboard combination** is pressed.

#### Example

```javascript
onKeyCombo("shift+F", () => {
    console.log("Shift + F pressed");
});
```

`'ctrl+s'`

```javascript
document.onKeyCombo('ctrl+s', (e) => { e.preventDefault(); saveDraft(); });
```

`'alt+1'`

```javascript
onKeyCombo('alt+1', () => { setFontSize(14); });
```

`'shift+F'`

```javascript
onKeyCombo('shift+F', () => { $('#search').focus(); });
```

#### Supported patterns

* `"ctrl+s"`
* `"cmd+shift+k"`
* `"alt+arrowup"`
* `"ctrl+alt+delete"` (not recommended 😂)
* `"shift+F"`
* ...etc

Key combinations are case-insensitive.

### Use cases

Use `onKeyCombo()` to:

* Save a draft without leaving the keyboard.
* Focus a search field quickly.
* Close a modal with the Escape key.

### More examples

Close an open modal with Escape:

```javascript
onKeyCombo('escape', () => {
  $('#modal').hide();
});
```

Focus search with a keyboard shortcut:

```javascript
onKeyCombo('ctrl+k', (e) => {
  e.preventDefault();
  $('#search').focus();
});
```

{% hint style="info" %}
Call `preventDefault()` when a shortcut replaces a browser or operating-system action.
{% endhint %}
