data-hotkey
Platform-aware keyboard shortcut display. Add data-hotkey to any <kbd> element to render platform-appropriate key symbols.
Overview
The data-hotkey attribute enhances native <kbd> elements with platform-aware key rendering. On Mac, modifier keys display as symbols; on other platforms, as text labels with + separators.
How It Works
- Reads the
data-hotkeyattribute value (e.g.,"meta+k") - Detects the user's platform (Mac vs. other)
- Maps modifier names to platform-appropriate symbols or labels
- Generates a
<kbd>element for each key in the combo - Replaces the element's content with the styled key caps
- Sets
aria-labelwith the readable text
The original text content (e.g., "Ctrl+K") serves as a no-JS fallback. Nested <kbd> inside <kbd> is valid HTML for key combinations per the spec.
Attributes
| Attribute | Type | Description |
|---|---|---|
data-hotkey |
string | Key combination string. Keys separated by +. Supported modifiers: meta, alt, shift, ctrl. |
data-hotkey-init |
boolean | Set automatically to prevent double-enhancement. Do not set manually. |
Modifier Key Mapping
Modifier keys are automatically translated based on the user's platform:
| Key name | Mac | Windows/Linux |
|---|---|---|
meta |
⌘ | Ctrl |
alt |
⌥ | Alt |
shift |
⇧ | Shift |
ctrl |
⌃ | Ctrl |
Non-modifier keys are displayed in uppercase (e.g., k becomes K).
Examples
Common keyboard shortcuts rendered with platform-aware symbols:
Inline Usage
Works naturally inline with paragraph text:
Press Ctrl+K to open the search palette.
Use Ctrl+Shift+P for the command palette.
Common Use Cases
Keyboard Shortcuts Table
Display a reference table of available shortcuts:
| Action | Shortcut |
|---|---|
| Search | Ctrl+K |
| Save | Ctrl+S |
| Undo | Ctrl+Z |
| Redo | Ctrl+Shift+Z |
| Select all | Ctrl+A |
With Tooltips
Combine with <tooltip-wc> to show shortcuts on hover:
Dynamic Elements
Elements added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.
Accessibility
- Sets
aria-labelon the element with the full text content for screen readers - Uses semantic
<kbd>elements which are recognized by assistive technology - Nested
<kbd>inside<kbd>is valid HTML for key combinations per the spec - The original text content serves as a no-JS fallback
- On Mac, separators between keys are omitted since the symbols are self-explanatory; on other platforms,
+separators are included for clarity