Skip to content

fix(terminal): resolve Ctrl chords positionally on a non-Latin layout - #14469

Merged
nwparker merged 1 commit into
mainfrom
nwparker/ctrl-chord-non-latin
Aug 14, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/ctrl-chord-non-latin

Conversation

@nwparker

Copy link
Copy Markdown
Contributor

Fixes #13331. Generalises the Ctrl+C fix from #14462 to the rest of the control chords, and grounds it in a measurement of what macOS itself does.

What the platform does

Measured on real hardware with UCKeyTranslate, per layout, at the physical A/U/C keys:

layout KeyA plain KeyA + Ctrl KeyU plain KeyU + Ctrl
keylayout.ABC a U+0001 u U+0015
keylayout.2SetHangul ㅁ U+0001 ㅕ U+0015
keylayout.Russian ф U+0001 г U+0015
keylayout.Greek α U+0001 θ U+0015

macOS resolves a control chord by physical key, on every layout. The .keylayout files carry an anyControl table that produces the C0 byte regardless of what the key types unmodified.

A native terminal inherits this for free — it reads the OS-provided characters and passes them through. I checked a reference implementation's source: it has no ASCII-capable-layout fallback and no virtual-keycode table anywhere. Its control-byte rule is charactersIgnoringModifiers in 'a'..'z', and when that misses — which is exactly the non-Latin case — it writes the OS's bytes verbatim. Ctrl+C works there on Russian because Apple's layout says U+0003, not because the terminal does anything layout-aware.

So positional resolution is the platform-correct answer, and matching it is matching every native terminal.

Why we do not get it for free

The browser does not expose that translation. KeyboardEvent.key carries the layout glyph (ㅁ), and nothing surfaces the OS's control byte.

xterm's legacy encoder is fine — it reads keyCode (65–90), which Chromium reports from the physical key, so control chords already work when the kitty protocol is off.

xterm's kitty encoder is not. It derives the key number from ev.key, and only consults ev.code when Shift or Option is held:

if ((ev.shiftKey || (macOptionAsAlt && ev.altKey)) && ev.code) { ... }   // Ctrl is not in this gate
if (ev.key.length === 1) { return ev.key.codePointAt(0)! }               // 'ㅁ' -> U+3141

So a pane with kitty negotiated reports CSI 12609;5u instead of CSI 97;5u, and the chord does nothing. That is #13331, and it is why #3941 needed a hand-written ETX bypass for Ctrl+C in the first place — Ctrl+C was the one letter someone had already rescued.

The rule

When Ctrl is held alone and key is a single non-ASCII codepoint, recover the letter from code and send the C0 byte — reproducing the OS control table.

  • An ASCII key stays authoritative, so a Dvorak remap is honoured.
  • Shift is excluded. Ctrl+Shift chords have their own encoding and are already correct under kitty; rewriting them would change a good report.
  • KeyC is excluded. The interrupt policy owns it: off macOS it must yield to a selection so the copy binding wins, and it resets the pane's kitty flags because a CLI can die on SIGINT before restoring them. Claiming C here would have sent ETX in exactly the case that policy deliberately declines — a regression I caught while wiring this up, and the reason the exclusion is explicit rather than incidental.
  • Digits and punctuation are untouched: they stay ASCII in key on these layouts, so xterm already sees them correctly.

A note on kitty and the reference implementation

Under the kitty protocol that reference terminal reports the layout codepoint too — its modern mapper re-translates through the current layout, so Ctrl+C on Russian yields CSI 1089;5u. We deliberately do not match that: it is the broken behaviour this issue is about. We match its legacy path, which is the one that works, and which agrees with the OS.

Tests

New terminal-non-latin-control-chord.test.ts, 16 cases, driven by the measured table above:

  • Korean / Russian / Greek / Hangul-syllable chords → the C0 byte macOS produces
  • the whole A–Z range, so this cannot be letter-by-letter incomplete again
  • ASCII key (Dvorak j on KeyC) → untouched
  • Ctrl+Shift, Alt, Meta, keyup → untouched
  • KeyC → left to the interrupt policy
  • digits, punctuation, named keys, Process, empty key → untouched
  • the matching keyup is suppressed so a kitty release cannot leak

Verified at this exact commit:

  • macOS — 265 files / 3400 tests passed, typecheck clean, oxlint + oxfmt clean
  • Windows — real host via a paired runtime, targeted specs 37/37; full pane suite 264 files passed / 1 skipped, 3398 tests passed / 1 skipped, zero failures

Not verified

No live end-to-end run with a Korean input source driving a TUI. The layout data is measured; the browser-level event shape comes from the #13331 and #14460 reports.

macOS maps a control chord by physical key regardless of layout: measured with
UCKeyTranslate, physical A/U under Control produce U+0001/U+0015 on 2SetHangul,
Russian and Greek exactly as on ABC, though unmodified those keys give ㅁ/ㅕ,
ф/г, α/θ. A native terminal inherits this by passing the OS characters through.

The browser does not expose that translation. xterm's legacy encoder works
anyway because it reads keyCode, which Chromium reports from the physical key,
but its kitty encoder derives the key number from `key` and only consults
`code` when Shift or Option is held. Ctrl is not in that gate, so a pane with
the kitty protocol negotiated reports CSI-u for U+3141 rather than 'a' and the
chord does nothing. Ctrl+C escaped this only via its hand-written ETX bypass.

Recover the byte from `code` when `key` is non-ASCII, which reproduces the
OS control table. An ASCII `key` stays authoritative so a Dvorak remap is
honoured. KeyC is excluded: the interrupt policy owns it, and off macOS that
policy declines to a selection so the copy binding wins.

Fixes #13331
@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds handling for non-ASCII Ctrl+letter keyboard events. It derives control bytes from physical letter keys, excludes unsupported combinations, and matches claimed keyups. Terminal lifecycle processing tracks the claimed key code, sends resolved input, suppresses the matching keyup, and updates Linux IME candidate-event state. Tests cover supported layouts, rejected events, mappings, and keyup behavior.

Merge Risk: 🟡 Moderate · up to 5023e

The change can incorrectly handle overlapping non-Latin Ctrl chords: an earlier key release may be forwarded to the terminal and produce an unintended kitty release report. Merge readiness therefore depends on tracking all claimed physical keys or explicitly accepting this bounded input-handling risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed but omits the required Visual Proof section and several template sections, including the checklist and explicit ELI5. Add the required Visual Proof or an allowed N/A justification, then complete the missing template sections and checklist items.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the positional Ctrl-chord fix for non-Latin layouts.
Linked Issues check ✅ Passed The implementation directly addresses issue #13331 by fixing Ctrl+A and Ctrl+U on Korean layouts, including with the kitty protocol.
Out of Scope Changes check ✅ Passed The code and tests remain focused on non-Latin positional Ctrl-chord handling and its integration with terminal keyboard processing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.ts (1)

1-18: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Keep implementation comments brief.

Reduce these comments to the non-obvious rationale. Keep the issue reference where needed. Move browser and xterm behavior details to linked documentation if they must be retained.

  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.ts#L1-L18: replace the long behavior description with a short statement of the physical-key translation rationale.
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.ts#L29-L37: reduce the KeyC policy explanation to its ownership boundary.
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.ts#L59-L64: retain only the Shift exclusion rationale.
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.test.ts#L21-L23: keep only the layout-coverage purpose.
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.test.ts#L48-L49: keep only the Ctrl+C ownership rationale.
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.test.ts#L83-L86: keep only the non-ASCII rule rationale.
  • src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts#L1087-L1090: retain only the ordering rationale.

As per coding guidelines: “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7f460332-9b01-4270-b807-298630e82dcb

📥 Commits

Reviewing files that changed from the base of the PR and between 9e5ee5e and 5023e71.

📒 Files selected for processing (3)
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.test.ts
  • src/renderer/src/components/terminal-pane/terminal-non-latin-control-chord.ts
  • src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts


// Why: let host-handled keys bypass xterm's kitty CSI-u encoder — with kittyKeyboard on it preventDefaults Cmd+C and blocks Chromium's native copy. See xterm-bypass-policy.ts.
let pendingTerminalInterruptKeyup = false
let claimedNonLatinControlChordCode: string | null = null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Track all claimed physical keys.

Line 989 stores only one claimed code. A second non-Latin Ctrl chord can overwrite the first code before its keyup arrives. The first keyup then reaches xterm and can emit the kitty release report that this flow must suppress.

Store claimed codes in a Set<string>. Remove only the matching code on keyup. Update isNonLatinControlChordKeyup and its tests to accept multiple claimed codes.

Proposed fix
- let claimedNonLatinControlChordCode: string | null = null
+ const claimedNonLatinControlChordCodes = new Set<string>()

- if (isNonLatinControlChordKeyup(e, claimedNonLatinControlChordCode)) {
-   claimedNonLatinControlChordCode = null
+ if (isNonLatinControlChordKeyup(e, claimedNonLatinControlChordCodes)) {
+   claimedNonLatinControlChordCodes.delete(e.code)
    observeLinuxCandidateEvent()
    return false
  }

  const nonLatinControlChord = resolveNonLatinControlChordInput(e)
  if (nonLatinControlChord) {
-   claimedNonLatinControlChordCode = e.code
+   claimedNonLatinControlChordCodes.add(e.code)
    pane.terminal.input(nonLatinControlChord)
- claimedCode: string | null
+ claimedCodes: ReadonlySet<string>

- if (event.type !== 'keyup' || !claimedCode) {
+ if (event.type !== 'keyup' || !event.code) {
    return false
  }
- return event.code === claimedCode
+ return claimedCodes.has(event.code)

Also applies to: 1091-1102

@nwparker
nwparker merged commit b6d36c2 into main Aug 14, 2026
45 checks passed
@nwparker
nwparker deleted the nwparker/ctrl-chord-non-latin branch August 14, 2026 07:02
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…stablyai#14469)

macOS maps a control chord by physical key regardless of layout: measured with
UCKeyTranslate, physical A/U under Control produce U+0001/U+0015 on 2SetHangul,
Russian and Greek exactly as on ABC, though unmodified those keys give ㅁ/ㅕ,
ф/г, α/θ. A native terminal inherits this by passing the OS characters through.

The browser does not expose that translation. xterm's legacy encoder works
anyway because it reads keyCode, which Chromium reports from the physical key,
but its kitty encoder derives the key number from `key` and only consults
`code` when Shift or Option is held. Ctrl is not in that gate, so a pane with
the kitty protocol negotiated reports CSI-u for U+3141 rather than 'a' and the
chord does nothing. Ctrl+C escaped this only via its hand-written ETX bypass.

Recover the byte from `code` when `key` is non-ASCII, which reproduces the
OS control table. An ASCII `key` stays authoritative so a Dvorak remap is
honoured. KeyC is excluded: the interrupt policy owns it, and off macOS that
policy declines to a selection so the copy binding wins.

Fixes stablyai#13331
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: macOS Korean 2-Set IME swallows Ctrl+A/Ctrl+U in Codex terminal input

1 participant