Keyboard shortcuts for Hotwire apps. No dependencies. No configuration. Just HTML.
Add to your Gemfile:
gem "hotkeys-rails"Run:
bundle install
rails generate hotkeys_rails:install<%= link_to "Back", root_path, hotkey: :esc %>
<%= button_to "New Card", cards_path, hotkey: :c %>
<%= button_tag "Save", hotkey: [:ctrl, :enter] %>The :ctrl modifier binds both Ctrl (Windows/Linux) and Cmd (Mac).
<%= link_to cards_path, hotkey: :c do %>
Add a card <%= hotkey_hint(:c) %>
<% end %><%= button_tag "Save",
title: "Save (#{hotkey_label(:ctrl, :enter)})",
hotkey: [:ctrl, :enter] %><%= text_field_tag :search, data: hotkey(:f, action: :focus) %><%= link_to "Edit", edit_path,
hotkey: :e,
data: { turbo_frame: "modal" } %>hotkey(*keys)- Returns data attributes for Stimulus controllerhotkey_label(*keys)- Platform-aware label (⌘ on Mac, Ctrl+ elsewhere)hotkey_hint(*keys)- Renders<kbd>element with hide-on-touch class
This gem overrides link_to, button_to, and button_tag to support the :hotkey option. When you use hotkey: :esc, it extracts the option and merges the Stimulus data attributes into the element.
If you prefer explicit control, use the hotkey() helper directly:
<%= link_to "Back", root_path, data: hotkey(:esc) %>MIT