Moey extras
Keyboard shortcuts
Give buttons and links a shortcut with one attribute — and people who live in your app all day can press ? to see every shortcut on the page. Keys show the right way on each computer (Ctrl on Windows, ⌘ on a Mac), and plain keys never get in the way of typing.
Click here first (not in a field), then press c, e, / or g s — and ? for the list.
<button class="btn" data-moey-shortcut="c">
Compose <kbd class="shortcut-hint" data-moey-keys="c"></kbd>
</button>
<input class="form-control" type="search" placeholder="Search" data-moey-shortcut="/">
<a href="/inbox" data-moey-shortcut="g i" data-moey-shortcut-label="Inbox">Inbox</a>
<button data-moey-shortcut="mod+enter">Send</button>
Add shortcuts, step by step
- Put
data-moey-shortcuton the button or link the key should press. On a field, the key puts the cursor in it (/ for search is a favourite). - Write the keys as you'd say them:
c,/,mod+s(Ctrl on Windows and Linux, ⌘ on a Mac),shift+n,alt+1,esc— or a sequence with a space:g imeans g, then i. - Name it for the list if the button's text isn't clear enough:
data-moey-shortcut-label="Go to inbox". Group shortcuts withdata-moey-shortcut-group="Mail"on the button or any parent. - Show it where people see the button:
<kbd class="shortcut-hint" data-moey-keys="c"></kbd>inside it, in its tooltip, or in a menu item. - That's all — ? now lists it, screen readers hear it (
aria-keyshortcuts), and hidden or disabled buttons are skipped.
How keys behave
- Plain keys (c, /, g i) only work when you're not typing in a field — so typing "c" in a search box types a "c".
- Combinations with Ctrl / ⌘ / Alt work everywhere, even in a field — Ctrl Enter to send, Ctrl S to save.
- While a dialog is open only combinations work; the dialog's own keys come first.
- Pick keys that don't fight the browser: avoid Ctrl + W, T, N, L, R and Tab.
From JavaScript
For actions without a button:
const remove = Moey.shortcuts.add({
keys: 'shift+d',
label: 'Toggle dark mode',
group: 'View',
run: () => toggleDarkMode(),
});
remove(); // when it's no longer needed
Moey.shortcuts.open(); // the "?" sheet, from a help menu
Moey.shortcuts.list(); // [{ keys, label, group }, …]
The "?" sheet
It lists every shortcut that works right now, by group, with a search box. Turn it off with data-moey-shortcuts="false" on <html>, or open it from a help button with Moey.shortcuts.open(). On a phone it opens as a bottom sheet (tablets with keyboards use shortcuts too).
Reference
| Attribute / API | What it does |
|---|---|
data-moey-shortcut="c" · "mod+s" · "g i" | Press this button / link (a field: focus it) with these keys |
data-moey-shortcut-label | The name in the list (else aria-label or the text) |
data-moey-shortcut-group | The heading in the list, on the element or a parent ("General") |
kbd.shortcut-hint[data-moey-keys] | A quiet key hint inside a button or menu item |
data-moey-shortcuts="false" on <html> | No built-in ? sheet |
| Key names | mod (Ctrl / ⌘) · ctrl · alt · shift · meta · enter · esc · space · up down left right · letters, digits, symbols |
Moey.shortcuts | add({ keys, label, group, run }) → remove · open() · close() · list() |