Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

2.4 KB
Raw Blame
import { isWindows } from "./platform";

/**
 * The keycap for the last part of a hotkey (a `KeyboardEvent.code` name):
 * `KeyR` → "R", `Digit2` → "2", `ArrowUp` → "↑", `Numpad1` → "Num 1". The
 * Mac uses its menu symbols for the editing keys, Windows their names.
 */
function keyLabel(code: string, windows: boolean): string {
  const shared: Record<string, string> = {
    ArrowUp: "↑",
    ArrowDown: "↓",
    ArrowLeft: "←",
    ArrowRight: "→",
    NumpadAdd: "Num +",
    NumpadSubtract: "Num -",
    NumpadMultiply: "Num *",
    NumpadDivide: "Num /",
    NumpadDecimal: "Num .",
    NumpadEqual: "Num =",
    NumpadEnter: "Num Enter",
    // Punctuation keys show the character on the key, not the code name
    // ("BracketLeft" -> "["). These are the US-layout characters, the same on
    // both platforms, matching how the shortcut editor records them.
    Minus: "-",
    Equal: "=",
    BracketLeft: "[",
    BracketRight: "]",
    Backslash: "\\",
    Semicolon: ";",
    Quote: "'",
    Backquote: "`",
    Comma: ",",
    Period: ".",
    Slash: "/",
  };
  const platform: Record<string, string> = windows
    ? { Enter: "Enter", Tab: "Tab", Backspace: "Backspace", Delete: "Del", Insert: "Ins", Home: "Home", End: "End", PageUp: "PgUp", PageDown: "PgDn" }
    : { Enter: "↩", Tab: "⇥", Backspace: "⌫", Delete: "⌦", Insert: "Ins", Home: "↖", End: "↘", PageUp: "⇞", PageDown: "⇟" };
  return shared[code] ?? platform[code] ?? code.replace(/^(Key|Digit)/, "").replace(/^Numpad(\d)$/, "Num $1");
}

const modifierLabels = (windows: boolean): Record<string, string> =>
  windows
    ? { Meta: "Win", Ctrl: "Ctrl", Control: "Ctrl", Alt: "Alt", Option: "Alt", Shift: "Shift" }
    : { Meta: "⌘", Ctrl: "⌃", Control: "⌃", Alt: "⌥", Option: "⌥", Shift: "⇧" };

/** `Meta+Shift+Digit2` → ["⌘", "⇧", "2"] (or Win/Ctrl/Alt/Shift words). */
export function hotkeyKeycaps(hotkey: string, windows: boolean = isWindows): string[] {
  if (!hotkey) return [];
  const parts = hotkey.split("+");
  const key = keyLabel(parts.pop() ?? "", windows);
  const labels = modifierLabels(windows);
  return [...parts.map((part) => labels[part] ?? part), key];
}

/** The whole hotkey as one label: "⌘⇧R" on the Mac, "Ctrl+Shift+R" on Windows. */
export function formatHotkey(hotkey: string, windows: boolean = isWindows): string {
  return hotkeyKeycaps(hotkey, windows).join(windows ? "+" : "");
}