npm install @mbolli/datastar-attribute-on-keys
<script type="importmap">
{
"imports": {
"datastar": "https://cdn.jsdelivr.net/gh/starfederation/datastar@v1.0.0-RC.6/bundles/datastar.js"
}
}
</script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@mbolli/datastar-attribute-on-keys@1/dist/index.js"></script>
Interactive demo showcasing keyboard event binding with key combinations and multiple keys
The data-on-keys attribute allows you to bind keyboard events to Datastar actions with powerful key combinations and modifiers.
data-on-keys[:key-spec][__modifier1][__modifier2]="action"
The key-spec defines which keys trigger the action. It can be:
space, enter, escape, tab, f1, actrl-k, alt-enter, shift-space, meta-s, ctrl-shift-zenter.tab, escape.f1.ctrl-q (any of these will trigger)Common key aliases are automatically mapped to standard JavaScript key names:
space → Space characterenter / return → Enterescape / esc → Escapetab → Tabbackspace → Backspacedelete / del → Deleteup → ArrowUpdown → ArrowDownleft → ArrowLeftright → ArrowRightpageup → PageUppagedown → PageDown
Note: Function keys (f1-f12), letter keys (a-z), and other standard keys work as expected.
__el - Binds to the element itself (requires focus)preventDefault())__noprevent - Allows default browser behavior__stop - Stops event propagation (stopPropagation())keydown__up - Triggers on keyup__capture - Use capture phase__passive - Passive event listener__once - Remove listener after first trigger<!-- 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 behavior -->
<div data-on-keys:tab__noprevent="logTab()"></div>
<!-- Multiple triggers -->
<div data-on-keys:escape.f1.ctrl-h="showHelp()"></div>
Uses data-on-keys:space to increment counter on spacebar (prevents page scroll by default).
Press Space to increment counter:
Uses data-on-keys:ctrl-k for a global keyboard shortcut (window is now default).
Press Ctrl + K anywhere to toggle mode:
Uses data-on-keys:enter.tab to trigger on multiple keys.
Press Enter or Tab to show message:
Uses data-on-keys:f1 to open modal and data-on-keys:escape to close it (window is now default).
Press F1 anywhere to open modal, Escape to close it:
Uses data-on-keys to capture any key press globally (prevents default to avoid conflicts).
Press any key anywhere on the page to see it captured:
Uses data-on-keys:ctrl-enter__el to capture Ctrl+Enter key only when the input has focus.
Focus this input and press Ctrl + Enter (only works when input is focused):
Uses data-on-keys:tab__noprevent to capture Tab key but still allow normal tab navigation.
Tab through these inputs - the counter will increment but tab navigation still works:
Uses data-on-keys:space__up to trigger on key release instead of key press.
Press and hold Space, then release to see the difference between keydown and keyup:
Tip: Hold space to see keydown fire repeatedly, then release to see keyup fire once.