Description: AlpineJS allows you to easily listen for specific key presses (e.g., .enter, .escape) or key combinations (e.g., .ctrl.enter) on input elements or globally on the window. This makes it straightforward to create intuitive keyboard-driven user interactions.
AlpineJS extends standard browser events like keydown and keyup with convenient modifiers for specific keys and modifier keys (Ctrl, Alt, Shift, Meta).
@ symbol (or x-on:). For keyboard events, you typically use @keydown or @keyup, followed by one or more key modifiers, and then an expression to execute.
<input @keydown.enter="submitForm()">
<div @keyup.escape.window="closeModal()"></div>
@keydown vs. @keyup:
@keydown: Fires when the key is first pressed down. This is often preferred for actions that should feel immediate, like submitting a form with Enter or navigating.@keyup: Fires when the key is released. This can be useful for actions that should occur after a key press is complete, or to avoid repeated actions if a key is held down (though not all keys trigger repeated `keydown` events for actions)..enter.escape (aliased as .esc).space.tab.arrow-up, .arrow-down, .arrow-left, .arrow-right.delete (handles both "Delete" and "Backspace" keys).backspace.home, .end, .page-up, .page-down@keydown.a="handleA", @keydown.shift.a="handleShiftA", @keydown.ctrl.1="handleCtrl1".For a comprehensive list, refer to the official AlpineJS documentation on event modifiers.
.ctrl: The Control key..shift: The Shift key..alt: The Alt key (Option key on macOS)..meta: The Meta key (Command key ⌘ on macOS, Windows key on Windows)..cmd: A convenient alias that maps to .meta on macOS systems and .ctrl on Windows/Linux systems. This is very useful for creating cross-platform shortcuts like "Cmd/Ctrl + S".
<button @keydown.cmd.s.prevent="save()">Save (Cmd/Ctrl+S)</button>
.ctrl.shift) generally doesn't matter.
<input @keydown.ctrl.shift.enter="doComplexAction()">
<input type="text" @keydown.enter="submitValue">
.window): Add the .window modifier to listen for key presses anywhere in the browser window, regardless of which element has focus. This is common for global shortcuts like closing a modal with Escape.
<div x-show="isModalOpen" @keydown.escape.window="isModalOpen = false">Modal Content</div>
.prevent, .stop):
.prevent: Calls event.preventDefault(), stopping the browser's default action for that key press (e.g., submitting a form on Enter, scrolling with arrow keys)..stop: Calls event.stopPropagation(), preventing the event from bubbling up to parent elements.<input @keydown.enter.prevent="customSubmit"> <!-- Prevents default form submission -->
keyup vs keydown inappropriately:
keydown fires when the key is pressed down, while keyup fires when it's released. For actions like submitting a form with 'Enter' or navigating with arrow keys, keydown often provides a more responsive feel. For actions that might be based on the final input after a key is held (though most action keys don't auto-repeat actions), or if you specifically want to act *after* the key interaction is complete, keyup might be more suitable. For text inputs, the input event is generally better for capturing value changes than keyup.
.window) firing too often or when not intended:
When you attach a keyboard listener to the .window (e.g., @keydown.window.escape="closeModal"), it will react to that key press regardless of what element is focused or what state your application is in. If you have multiple components listening for the same global key press (e.g., multiple modals that can be closed with Escape), they might all react. Ensure your component's logic correctly determines if it *should* act. For example, a modal should only close itself if it's currently open:
<div x-data="{ isOpen: false }">
<button @click="isOpen = true">Open Modal</button>
<div x-show="isOpen" @keydown.window.escape="if (isOpen) isOpen = false">
Modal Content (Press ESC to close if open)
</div>
</div>
Many key combinations (e.g., Ctrl+S/Cmd+S for Save, Ctrl+R/Cmd+R for Reload, Ctrl+T for New Tab, Alt+F4 or Cmd+Q for Close) are standard browser or operating system shortcuts. While AlpineJS's .prevent modifier can sometimes stop the browser's default action (e.g., @keydown.cmd.s.prevent="customSave"), it's not always guaranteed, especially for OS-level shortcuts. It's generally best to avoid overriding universally expected browser behavior unless absolutely necessary and clearly indicated to the user. If you need application-specific shortcuts, try to choose combinations that don't conflict with common browser/OS ones.
For keyboard events to be triggered on a non-input element (like a <div>), the element often needs to be focusable. You can make an element focusable by adding tabindex="0" to it. This is crucial if you want to capture arrow keys or other keys on custom interactive components.
Last action: None
Current filter:
Press Cmd+K (Mac) or Ctrl+K (Windows/Linux) to toggle the "Command Palette".
✨ Command Palette Visible! ✨
You can add quick actions here. Press Cmd/Ctrl+K again to hide.
Palette is currently hidden.
Press ESC key to close this modal.
Click here to focus, then use Up/Down arrow keys.