Hotkeys Rails
Keyboard shortcuts for Hotwire apps. No dependencies. No configuration. Just HTML.
Installation
Add to your Gemfile:
gem "hotkeys-rails"Run:
bundle install
rails generate hotkeys_rails:installUsage
<%= 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).
Visual Hints
<%= link_to cards_path, hotkey: :c do %>
Add a card <%= hotkey_hint(:c) %>
<% end %>Labels for Tooltips
<%= button_tag "Save",
title: "Save (#{hotkey_label(:ctrl, :enter)})",
hotkey: [:ctrl, :enter] %>Focus Instead of Click
<%= text_field_tag :search, data: hotkey(:f, action: :focus) %>Works with Other Data Attributes
<%= link_to "Edit", edit_path,
hotkey: :e,
data: { turbo_frame: "modal" } %>Helpers
-
hotkey(*keys)- Returns data attributes for Stimulus controller -
hotkey_label(*keys)- Platform-aware label (⌘ on Mac, Ctrl+ elsewhere) -
hotkey_hint(*keys)- Renders<kbd>element with hide-on-touch class
How It Works
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) %>License
MIT