Skip to content

Shortcuts ​

One shortcut, written once. The same value decides whether a key event is the shortcut and how the shortcut is printed.

tsx
import { formatShortcut, isApplePlatform, matchesShortcut, shortcut } from '@adecore/ui';

const SAVE = shortcut('Mod+S');
tsx
import { Kbd, formatShortcut, isApplePlatform, matchesShortcut, shortcut, shortcutParts } from '@adecore/ui';
import { useEffect, useState } from 'react';

const SAVE = shortcut('Mod+S');

export default function ShortcutsDemo() {
    const [saves, setSaves] = useState(0);

    useEffect(() => {
        const onKeyDown = (event: KeyboardEvent): void => {
            if (matchesShortcut(SAVE, event, isApplePlatform())) {
                event.preventDefault();
                setSaves((count) => count + 1);
            }
        };

        window.addEventListener('keydown', onKeyDown);

        return () => window.removeEventListener('keydown', onKeyDown);
    }, []);

    return (
        <div className="flex flex-col items-center gap-2 text-sm text-text">
            <p>
                Press <Kbd shortcut={SAVE} variant="inline" /> on this page. Saved {saves} times.
            </p>
            <p className="text-xs text-text-muted">
                macOS writes it {formatShortcut(SAVE, true)}, everywhere else {formatShortcut(SAVE, false)} ({shortcutParts(SAVE, false).join(' and ')}).
            </p>
        </div>
    );
}

Writing one ​

shortcut(text) parses modifiers and one key joined by +. Mod is Cmd on macOS and Ctrl elsewhere. Ctrl and Meta name the physical key, for shortcuts that differ per platform on purpose. Alt and Shift complete the set.

The key is a letter, a digit, one of , . / ; ' ` \ [ ], +, -, =, a named key (Enter, Escape, Backspace, Delete, Tab, Space and the four arrows) or F1 to F12. Mod++ is Mod and the plus key. A modifier alone, such as shortcut('Mod'), never matches; it prints a modifier held during a pointer gesture.

shortcut throws on an unknown word or on two keys. Define shortcuts at module level, so a typo fails on load.

Matching ​

matchesShortcut(target, event, apple) is strict. Every modifier the shortcut does not name must be up, so Ctrl+K on a Mac is not Cmd+K. Letters, digits and punctuation match on the physical key (event.code), so Shift and Option do not change them. +, - and = match on the character, as their keys differ per layout.

isModHeld(event, apple) says whether the platform's modifier is down during a pointer gesture, such as a Cmd-click.

isApplePlatform() says whether the keyboard is a Mac's, for both matching and printing. It reads the page, unless the app handed over its own answer with setApplePlatform(source). A source is an ApplePlatformSource, a function that answers true or false, or undefined to leave the answer to the page. A desktop shell does this, since it knows its platform where a page can only guess. setApplePlatform returns the source it replaced, and null takes the app's answer away.

Printing ​

formatShortcut(target, apple) writes ⌥⇧⌘K on macOS and Ctrl+Alt+Shift+K elsewhere. shortcutParts(target, apple) returns the same as separate caps, which Keys draws. Named keys get the platform's symbol, such as ↩ for Enter on a Mac.

Recording ​

shortcutFromEvent(event, apple) reads a key press as a Shortcut, for a screen where a person chooses their own keys. The platform's modifier becomes mod, so the shortcut matches and prints on every platform; Ctrl on macOS and Meta elsewhere stay ctrl and meta. Letters, digits and punctuation come from the physical key, the same way matchesShortcut reads them.

It returns null for a modifier pressed alone and for a key shortcut() has no word for, such as a numpad digit or _ where Shift and - type it. Whatever it returns matches the event it was read from. It does not ask for a modifier: a bare letter reads as a bare letter, and refusing it is the app's rule.

shortcutText(target) writes a shortcut the way shortcut() reads it, with the modifiers in the order Mod, Ctrl, Meta, Alt, Shift: Mod+Shift+K. Keep that text to store a choice, and read it back with shortcut(). ShortcutInput records with both.

Tables ​

EDIT_SHORTCUTS holds the browser's editing shortcuts (copy, cut, paste, selectAll), for printing in a menu; the browser handles the keys. KEY_SHORTCUTS holds keys printed as a shortcut in a hint: enter, modEnter, backspace, escape, shift and rename (F2).

Types ​

A Shortcut is { mod, ctrl, meta, alt, shift, key }. KeyLike is the metaKey, ctrlKey, altKey, shiftKey, key and code of a KeyboardEvent, which a test can write by hand.

Bind each shortcut once, on the window, in one place. A shortcut needs a modifier; see Principles.

Licenses and third-party notices are listed in each package.