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 characterenter/return→ Enterescape/esc→ Escapetab→ Tabbackspace→ Backspacedelete/del→ Delete
Punctuation
question→ ?slash→ /plus→ +minus→ -period→ .comma→ ,underscore→ _
Navigation keys
up→ ArrowUpdown→ ArrowDownleft→ ArrowLeftright→ ArrowRightpageup→ PageUppagedown→ 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 onkeyup
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.500msor__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 exampledata-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>
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.
Key combination: Ctrl+K
Uses data-on-keys:ctrl-k for a global keyboard shortcut.
Press Ctrl + K anywhere to toggle the mode.
Multiple keys: Enter or Tab
Uses data-on-keys:enter.tab to trigger on either key.
Press Enter or Tab to update the message.
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.
Global key monitor
Uses data-on-keys without a key to capture every key press globally (prevents the default to avoid conflicts).
Press any key anywhere on the page.
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.
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.
Keyup events
Uses data-on-keys:space__up to trigger on key release instead of key press.
Press and hold Space, then release it.
Holding Space fires keydown repeatedly; releasing it fires keyup once.
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.500msor__delay.1s. - Debounce waits until the keys stop, useful for search inputs. Supports
__debounce.500ms__leadingand__debounce.500ms__notrailing. - Throttle limits the rate. Supports
__throttle.500ms__trailingand__throttle.500ms__noleading. - View transition wraps the state change in the View Transitions API.
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.
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.