Datastar plugin

data-on-keys

Bind keyboard events to Datastar expressions, with key combinations, several keys per binding and timing modifiers.

Guide

Usage

The data-on-keys attribute binds keyboard events to Datastar expressions, with key combinations and modifiers.

Basic syntax

data-on-keys[:key-spec][__modifier1][__modifier2]="action"

Key specifications

The key-spec defines which keys trigger the action. It can be:

  • Single keys: space, enter, escape, tab, f1, a
  • Key combinations: ctrl-k, alt-enter, shift-space, meta-s, ctrl-shift-z
  • Multiple options: enter.tab, escape.f1.ctrl-q (any of these triggers)
  • Sequences: g_q, ctrl-k_ctrl-s (one key after the other, within 1500 ms or __within.{time})
  • Punctuation: ?, question, slash (matches with or without Shift)
  • Any key: omit the key-spec to capture every key press

Key name mapping

Common aliases map to the standard JavaScript key names:

Special keys

  • space → Space character
  • enter / return → Enter
  • escape / esc → Escape
  • tab → Tab
  • backspace → Backspace
  • delete / del → Delete

Punctuation

  • question → ?
  • slash → /
  • plus → +
  • minus → -
  • period → .
  • comma → ,
  • underscore → _

Navigation keys

  • up → ArrowUp
  • down → ArrowDown
  • left → ArrowLeft
  • right → ArrowRight
  • pageup → PageUp
  • pagedown → PageDown

Function keys (f1 to f12), letter keys (a to z) and other standard keys work as expected.

Modifiers

Target

  • Default: binds to window (global)
  • __el: binds to the element itself (needs focus)
  • In fields: while a form field or contenteditable element has focus, keys that type a character (no Ctrl, Alt or Meta) do not fire
  • __typing: fires typed characters in fields too
  • __notyping: skips every key while a field has focus

Behaviour

  • Default: prevents the browser default (preventDefault())
  • __noprevent: allows the browser default
  • __stop: stops propagation (stopPropagation())

Event

  • Default: triggers on keydown
  • __up: triggers on keyup

Listener options

  • __capture: use the capture phase
  • __passive: passive listener
  • __once: remove the listener after the first trigger

Timing modifiers

Control when and how often the expression runs:

  • __delay.{time}: postpone by a fixed time, for example __delay.500ms or __delay.1s
  • __debounce.{time}: wait until the keys stop. Options __leading, __notrailing, for example __debounce.300ms__leading
  • __throttle.{time}: limit the rate. Options __trailing, __noleading, for example __throttle.1s__trailing

Times are given in milliseconds (ms) or seconds (s).

View transitions

  • __viewtransition: wraps the state change in the View Transitions API, for example data-on-keys:space__viewtransition="$counter++"

Browsers without the View Transitions API ignore the modifier.

Common examples

<!-- Global shortcuts -->
<div data-on-keys:ctrl-s="saveDocument()"></div>
<div data-on-keys:escape="closeModal()"></div>

<!-- Element-specific -->
<input data-on-keys:enter__el="submitForm()">
<textarea data-on-keys:ctrl-enter__el="submit()"></textarea>

<!-- Allow default behaviour -->
<div data-on-keys:tab__noprevent="logTab()"></div>

<!-- Multiple triggers -->
<div data-on-keys:escape.f1.ctrl-h="showHelp()"></div>

<!-- Sequences and punctuation, skipped while typing in a field -->
<div data-on-keys:g_h="goHome()"></div>
<div data-on-keys:question="showHelp()"></div>

<!-- Not in fields at all, or in fields too -->
<div data-on-keys:ctrl-k__notyping="openPalette()"></div>
<input data-on-keys__el__typing="$keys++">

<!-- Timing modifiers -->
<div data-on-keys:enter__delay.500ms="$message = 'Delayed!'"></div>
<input data-on-keys__debounce.300ms__el__typing="$search($event.target.value)">
<div data-on-keys:space__throttle.1s="$counter++"></div>

<!-- View transitions -->
<div data-on-keys:space__viewtransition="$counter++"></div>
Test 1

Single key: Space counter

Uses data-on-keys:space to increment a counter on the space bar (and prevents the page from scrolling).

Press Space to increment the counter.

Test 2

Key combination: Ctrl+K

Uses data-on-keys:ctrl-k for a global keyboard shortcut.

Press Ctrl + K anywhere to toggle the mode.

Mode:
Test 3

Multiple keys: Enter or Tab

Uses data-on-keys:enter.tab to trigger on either key.

Press Enter or Tab to update the message.

Test 4

Global events: modal control

Uses data-on-keys:f1 to open a modal and data-on-keys:escape to close it.

Press F1 anywhere to open the modal, Escape to close it.

Keyboard-controlled modal

This modal opens with F1 and closes with Escape or the button below.

Test 5

Global key monitor

Uses data-on-keys without a key to capture every key press globally (prevents the default to avoid conflicts).

Last key pressed:

Press any key anywhere on the page.

Test 6

Element-specific binding

Uses data-on-keys:ctrl-enter__el to capture Ctrl+Enter only while the input has focus.

Focus the input and press Ctrl + Enter.

Test 7

Allow default behaviour

Uses data-on-keys:tab__noprevent to count Tab presses while tab navigation keeps working.

Tab through these inputs: the counter goes up and the focus still moves.

Test 8

Keyup events

Uses data-on-keys:space__up to trigger on key release instead of key press.

Keydown events
Keyup events

Press and hold Space, then release it.

Holding Space fires keydown repeatedly; releasing it fires keyup once.

Test 9

Timing modifiers and view transitions

Delay, debounce, throttle and view transitions on keyboard events.

1. No timing modifier: data-on-keys:1

Press 1 to increment immediately.


2. Delay: data-on-keys:2__delay.500ms

Press 2: the counter increments after 500 ms.


3. Debounce: data-on-keys:3__debounce.500ms

Press 3 several times quickly: the counter increments once, 500 ms after the last press.


4. Throttle: data-on-keys:4__throttle.500ms

Hold 4: the counter increments at most once every 500 ms.


5. View transition: data-on-keys:5__viewtransition

Press 5 to increment with a view transition, where the browser supports it.

Timing modifiers

  • Delay postpones by a fixed time: __delay.500ms or __delay.1s.
  • Debounce waits until the keys stop, useful for search inputs. Supports __debounce.500ms__leading and __debounce.500ms__notrailing.
  • Throttle limits the rate. Supports __throttle.500ms__trailing and __throttle.500ms__noleading.
  • View transition wraps the state change in the View Transitions API.
Test 10

Sequences, punctuation and typing

While a field has focus, keys that type a character do not fire, unless the binding has __typing. Other keys fire as usual, unless the binding has __notyping.

K or ?, default
J, with __typing
Escape, with __notyping
G, then D

Press K or ? (with or without Shift, depending on your layout), J, Escape, or G and then D within 1500 ms.

Now press the same keys in this field: only J counts, Escape does not either, and the characters appear in the field.