Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(shortcut): literal Ctrl token, strict modifier match, punctuation aliases

enes5894802a4fea
diff --git a/src/shared/shortcut.test.ts b/src/shared/shortcut.test.ts
index 9b2f2e67e..5f415e1c4 100644
--- a/src/shared/shortcut.test.ts
+++ b/src/shared/shortcut.test.ts
@@ -8,29 +8,56 @@ import {
isModifierEventKey,
matchesShortcut,
parseShortcut,
+ resolvedModifiers,
+ serializeShortcut,
shortcutKeyParts
} from './shortcut'
describe('parseShortcut', () => {
it('parses the default combo', () => {
- expect(parseShortcut('Cmd+Shift+D')).toEqual({ cmd: true, shift: true, alt: false, key: 'D' })
+ expect(parseShortcut('Cmd+Shift+D')).toEqual({
+ cmd: true,
+ ctrl: false,
+ shift: true,
+ alt: false,
+ key: 'D'
+ })
})
it('parses a combo without Shift/Alt', () => {
- expect(parseShortcut('Cmd+D')).toEqual({ cmd: true, shift: false, alt: false, key: 'D' })
+ expect(parseShortcut('Cmd+D')).toEqual({
+ cmd: true,
+ ctrl: false,
+ shift: false,
+ alt: false,
+ key: 'D'
+ })
})
it('parses Alt', () => {
- expect(parseShortcut('Cmd+Alt+D')).toEqual({ cmd: true, shift: false, alt: true, key: 'D' })
+ expect(parseShortcut('Cmd+Alt+D')).toEqual({
+ cmd: true,
+ ctrl: false,
+ shift: false,
+ alt: true,
+ key: 'D'
+ })
})
it('parses a named key (F-key)', () => {
- expect(parseShortcut('Cmd+F5')).toEqual({ cmd: true, shift: false, alt: false, key: 'F5' })
+ expect(parseShortcut('Cmd+F5')).toEqual({
+ cmd: true,
+ ctrl: false,
+ shift: false,
+ alt: false,
+ key: 'F5'
+ })
})
it('parses a named key (Space)', () => {
expect(parseShortcut('Cmd+Shift+Space')).toEqual({
cmd: true,
+ ctrl: false,
shift: true,
alt: false,
key: 'SPACE'
@@ -40,6 +67,7 @@ describe('parseShortcut', () => {
it('is tolerant of stray whitespace', () => {
expect(parseShortcut(' Cmd + Shift + D ')).toEqual({
cmd: true,
+ ctrl: false,
shift: true,
alt: false,
key: 'D'
@@ -47,12 +75,19 @@ describe('parseShortcut', () => {
})
it('parses a modifier-only chord (no trailing key) — the v3 hold-to-talk shape', () => {
- expect(parseShortcut('Cmd+Alt')).toEqual({ cmd: true, alt: true, shift: false, key: null })
+ expect(parseShortcut('Cmd+Alt')).toEqual({
+ cmd: true,
+ ctrl: false,
+ alt: true,
+ shift: false,
+ key: null
+ })
})
it('parses a modifier-only chord with all three modifiers', () => {
expect(parseShortcut('Cmd+Alt+Shift')).toEqual({
cmd: true,
+ ctrl: false,
alt: true,
shift: true,
key: null
@@ -298,7 +333,13 @@ describe('buildModifierChord', () => {
it('round-trips through parseShortcut', () => {
const combo = buildModifierChord({ cmd: true, alt: true, shift: false })
expect(combo).not.toBeNull()
- expect(parseShortcut(combo as string)).toEqual({ cmd: true, alt: true, shift: false, key: null })
+ expect(parseShortcut(combo as string)).toEqual({
+ cmd: true,
+ ctrl: false,
+ alt: true,
+ shift: false,
+ key: null
+ })
expect(isHoldChord(combo as string)).toBe(true)
})
})
@@ -347,3 +388,69 @@ describe('captureToShortcut', () => {
expect(matchesShortcut(e, combo as string, true)).toBe(true)
})
})
+
+describe('literal Ctrl token', () => {
+ it('parses Ctrl as literal control, not the abstract primary', () => {
+ expect(parseShortcut('Ctrl+Insert')).toEqual({
+ cmd: false, ctrl: true, shift: false, alt: false, key: 'INSERT'
+ })
+ })
+ it('matches literal Ctrl via ctrlKey on mac, with metaKey forbidden', () => {
+ const e = { metaKey: false, ctrlKey: true, shiftKey: false, altKey: false, key: 'Insert' }
+ expect(matchesShortcut(e, 'Ctrl+Insert', true)).toBe(true)
+ expect(matchesShortcut({ ...e, metaKey: true }, 'Ctrl+Insert', true)).toBe(false)
+ })
+ it('keeps non-mac behavior for Ctrl strings identical to Cmd strings', () => {
+ const e = { metaKey: false, ctrlKey: true, shiftKey: false, altKey: false, key: 'x' }
+ expect(matchesShortcut(e, 'Ctrl+X', false)).toBe(true)
+ expect(matchesShortcut(e, 'Cmd+X', false)).toBe(true)
+ })
+})
+
+describe('strict modifier matching', () => {
+ it('rejects a held Control on top of a Cmd chord on mac', () => {
+ const e = { metaKey: true, ctrlKey: true, shiftKey: false, altKey: false, key: 'k' }
+ expect(matchesShortcut(e, 'Cmd+K', true)).toBe(false)
+ })
+ it('rejects a held Meta on top of a Cmd chord on non-mac', () => {
+ const e = { metaKey: true, ctrlKey: true, shiftKey: false, altKey: false, key: 'k' }
+ expect(matchesShortcut(e, 'Cmd+K', false)).toBe(false)
+ })
+ it('chordHeld applies the same strictness', () => {
+ const e = { metaKey: true, ctrlKey: true, shiftKey: false, altKey: true, key: 'Control' }
+ expect(chordHeld(e, 'Cmd+Alt', true)).toBe(false)
+ })
+})
+
+describe('named punctuation keys', () => {
+ it('normalizes Comma and Slash tokens to their e.key characters', () => {
+ expect(parseShortcut('Cmd+Comma').key).toBe(',')
+ expect(parseShortcut('Cmd+Slash').key).toBe('/')
+ })
+ it('matches a comma keydown against Cmd+Comma', () => {
+ const e = { metaKey: true, ctrlKey: false, shiftKey: false, altKey: false, key: ',' }
+ expect(matchesShortcut(e, 'Cmd+Comma', true)).toBe(true)
+ })
+ it('renders punctuation parts as the character itself', () => {
+ expect(shortcutKeyParts('Cmd+Slash', true)).toEqual(['⌘', '/'])
+ })
+})
+
+describe('serializeShortcut', () => {
+ it('round-trips in canonical Cmd,Ctrl,Alt,Shift,key order', () => {
+ expect(serializeShortcut(parseShortcut('shift+d+cmd'))).toBe('Cmd+Shift+D')
+ expect(serializeShortcut(parseShortcut('Ctrl+Insert'))).toBe('Ctrl+Insert')
+ expect(serializeShortcut(parseShortcut('Alt+Cmd'))).toBe('Cmd+Alt')
+ })
+ it('round-trips a named key through its canonical spelling', () => {
+ expect(serializeShortcut(parseShortcut('Cmd+Enter'))).toBe('Cmd+Enter')
+ })
+})
+
+describe('resolvedModifiers', () => {
+ it('maps Cmd to meta on mac and ctrl elsewhere', () => {
+ const p = parseShortcut('Cmd+Shift+K')
+ expect(resolvedModifiers(p, true)).toEqual({ meta: true, ctrl: false, alt: false, shift: true })
+ expect(resolvedModifiers(p, false)).toEqual({ meta: false, ctrl: true, alt: false, shift: true })
+ })
+})
diff --git a/src/shared/shortcut.ts b/src/shared/shortcut.ts
index 660b7d9e6..e254d93fa 100644
--- a/src/shared/shortcut.ts
+++ b/src/shared/shortcut.ts
@@ -5,9 +5,15 @@
* Canonical string shape: modifier tokens joined by "+", optionally ending in one non-modifier
* key token, e.g. `"Cmd+Shift+D"`, `"Cmd+F5"`, or (v3) a MODIFIER-ONLY chord with no trailing key,
* e.g. `"Cmd+Alt"`. `Cmd` is a PLATFORM-ABSTRACTED primary modifier — it means "the primary
- * modifier for this platform": metaKey (⌘) on mac, ctrlKey elsewhere. There is no separate literal
- * "Ctrl" token; storing shortcuts in terms of the abstract "Cmd" is what lets one stored string
- * match on every platform via `matchesShortcut(e, s, isMac)`.
+ * modifier for this platform": metaKey (⌘) on mac, ctrlKey elsewhere. Storing shortcuts in terms
+ * of the abstract "Cmd" is what lets one stored string match on every platform via
+ * `matchesShortcut(e, s, isMac)`. `Ctrl` is a SEPARATE, LITERAL token meaning ctrlKey on every
+ * platform (⌃ on mac) — on non-mac it happens to resolve to the same physical key as `Cmd`, so a
+ * `Ctrl+X` string still behaves exactly like `Cmd+X` there.
+ *
+ * Modifier matching is EXACT on all four flags: a chord matches only when the event's meta/ctrl/
+ * alt/shift state is precisely what the chord resolves to, so an extra modifier held on top never
+ * fires a shorter binding.
*
* A modifier-only chord (`key === null`, see `isHoldChord`) means hold-to-talk: the chord is held
* down to record and released to stop, instead of toggling on a keyed press. `chordHeld` (not
@@ -18,6 +24,9 @@
export interface ParsedShortcut {
/** Primary modifier required: metaKey on mac, ctrlKey elsewhere. */
cmd: boolean
+ /** LITERAL Control required (⌃ on mac). On non-mac `Cmd` already resolves to Control, so a
+ * chord never needs both; validation of both-at-once lives in keybindings.ts. */
+ ctrl: boolean
shift: boolean
alt: boolean
/** Uppercased single char (e.g. "D") or named key (e.g. "F5", "SPACE", "ESCAPE"). `null` means
@@ -41,12 +50,17 @@ const KEY_LABELS: Record<string, string> = {
ARROWRIGHT: '→'
}
+/** Named tokens for keys whose `e.key` is punctuation, so canonical strings stay readable. */
+const KEY_ALIASES: Record<string, string> = { COMMA: ',', SLASH: '/', PERIOD: '.' }
+
/** Modifier-only `e.key` values — never a valid trailing key on their own. */
const MODIFIER_KEYS = new Set(['META', 'CONTROL', 'CTRL', 'SHIFT', 'ALT', 'ALTGRAPH', 'OS'])
-/** `"d"` / `"D"` / `"Escape"` / `"F5"` -> the uppercased canonical key token. */
+/** `"d"` / `"D"` / `"Escape"` / `"F5"` -> the uppercased canonical key token; a punctuation alias
+ * (`"Comma"`) -> the character `e.key` actually reports (`","`). */
function normalizeKey(key: string): string {
- return key.toUpperCase()
+ const upper = key.toUpperCase()
+ return KEY_ALIASES[upper] ?? upper
}
/** True when `key` (a raw `e.key`, any case) names a modifier key itself rather than a
@@ -57,9 +71,10 @@ export function isModifierEventKey(key: string): boolean {
return MODIFIER_KEYS.has(normalizeKey(key))
}
-/** Parse a canonical combo string, e.g. `"Cmd+Shift+D"` -> `{cmd:true, shift:true, alt:false,
- * key:'D'}`; a modifier-only chord, e.g. `"Cmd+Alt"` -> `{cmd:true, shift:false, alt:true,
- * key:null}` (see `isHoldChord`). */
+/** Parse a canonical combo string, e.g. `"Cmd+Shift+D"` -> `{cmd:true, ctrl:false, shift:true,
+ * alt:false, key:'D'}`; a modifier-only chord, e.g. `"Cmd+Alt"` -> `{cmd:true, ctrl:false,
+ * shift:false, alt:true, key:null}` (see `isHoldChord`). `Ctrl`/`Control` set the LITERAL `ctrl`
+ * field — they are no longer a spelling of the abstract `Cmd`. */
export function parseShortcut(s: string): ParsedShortcut {
const parts = s
.split('+')
@@ -67,13 +82,16 @@ export function parseShortcut(s: string): ParsedShortcut {
.filter(Boolean)
let cmd = false
+ let ctrl = false
let shift = false
let alt = false
let key: string | null = null
for (const part of parts) {
const lower = part.toLowerCase()
- if (lower === 'cmd' || lower === 'command' || lower === 'ctrl' || lower === 'control') {
+ if (lower === 'cmd' || lower === 'command') {
cmd = true
+ } else if (lower === 'ctrl' || lower === 'control') {
+ ctrl = true
} else if (lower === 'shift') {
shift = true
} else if (lower === 'alt' || lower === 'option') {
@@ -82,7 +100,47 @@ export function parseShortcut(s: string): ParsedShortcut {
key = normalizeKey(part)
}
}
- return { cmd, shift, alt, key }
+ return { cmd, ctrl, shift, alt, key }
+}
+
+/** Canonical spellings for the key tokens that are not a single character. Serializing through
+ * this (rather than emitting the uppercased token) is what makes `serializeShortcut` the exact
+ * inverse of `parseShortcut`: `"Ctrl+Insert"` round-trips instead of becoming `"Ctrl+INSERT"`.
+ * Single letters, digits and F-keys are already canonical and pass through. */
+const CANONICAL_KEY_NAMES: Record<string, string> = {
+ ENTER: 'Enter',
+ DELETE: 'Delete',
+ BACKSPACE: 'Backspace',
+ INSERT: 'Insert',
+ ESCAPE: 'Escape',
+ TAB: 'Tab',
+ SPACE: 'Space',
+ ARROWUP: 'ArrowUp',
+ ARROWDOWN: 'ArrowDown',
+ ARROWLEFT: 'ArrowLeft',
+ ARROWRIGHT: 'ArrowRight',
+ PAGEUP: 'PageUp',
+ PAGEDOWN: 'PageDown',
+ ',': 'Comma',
+ '/': 'Slash',
+ '.': 'Period'
+}
+
+function keyTokenForSerialize(key: string): string {
+ return CANONICAL_KEY_NAMES[key] ?? key
+}
+
+/** Canonical string for a parsed chord: `Cmd`,`Ctrl`,`Alt`,`Shift`, then the key token
+ * (punctuation keys serialize back to their named alias). Two spellings of the same chord
+ * always serialize identically — keybindings.ts keys its conflict identities off this. */
+export function serializeShortcut(p: ParsedShortcut): string {
+ const parts: string[] = []
+ if (p.cmd) parts.push('Cmd')
+ if (p.ctrl) parts.push('Ctrl')
+ if (p.alt) parts.push('Alt')
+ if (p.shift) parts.push('Shift')
+ if (p.key !== null) parts.push(keyTokenForSerialize(p.key))
+ return parts.join('+')
}
/** True when `s` is a modifier-only chord (no trailing key) — the v3 hold-to-talk shape. A
@@ -102,16 +160,18 @@ function keyLabel(key: string): string {
/** `"Cmd+Shift+D"` + mac -> `["⌘", "⇧", "D"]`; + non-mac -> `["Ctrl", "Shift", "D"]`; a
* modifier-only chord (`"Cmd+Alt"`) -> `["⌘", "⌥"]` (no trailing key badge). One badge per
- * element — used by ShortcutsPanel's `<kbd>` row rendering. */
+ * element — used by ShortcutsPanel's `<kbd>` row rendering. The non-mac `cmd || ctrl` collapse
+ * is safe because keybindings.ts validation forbids both in one chord. */
export function shortcutKeyParts(s: string, isMac: boolean): string[] {
- const { cmd, shift, alt, key } = parseShortcut(s)
+ const { cmd, ctrl, shift, alt, key } = parseShortcut(s)
const parts: string[] = []
if (isMac) {
if (cmd) parts.push('⌘')
+ if (ctrl) parts.push('⌃')
if (alt) parts.push('⌥')
if (shift) parts.push('⇧')
} else {
- if (cmd) parts.push('Ctrl')
+ if (cmd || ctrl) parts.push('Ctrl')
if (alt) parts.push('Alt')
if (shift) parts.push('Shift')
}
@@ -136,16 +196,31 @@ export interface ShortcutKeyEvent {
key: string
}
-/** Does `e` match the canonical combo `s`? `cmd` in `s` means metaKey on mac, ctrlKey elsewhere.
- * For a keyed combo only — a modifier-only chord (`isHoldChord(s)`) never matches here (its
- * `key` is `null`, which no `e.key` ever equals); the Canvas hold-mode listener uses
- * `chordHeld` instead. */
+/** The concrete modifier flags a chord requires on `isMac`. `Cmd` resolves to meta on mac and
+ * ctrl elsewhere; the literal `ctrl` field always demands ctrlKey. */
+export function resolvedModifiers(
+ p: ParsedShortcut,
+ isMac: boolean
+): { meta: boolean; ctrl: boolean; alt: boolean; shift: boolean } {
+ return {
+ meta: isMac && p.cmd,
+ ctrl: p.ctrl || (!isMac && p.cmd),
+ alt: p.alt,
+ shift: p.shift
+ }
+}
+
+/** Does `e` match the canonical combo `s`? `cmd` in `s` means metaKey on mac, ctrlKey elsewhere;
+ * `ctrl` always means ctrlKey. All four modifier flags must match EXACTLY, so a chord never
+ * fires with an extra modifier held on top of it (a Control held over `Cmd+K` on mac is a
+ * different chord, not a sloppier one). For a keyed combo only — a modifier-only chord
+ * (`isHoldChord(s)`) never matches here (its `key` is `null`, which no `e.key` ever equals); the
+ * Canvas hold-mode listener uses `chordHeld` instead. */
export function matchesShortcut(e: ShortcutKeyEvent, s: string, isMac: boolean): boolean {
const parsed = parseShortcut(s)
- const primaryPressed = isMac ? e.metaKey : e.ctrlKey
- if (parsed.cmd !== primaryPressed) return false
- if (parsed.shift !== e.shiftKey) return false
- if (parsed.alt !== e.altKey) return false
+ const need = resolvedModifiers(parsed, isMac)
+ if (e.metaKey !== need.meta || e.ctrlKey !== need.ctrl) return false
+ if (e.shiftKey !== need.shift || e.altKey !== need.alt) return false
return parsed.key !== null && normalizeKey(e.key) === parsed.key
}
@@ -158,9 +233,13 @@ export function matchesShortcut(e: ShortcutKeyEvent, s: string, isMac: boolean):
* guard for modifier keys specifically (a non-modifier third key is guarded separately, since
* this function ignores `e.key` entirely). */
export function chordHeld(e: ShortcutKeyEvent, s: string, isMac: boolean): boolean {
- const parsed = parseShortcut(s)
- const primaryPressed = isMac ? e.metaKey : e.ctrlKey
- return parsed.cmd === primaryPressed && parsed.shift === e.shiftKey && parsed.alt === e.altKey
+ const need = resolvedModifiers(parseShortcut(s), isMac)
+ return (
+ e.metaKey === need.meta &&
+ e.ctrlKey === need.ctrl &&
+ e.shiftKey === need.shift &&
+ e.altKey === need.alt
+ )
}
/**