Keyboard shortcuts
Give any button a key with data-momo-shortcut, show keys that match the person's computer (⌘ on a Mac, Ctrl elsewhere), and let people press ? for the list of everything they can press.
Keys
Write <kbd data-momo-keys="mod+k"></kbd> and Momo draws the right keys. mod means ⌘ on a Mac and Ctrl everywhere else. Steps with a space are a sequence: g d means press G, then D. A single key is just <kbd>Esc</kbd>.
Search · Save · Redo · Go to calendar · Move up · Help ?
small · plain
<p>Search <kbd data-momo-keys="mod+k"></kbd> · Save <kbd data-momo-keys="mod+s"></kbd> · Redo <kbd data-momo-keys="mod+shift+z"></kbd> · Go to calendar <kbd data-momo-keys="g c"></kbd> · Move up <kbd data-momo-keys="alt+up"></kbd> · Help <kbd>?</kbd></p>
<p><kbd class="kbd-sm" data-momo-keys="mod+enter"></kbd> small · <kbd class="kbd-plain" data-momo-keys="mod+enter"></kbd> plain</p>Buttons with shortcuts
Pressing the key clicks the button (on a field, it puts the cursor in it). The button flashes so people see what happened. Try N, , / and on this page.
<div class="form-field">
<label class="form-label" for="sc-search">Filter tasks</label>
<input class="form-control" id="sc-search" type="search" placeholder="Press / to jump here" data-momo-shortcut="/" data-momo-shortcut-desc="Filter tasks" data-momo-shortcut-group="Tasks">
</div>
<div class="btn-row" data-momo-shortcut-group="Tasks">
<button class="btn btn-brand" type="button" data-momo-shortcut="n" data-momo-tooltip="New task" data-momo-toast="New task" data-momo-toast-text="Opened with N">
<i class="mi mi-plus" aria-hidden="true"></i> New task
</button>
<button class="btn" type="button" data-momo-shortcut="mod+s" data-momo-shortcut-desc="Save changes" data-momo-toast="Saved" data-momo-toast-color="green">
Save <kbd data-momo-keys="mod+s"></kbd>
</button>
<button class="btn btn-ghost" type="button" data-momo-shortcut="g c" data-momo-shortcut-group="Go to" data-momo-shortcut-desc="Go to calendar" data-momo-toast="Calendar" data-momo-toast-text="Opened with G then C">
<i class="mi mi-calendar" aria-hidden="true"></i> Calendar
</button>
</div>A shortcut without a modifier (N, /, g c) does nothing while someone is typing in a field. Shortcuts with or Ctrl still work there. While a dialog is open, only the shortcuts inside it (and ?) work. A tooltip on the button shows its keys, and aria-keyshortcuts is added for screen readers.
Shortcut list
Put an empty <dialog data-momo-shortcuts-help> on the page: pressing ? opens it with every shortcut, grouped (by data-momo-shortcut-group) and searchable. Press ? now, or use the button.
<button class="btn" type="button" data-momo-dialog="sc-help"><i class="mi mi-keyboard" aria-hidden="true"></i> Keyboard shortcuts <kbd class="kbd-plain">?</kbd></button>
<dialog class="dialog dialog-lg" id="sc-help" data-momo-shortcuts-help aria-labelledby="sc-help-title">
<div class="dialog-header">
<h2 class="dialog-title" id="sc-help-title">Keyboard shortcuts</h2>
<button class="btn btn-icon btn-sm btn-ghost" type="button" data-momo-close aria-label="Close"><i class="mi mi-x"></i></button>
</div>
<div class="dialog-body">
<div class="shortcuts-search form-icon"><i class="mi mi-search" aria-hidden="true"></i><input class="form-control" type="search" placeholder="Search shortcuts" aria-label="Search shortcuts"></div>
</div>
</dialog>A written cheat sheet
For a help page, write the list yourself with the same classes: .shortcuts holds .shortcuts-groups, each a title and a dl.shortcuts-list of .shortcut rows.
Editing
- Bold
- Add a link
- Send
Go to
- Inbox
- Projects
- Next item
<div class="shortcuts">
<section class="shortcuts-group">
<h3 class="shortcuts-title">Editing</h3>
<dl class="shortcuts-list">
<div class="shortcut"><dt>Bold</dt><dd><kbd data-momo-keys="mod+b"></kbd></dd></div>
<div class="shortcut"><dt>Add a link</dt><dd><kbd data-momo-keys="mod+k"></kbd></dd></div>
<div class="shortcut"><dt>Send</dt><dd><kbd data-momo-keys="mod+enter"></kbd></dd></div>
</dl>
</section>
<section class="shortcuts-group">
<h3 class="shortcuts-title">Go to</h3>
<dl class="shortcuts-list">
<div class="shortcut"><dt>Inbox</dt><dd><kbd data-momo-keys="g i"></kbd></dd></div>
<div class="shortcut"><dt>Projects</dt><dd><kbd data-momo-keys="g p"></kbd></dd></div>
<div class="shortcut"><dt>Next item</dt><dd><kbd data-momo-keys="j"></kbd></dd></div>
</dl>
</section>
</div>In menus
A combo at the end of a menu item is shown small and quiet.
<button class="btn" type="button" data-momo-menu="sc-menu">Edit <i class="mi mi-chevron-down" aria-hidden="true"></i></button>
<div class="menu" id="sc-menu">
<button class="menu-item" type="button">Undo <kbd data-momo-keys="mod+z"></kbd></button>
<button class="menu-item" type="button">Redo <kbd data-momo-keys="mod+shift+z"></kbd></button>
<div class="menu-divider"></div>
<button class="menu-item" type="button">Duplicate <kbd data-momo-keys="mod+d"></kbd></button>
</div>JavaScript
Add shortcuts from code. Leave out the function to only list a key that something else already handles. Every shortcut fires a cancelable momo:shortcut event first.
const stop = Momo.shortcuts.add('mod+enter', () => form.requestSubmit(), { desc: 'Send', group: 'Editor' });
stop(); // remove it again
Momo.shortcuts.add('mod+k', null, { desc: 'Search', group: 'General' }); // listed only
Momo.shortcuts.label('mod+shift+z'); // '⌘⇧Z' on a Mac, 'Ctrl+Shift+Z' elsewhere
Momo.shortcuts.list(); // everything, for your own help page
Good to know
- Don't take keys the browser or screen readers need (, , , Tab). Single letters are fine: they never fire while typing.
- A shortcut is a shortcut: everything must also work with a click and with Tab.
- Holding a key down does not repeat a shortcut.