New releases

Hotwire Club tooling is now open-source

Explore the agentic skills pack and the MCP server for building assistant workflows.

Stimulus - KeyboardEvent 101

Use Stimulus' built in KeyboardEvent functionality to handle basic hotkeys.

View Solution on Patreon
Stimulus - KeyboardEvent 101

Premise

If you wanted to add convenient keyboard shortcuts to your application, you used to have to fall back to third party libraries like hotkeys.js. Since a while ago, you can now leverage Stimulus’ native KeyboardEvent filter functionality to achieve the same result - at least for more basic use cases.

Challenge

Implement a way to dynamically add/remove input fields, as well as alter their types, using hotkeys:

Here are a few suggestions:

  • alt+up/down to add or remove inputs (ensure that you clone the enclosing div!)
  • ctrl+0/9 to cycle through the valid types. My suggestion would be to use value change callbacks for this again.

Make sure to disallow the removal of non-empty inputs, and focus on the newly added one in case of insertion, or the previous element in case of removal.

Managing Form Fields Dynamically

Caveats

  • make sure to leave at least one input!
  • you cannot override any system shortcuts (and it’s also an accessibility antipattern)

Teaser

In Stimulus, how could you respond to a new input element being added, or one being removed?

This is The Hotwire Club

46 hands-on challenges with detailed solutions, published biweekly since 2023. Subscribe to access all solutions and join the Discord community.

Subscribe on Patreon

More from

Turbo Streams - Custom Stream Actions - pushState
14 April 2026

Synchronize browser history with Turbo Stream responses using a custom push_state action, a Stimulus controller, and the popstate event.

Turbo Frames - Chained Selects
24 March 2026

Build dependent dropdown menus that update dynamically using Turbo Frames and a small Stimulus controller.

Stimulus - Web Share API
25 November 2025

Use the native browser web sharing capabilities from Stimulus

Cookies
essential