Skip to content

Register Hotkeys with @hotkey and HotkeyController in Lit

The @hotkey decorator registers a keyboard shortcut for a method while its element is connected. HotkeyController does the same for any callback, and accepts a hotkey and options that change. Both take the same hotkeys and options. To register hotkeys outside Lit elements, use the hotkey manager.

Chords and sequences

A hotkey is one of two kinds:

  • A chord is keys pressed together, such as 'Mod+S', or a single key, such as 'Escape'.
  • A sequence is chords pressed one after another, such as ['G', 'H']. Write it as an array of chords.
ts
@hotkey('Mod+K') // Mod and K together
openSearch() {}

@hotkey(['G', 'H']) // G, then H
goHome() {}

Every decorator, controller, and option in this guide accepts both kinds, and most examples use chords. Sequences covers how sequences match, the time allowed between steps, and sequences that start with the same chord.

Register a hotkey

Decorate the method that handles the hotkey with @hotkey:

ts
import { LitElement, html } from 'lit'
import { customElement } from 'lit/decorators.js'
import { hotkey } from '@tanstack/lit-hotkeys'

@customElement('my-editor')
class MyEditor extends LitElement {
  @hotkey('Mod+S')
  save() {
    saveDocument()
  }

  render() {
    return html`<textarea></textarea>`
  }
}

The method receives the KeyboardEvent and a context object. Type the context to match the hotkey, HotkeyChordCallbackContext for a chord:

ts
@hotkey('Mod+S')
save(event: KeyboardEvent, context: HotkeyChordCallbackContext) {
  console.log(context.hotkey) // 'Mod+S'
  console.log(context.parsedChord.modifiers) // ['Meta'] on macOS, ['Control'] elsewhere
}

The method runs with the element as this, so it reads the element's current properties. The decorator looks the method up each time the hotkey fires, so a subclass that overrides it runs the override. When the subclass also puts @hotkey on the override, the subclass's hotkeys replace the parent's for that method: @hotkey('K') on both registers K once, and @hotkey('J') on the override moves the method from K to J. To keep K and add J, put both on the override. @hotkey works with standard and legacy decorators. With standard decorators, it also works on #private methods.

Decorator arguments are fixed when the class is defined. When the hotkey or its options change, use HotkeyController and pass them as functions. The controller reads them when the element connects and after each update. When only the options change, it updates the existing registration. When the hotkey or its target changes, it replaces the registration:

ts
import { LitElement, html } from 'lit'
import { customElement, property } from 'lit/decorators.js'
import { HotkeyController } from '@tanstack/lit-hotkeys'
import type { Hotkey } from '@tanstack/lit-hotkeys'

@customElement('my-editor')
class MyEditor extends LitElement {
  @property({ attribute: false }) saveHotkey: Hotkey = 'Mod+S'
  @property({ type: Boolean }) readOnly = false

  private saveController = new HotkeyController(
    this,
    () => this.saveHotkey,
    () => this.save(),
    () => ({ enabled: !this.readOnly }),
  )

  save() {
    saveDocument()
  }

  render() {
    return html`<textarea></textarea>`
  }
}

Keep the values the functions read in reactive properties, so a change triggers an update. A controller callback receives HotkeyCallbackContext, which can belong to either kind of hotkey, so check context.kind before reading context.parsedChord.

That lets you register a hotkey the user chose, such as one from the hotkey recorder. To let users change hotkeys across your app and save their choices, see App Hotkeys.

Optional hotkeys

A hotkey of undefined, null, or '' registers nothing in HotkeyController, so an element can take an optional shortcut. The @hotkey decorator always needs a hotkey. When the hotkey changes to one of these values, its registration is removed. When it changes back to a hotkey, it registers again. Hints for no hotkey are hidden.

To keep a hotkey registered, and listed in devtools and cheat sheets, but stop it from firing, set enabled: false instead.

Write a chord as a string or an object

Write most chords as strings. Your editor autocompletes them, and TypeScript rejects a string that isn't a valid chord:

ts
@hotkey('Escape')
close() {}

@hotkey('Mod+S') // Command+S on macOS, Control+S elsewhere
save() {}

@hotkey('Mod+Alt+Shift+K') // Modifiers in order: Mod or Control, Alt, Shift, Meta
openCommandLog() {}

@hotkey('[KeyW]') // The W position, whatever the layout types there
moveUp() {}

@hotkey('Shift+Mod+S') // Type error: Mod comes first
saveAs() {}

Write a chord as a RawHotkeyChord object when you build it from data, such as a form with a checkbox for each modifier:

ts
@hotkey({ key: 'S', mod: true, shift: true }) // Same as 'Mod+Shift+S'
saveAs() {}

@hotkey({ code: 'KeyW' }) // Same as '[KeyW]'
moveUp() {}

private actionHotkey = new HotkeyController(
  this,
  () => ({ key: this.binding.key, mod: this.binding.mod, shift: this.binding.shift }),
  () => this.runAction(),
)

Sequence steps can be strings, objects, or a mix:

ts
@hotkey(['G', { key: 'H', shift: true }])
goToHistory() {}

Every chord and sequence step registers as a string, so context.hotkey shows 'Mod+Shift+S' for the first object above. Match a key's position instead explains codes such as [KeyW].

Hotkey options

Pass options as the decorator's second argument, or the controller's fourth:

ts
// Submit with Enter, even while focus is in a text field
@hotkey('Enter', { ignoreInputs: false })
submit() {}
OptionDefaultDescription
enabledtrueWhether the hotkey responds to key presses. See Enable and disable hotkeys.
targetdocumentWhere the hotkey listens. See Hotkey targets.
preventDefaulttrueStops the browser's own action, such as the Save dialog for Mod+S. 'always' also stops it while the hotkey can't fire.
stopPropagationtrueStops the key press from reaching other listeners
ignoreInputs'auto'Skips key presses that type in a text field or operate a focused control. See Typing in inputs does not trigger shortcuts.
ignoreDefaultPreventedfalseSkips a key press that another listener already handled with preventDefault(), such as a component library's dialog closing on Escape. See Component libraries that handle the same keys.
eventType'keydown'Fires on 'keydown' or 'keyup'
capturefalseRuns before the keyboard handlers of elements inside the target. See Hotkeys run after the page's own handlers.
requireResetfalseChords only. Fires once per press instead of again on each automatic repeat while the key is held.
timeout1000Sequences only. Milliseconds allowed between steps.
conflictBehavior'warn'What happens when the same hotkey is already registered. See Handle duplicate hotkeys.
scope'global'The scope the hotkey belongs to. See Add scopes.
platformDetectedThe platform that decides what Mod means: 'mac', 'windows', or 'linux'. Useful in tests.
metaNoneA name, a description, and a group. See Describe hotkeys with meta.

The defaults suit most hotkeys. Sensible Defaults explains why each one was chosen and when to change it. The HotkeyControllerOptions reference lists the types.

Lit has no provider for app-wide defaults, so pass options to each hotkey. To give several hotkeys the same options, share an options object. See Share options.

Chord and sequence options

Most options apply to both kinds of hotkey. requireReset applies only to chords, and timeout only to sequences. The decorator's option types follow the hotkey you pass, so TypeScript rejects options for the other kind:

ts
@hotkey('Escape', { requireReset: true }) // OK
close() {}

@hotkey(['G', 'G'], { timeout: 500 }) // OK
scrollToTop() {}

@hotkey('Escape', { timeout: 500 }) // Type error
cancel() {}

When the kind isn't known until runtime, such as a Hotkey loaded from settings, the decorator accepts only the options that both kinds share.

Register multiple hotkeys

Decorate one method for each hotkey. Each decorator registers its own hotkey:

ts
@customElement('my-editor')
class MyEditor extends LitElement {
  @hotkey('Mod+S')
  save() {}

  @hotkey('Mod+Z')
  undo() {}

  @hotkey('Mod+Shift+Z')
  redo() {}

  @hotkey(['G', 'G'], { timeout: 500 })
  scrollToTop() {}
}

Chords and sequences can sit side by side on the same element.

Share options

Keep options that several hotkeys share in one object. Pass it to each hotkey, or spread it to add more options:

ts
import type { HotkeyControllerChordOptions } from '@tanstack/lit-hotkeys'

// The formatting hotkeys toggle, so each fires once per press
const toggle: HotkeyControllerChordOptions = { requireReset: true }

@customElement('my-editor')
class MyEditor extends LitElement {
  @hotkey('Mod+B', toggle)
  toggleBold() {}

  @hotkey('Mod+I', toggle)
  toggleItalic() {}

  @hotkey('Mod+U', { ...toggle, meta: { name: 'Underline' } })
  toggleUnderline() {}
}

Build the list from data

Each decorator and controller registers one hotkey, and Lit has no API for a list whose length changes. When the list is known when the element is created, create one HotkeyController for each item in the constructor.

To add an item later, create its controller then. It registers right away while the element is connected. To remove an item, call its controller's dispose(), which unregisters the hotkey and removes the controller from the element. Lit's removeController alone leaves the hotkey registered, even after the element disconnects:

ts
import { LitElement } from 'lit'
import { customElement } from 'lit/decorators.js'
import { HotkeyController } from '@tanstack/lit-hotkeys'
import type { Hotkey } from '@tanstack/lit-hotkeys'

@customElement('tool-palette')
class ToolPalette extends LitElement {
  private tools = new Map<string, HotkeyController>()

  addTool(id: string, hotkey: Hotkey, run: () => void) {
    this.removeTool(id)
    this.tools.set(id, new HotkeyController(this, hotkey, run))
  }

  removeTool(id: string) {
    this.tools.get(id)?.dispose()
    this.tools.delete(id)
  }
}

The other Lit controllers, such as HotkeyHintController and HotkeyRecorderController, have dispose() too.

Enable and disable hotkeys

A hotkey is on while its element is connected. Most hotkeys need no other on and off logic, so put each one on the element it belongs to:

ts
@customElement('settings-dialog')
class SettingsDialog extends LitElement {
  // Registered only while the dialog is connected
  @hotkey('Escape')
  close() {
    this.dispatchEvent(new CustomEvent('close'))
  }

  render() {
    return html`<div role="dialog">...</div>`
  }
}

For a condition that doesn't match an element, pass enabled from a HotkeyController options function:

ts
private saveHotkey = new HotkeyController(this, 'Mod+S', () => this.save(), () => ({
  enabled: this.hasUnsavedChanges,
}))

A disabled hotkey stays registered, and changing enabled doesn't register it again. While disabled, it ignores key presses, so pressing Mod+S opens the browser's Save dialog. To keep the dialog closed, set preventDefault: 'always':

ts
private saveHotkey = new HotkeyController(this, 'Mod+S', () => this.save(), () => ({
  enabled: this.hasUnsavedChanges,
  preventDefault: 'always',
}))

With 'always', a hotkey that can't fire, because it's disabled or its scope is inactive or suspended, still prevents the browser's action. It doesn't stop propagation, and when another hotkey handles the key, that hotkey's options apply instead. Sequences treat 'always' as true. To apply it to several hotkeys, put it in a shared options object. We recommend it for most hotkeys, but not for hotkeys you turn off to hand the key back to the browser, such as Mod+C or Tab. See Hotkeys that can't fire leave the key alone.

enabled works the same way for sequences. A disabled sequence ignores its steps.

Single-key hotkeys, such as K, need a setting that lets users turn them off. Pass that setting as enabled. See Single-key shortcuts need an off switch.

When a group of hotkeys turns on and off together, put them in a scope instead of passing enabled to each one. A scope can turn off the hotkeys in a tab panel that stays connected, or suspend the rest of the page while a dialog is open. See Add scopes.

Hotkey targets

A hotkey listens on document by default, so it works wherever focus is on the page. Set target to an element, document, or window to listen somewhere else. For an element the hotkey's element renders, pass the options as a function on a HotkeyController:

ts
import { LitElement, html } from 'lit'
import { customElement } from 'lit/decorators.js'
import { createRef, ref } from 'lit/directives/ref.js'
import { HotkeyController } from '@tanstack/lit-hotkeys'

@customElement('my-panel')
class MyPanel extends LitElement {
  private panel = createRef<HTMLDivElement>()

  // Fires only while the panel, or something inside it, has focus
  private closeHotkey = new HotkeyController(this, 'Escape', () => this.closePanel(), () => ({
    target: this.panel.value,
  }))

  render() {
    return html`<div ${ref(this.panel)} tabindex="0">Panel content</div>`
  }
}

A null or undefined target waits until the element renders. Only an omitted target listens on document; set target: document to choose it explicitly.

The controller reads the options function when its element connects and after each of its element's updates. Render the target in the controller's own element, as above, so each render that adds or replaces the target also updates the hotkey.

When another element renders the target, such as a child that shows it from its own state, the controller's element doesn't update, so the hotkey keeps the old target or none. Either keep the element in a reactive property, for example set from an event the child dispatches, or call this.requestUpdate() on the controller's element after the target changes. You can also register the hotkey in the child with its own HotkeyController.

Note

An element receives key presses only while it, or something inside it, has focus. Give it a tabindex if it isn't focusable already.

While focus is inside the element, its hotkeys run before hotkeys on document. With the default stopPropagation: true, a document hotkey for the same key doesn't run at all. A sequence with a target advances only on key presses inside the target.

A target is not a scope

A target and a scope both limit when a hotkey works, in different ways:

  • A target decides which key presses the hotkey receives, based on where focus is.
  • A scope decides whether the hotkey is on, based on which parts of your app are active, such as an open dialog. Focus doesn't matter.

Use a target for hotkeys that belong to a focusable element, such as a list or a code editor. Use a scope for hotkeys that belong to a part of your app, whether or not it has focus. A hotkey can have both. See Add scopes.

Handle duplicate hotkeys

When you register a hotkey that is already registered on the same target, in the same scope, and for the same event type, TanStack Hotkeys logs a warning in development, and both callbacks run. Set conflictBehavior to change that:

  • 'warn' (default) logs a warning in development and keeps both registrations.
  • 'error' throws an error.
  • 'replace' unregisters the existing registration.
  • 'allow' keeps both without a warning.

The comparison resolves modifiers first, so Mod+S and Meta+S are duplicates on macOS. Two sequences are duplicates when all their steps match. Sequences that only start with the same chord, such as ['G', 'H'] and ['G', 'S'], are not duplicates.

The same hotkey in two different scopes is not a duplicate either. When both scopes are active, the scope with higher precedence handles the key.

Describe hotkeys with meta

Give a hotkey a name, a description, and a group with meta:

ts
@hotkey('Mod+S', {
  meta: { name: 'Save', description: 'Save the document', group: 'File' },
})
save() {}

meta doesn't change how a hotkey behaves. You can build a list of your app's shortcuts from it. See Cheat Sheet UI Helpers.