Utility hotkey
All controllers

Hotkey

Binds keyboard shortcuts to click or focus an element. Automatically ignores keystrokes typed inside form controls, editable content, and rich-text editors.

Identifier: hotkey
Loaded by: auto-loaded after php artisan hotwire:install; publish only to customize with php artisan hotwire:controllers hotkey.

Requirements

  • No external dependencies.
  • Ships with _composition.js; publishing the controller publishes this helper too.

Actions

Action Description
hotkey#click Clicks the controller element when the key is pressed
hotkey#focus Focuses the controller element when the key is pressed

Both actions are no-ops when:

  • The element has pointer-events: none (e.g. a disabled button styled via CSS)
  • The key event originated inside an <input>, <textarea>, <select>, editable [contenteditable], <lexxy-editor>, or .ProseMirror editor. Explicit [contenteditable="false"] regions remain eligible for shortcuts.
  • The user is composing text with an IME
  • The event was already prevented by another handler

Basic Usage — keyboard shortcut for a button

HTML
<button
    data-controller="hotkey"
    data-action="keydown.n@window->hotkey#click"
>
    New post
</button>

Pressing n anywhere on the page (except inside a text field) clicks the button.

Put data-controller="hotkey" on the element you want clicked — not on a common ancestor. The click/focus actions operate on this.element, which Stimulus resolves to the closest ancestor carrying data-controller="hotkey". If you mount it on <body> and put the action on a child <a>, the controller will dutifully click the <body> — silent no-op. There's no registry pattern here; each element owns its own shortcut.

HTML
{{-- ❌ Won't work: this.element resolves to <body> --}}
<body data-controller="hotkey">
    <a data-action="keydown.n@window->hotkey#click" href="…">New</a>
</body>

{{-- ✓ Works: this.element is the <a> --}}
<body>
    <a
        data-controller="hotkey"
        data-action="keydown.n@window->hotkey#click"
        href="…"
    >New</a>
</body>

Focus a search input

HTML
<input
    type="search"
    name="q"
    placeholder="Search… (/)"
    data-controller="hotkey"
    data-action="keydown.slash@window->hotkey#focus"
/>

Multiple shortcuts on one element

HTML
<a
    href="/dashboard"
    data-controller="hotkey"
    data-action="keydown.g@window->hotkey#click keydown.h@window->hotkey#click"
>
    Dashboard
</a>

With modifier keys

Stimulus action descriptors support modifier keys via filters:

HTML
<button
    data-controller="hotkey"
    data-action="keydown.ctrl+k@window->hotkey#click"
>
    Command palette
</button>

Combining with Turbo

Because the controller clicks or focuses the real element, it works transparently with Turbo Drive links and Turbo Frame forms — no extra wiring needed.

HTML
<a
    href="/posts/new"
    data-turbo-frame="modal"
    data-controller="hotkey"
    data-action="keydown.c@window->hotkey#click"
>
    Compose
</a>