// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The extra-keys bar: phones only, and only while the on-screen keyboard is // up (App.tsx decides). Esc, Tab, a latching Ctrl, ^C, the page's shortcut // prefix (^A: arms it, keys.ts, as Ctrl-A on a keyboard does, and the // shortcuts HUD shows; again disarms it), arrows, a few symbols and Paste. // The display's soft keyboard (Display.tsx, Pad.tsx) shows one too, with its own press // (key presses for the guest), without the prefix key and without Paste there. import { useRef, type MouseEvent, type TouchEvent } from "react"; import { KW } from "../kw.ts"; import { PREFIX, PREFIX_SHORT } from "../keys.ts"; import { useStore } from "../store.ts"; type K = { key: string; label: string; word?: boolean; aria?: string; title?: string } | "gap"; const KEYS: K[] = [ { key: "esc", label: "Esc", word: true }, { key: "tab", label: "Tab", word: true }, { key: "ctrl", label: "Ctrl", word: true }, { key: "^c", label: "^C", word: true, title: "Ctrl-C" }, { key: "prefix", label: PREFIX_SHORT, word: true, title: `${PREFIX}: the page's shortcuts` }, "gap", { key: "left", label: "←", aria: "Left" }, { key: "up", label: "↑", aria: "Up" }, { key: "down", label: "↓", aria: "Down" }, { key: "right", label: "→", aria: "Right" }, "gap", { key: "|", label: "|" }, { key: "/", label: "/" }, { key: "-", label: "-" }, { key: "~", label: "~" }, { key: "paste", label: "Paste", word: true }, ]; const keyOf = (e: { target: EventTarget }) => (e.target as Element | null)?.closest?.("button")?.getAttribute("data-key") ?? null; export function Keys({ show, onPress, omit, className = "" }: { /** Shown (default: the terminal's bar, while its keyboard is up). */ show?: boolean; /** Where presses go (default: the terminal). */ onPress?: (k: string) => void; omit?: readonly string[]; className?: string; } = {}) { const ctrl = useStore((s) => s.ctrl); const keysOn = useStore((s) => s.keysOn); const prefix = useStore((s) => s.prefix); const on = show ?? keysOn; // Keep focus (and so the on-screen keyboard) on the terminal: a mouse // press is cancelled at mousedown; a tap is handled at touchend, whose // cancellation suppresses the tap's focus change and click. A touch that // moved is a horizontal scroll of the bar, not a key press. const touch0 = useRef<{ x: number; y: number } | null>(null); const press = (k: string | null) => { if (k) (onPress ?? KW.ui!.pressKey)(k); }; return ( ); }