Skip to content

Commit 73a4f5a

Browse files
committed
keybind: match by baseCode for non-Latin layouts
Keyboard shortcuts like Ctrl+C fail on non-Latin input layouts because the terminal reports the layout-specific character name instead of the Latin one. Fall back to the baseCode field from the Kitty keyboard protocol to identify the physical key when names differ. Consolidate inline modifier checks in TUI components behind the new matchParsedKey helper. Issue #21163
1 parent 8b9b9ad commit 73a4f5a

8 files changed

Lines changed: 166 additions & 11 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/app.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import { TuiConfigProvider, useTuiConfig } from "./context/tui-config"
5959
import { TuiConfig } from "@/config/tui"
6060
import { createTuiApi, TuiPluginRuntime, type RouteMap } from "./plugin"
6161
import { FormatError, FormatUnknownError } from "@/cli/error"
62+
import { Keybind } from "@/util/keybind"
6263

6364
async function getTerminalBackgroundColor(): Promise<"dark" | "light"> {
6465
// can't set raw mode if not a TTY
@@ -310,7 +311,7 @@ function App(props: { onSnapshot?: () => Promise<string[]> }) {
310311
// - Ctrl+C copies and dismisses selection
311312
// - Esc dismisses selection
312313
// - Most other key input dismisses selection and is passed through
313-
if (evt.ctrl && evt.name === "c") {
314+
if (Keybind.matchParsedKey("ctrl+c", evt)) {
314315
if (!Selection.copy(renderer, toast)) {
315316
renderer.clearSelection()
316317
return

‎packages/opencode/src/cli/cmd/tui/component/dialog-mcp.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function DialogMcp() {
4747

4848
const keybinds = createMemo(() => [
4949
{
50-
keybind: Keybind.parse("space")[0],
50+
keybind: Keybind.parseOne("space"),
5151
title: "toggle",
5252
onTrigger: async (option: DialogSelectOption<string>) => {
5353
// Prevent toggling while an operation is already in progress

‎packages/opencode/src/cli/cmd/tui/component/dialog-session-list.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function DialogSessionList() {
162162
},
163163
},
164164
{
165-
keybind: Keybind.parse("ctrl+w")[0],
165+
keybind: Keybind.parseOne("ctrl+w"),
166166
title: "new workspace",
167167
side: "right",
168168
disabled: !Flag.OPENCODE_EXPERIMENTAL_WORKSPACES,

‎packages/opencode/src/cli/cmd/tui/component/error-component.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createSignal } from "solid-js"
55
import { Installation } from "@/installation"
66
import { win32FlushInputBuffer } from "../win32"
77
import { getScrollAcceleration } from "../util/scroll"
8+
import { Keybind } from "@/util/keybind"
89

910
export function ErrorComponent(props: {
1011
error: Error
@@ -25,7 +26,7 @@ export function ErrorComponent(props: {
2526
}
2627

2728
useKeyboard((evt) => {
28-
if (evt.ctrl && evt.name === "c") {
29+
if (Keybind.matchParsedKey("ctrl+c", evt)) {
2930
handleExit()
3031
}
3132
})

‎packages/opencode/src/cli/cmd/tui/ui/dialog-select.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
195195
useKeyboard((evt) => {
196196
setStore("input", "keyboard")
197197

198-
if (evt.name === "up" || (evt.ctrl && evt.name === "p")) move(-1)
199-
if (evt.name === "down" || (evt.ctrl && evt.name === "n")) move(1)
198+
if (evt.name === "up" || Keybind.matchParsedKey("ctrl+p", evt)) move(-1)
199+
if (evt.name === "down" || Keybind.matchParsedKey("ctrl+n", evt)) move(1)
200200
if (evt.name === "pageup") move(-10)
201201
if (evt.name === "pagedown") move(10)
202202
if (evt.name === "home") moveTo(0)

‎packages/opencode/src/cli/cmd/tui/ui/dialog.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createStore } from "solid-js/store"
66
import { useToast } from "./toast"
77
import { Flag } from "@/flag/flag"
88
import { Selection } from "@tui/util/selection"
9+
import { Keybind } from "@/util/keybind"
910

1011
export function Dialog(
1112
props: ParentProps<{
@@ -72,12 +73,13 @@ function init() {
7273
})
7374

7475
const renderer = useRenderer()
75-
7676
useKeyboard((evt) => {
7777
if (store.stack.length === 0) return
7878
if (evt.defaultPrevented) return
79-
if ((evt.name === "escape" || (evt.ctrl && evt.name === "c")) && renderer.getSelection()?.getSelectedText()) return
80-
if (evt.name === "escape" || (evt.ctrl && evt.name === "c")) {
79+
const isCtrlC = Keybind.matchParsedKey("ctrl+c", evt)
80+
81+
if ((evt.name === "escape" || isCtrlC) && renderer.getSelection()?.getSelectedText()) return
82+
if (evt.name === "escape" || isCtrlC) {
8183
if (renderer.getSelection()) {
8284
renderer.clearSelection()
8385
}

‎packages/opencode/src/util/keybind.ts‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,70 @@ export namespace Keybind {
66
* Keybind info derived from OpenTUI's ParsedKey with our custom `leader` field.
77
* This ensures type compatibility and catches missing fields at compile time.
88
*/
9-
export type Info = Pick<ParsedKey, "name" | "ctrl" | "meta" | "shift" | "super"> & {
9+
export type Info = Pick<ParsedKey, "name" | "ctrl" | "meta" | "shift" | "super" | "baseCode"> & {
1010
leader: boolean // our custom field
1111
}
1212

13+
function getBaseCodeName(baseCode: number | undefined): string | undefined {
14+
if (baseCode === undefined || baseCode < 32 || baseCode === 127) {
15+
return undefined
16+
}
17+
18+
try {
19+
const name = String.fromCodePoint(baseCode)
20+
21+
if (name.length === 1 && name >= "A" && name <= "Z") {
22+
return name.toLowerCase()
23+
}
24+
25+
return name
26+
} catch {
27+
return undefined
28+
}
29+
}
30+
1331
export function match(a: Info | undefined, b: Info): boolean {
1432
if (!a) return false
1533
const normalizedA = { ...a, super: a.super ?? false }
1634
const normalizedB = { ...b, super: b.super ?? false }
17-
return isDeepEqual(normalizedA, normalizedB)
35+
if (isDeepEqual(normalizedA, normalizedB)) {
36+
return true
37+
}
38+
39+
const modifiersA = {
40+
ctrl: normalizedA.ctrl,
41+
meta: normalizedA.meta,
42+
shift: normalizedA.shift,
43+
super: normalizedA.super,
44+
leader: normalizedA.leader,
45+
}
46+
const modifiersB = {
47+
ctrl: normalizedB.ctrl,
48+
meta: normalizedB.meta,
49+
shift: normalizedB.shift,
50+
super: normalizedB.super,
51+
leader: normalizedB.leader,
52+
}
53+
54+
if (!isDeepEqual(modifiersA, modifiersB)) {
55+
return false
56+
}
57+
58+
return (
59+
normalizedA.name === normalizedB.name ||
60+
getBaseCodeName(normalizedA.baseCode) === normalizedB.name ||
61+
getBaseCodeName(normalizedB.baseCode) === normalizedA.name
62+
)
63+
}
64+
65+
export function parseOne(key: string): Info {
66+
const parsed = parse(key)
67+
68+
if (parsed.length !== 1) {
69+
throw new Error(`Expected exactly one keybind, got ${parsed.length}: ${key}`)
70+
}
71+
72+
return parsed[0]!
1873
}
1974

2075
/**
@@ -28,10 +83,23 @@ export namespace Keybind {
2883
meta: key.meta,
2984
shift: key.shift,
3085
super: key.super ?? false,
86+
baseCode: key.baseCode,
3187
leader,
3288
}
3389
}
3490

91+
export function matchParsedKey(binding: Info | string | undefined, key: ParsedKey, leader = false): boolean {
92+
const bindings = typeof binding === "string" ? parse(binding) : binding ? [binding] : []
93+
94+
if (!bindings.length) {
95+
return false
96+
}
97+
98+
const parsed = fromParsedKey(key, leader)
99+
100+
return bindings.some((item) => match(item, parsed))
101+
}
102+
35103
export function toString(info: Info | undefined): string {
36104
if (!info) return ""
37105
const parts: string[] = []

‎packages/opencode/test/keybind.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,24 @@ describe("Keybind.match", () => {
162162
expect(Keybind.match(a, b)).toBe(true)
163163
})
164164

165+
test("should match ctrl shortcuts by baseCode from alternate layouts", () => {
166+
const a: Keybind.Info = { ctrl: true, meta: false, shift: false, leader: false, name: "c" }
167+
const b: Keybind.Info = { ctrl: true, meta: false, shift: false, leader: false, name: "ㅊ", baseCode: 99 }
168+
expect(Keybind.match(a, b)).toBe(true)
169+
})
170+
171+
test("should still match the reported character when baseCode is also present", () => {
172+
const a: Keybind.Info = { ctrl: true, meta: false, shift: false, leader: false, name: "ㅊ" }
173+
const b: Keybind.Info = { ctrl: true, meta: false, shift: false, leader: false, name: "ㅊ", baseCode: 99 }
174+
expect(Keybind.match(a, b)).toBe(true)
175+
})
176+
177+
test("should not match a different shortcut just because baseCode exists", () => {
178+
const a: Keybind.Info = { ctrl: true, meta: false, shift: false, leader: false, name: "x" }
179+
const b: Keybind.Info = { ctrl: true, meta: false, shift: false, leader: false, name: "ㅊ", baseCode: 99 }
180+
expect(Keybind.match(a, b)).toBe(false)
181+
})
182+
165183
test("should match super+shift combination", () => {
166184
const a: Keybind.Info = { ctrl: false, meta: false, shift: true, super: true, leader: false, name: "z" }
167185
const b: Keybind.Info = { ctrl: false, meta: false, shift: true, super: true, leader: false, name: "z" }
@@ -419,3 +437,68 @@ describe("Keybind.parse", () => {
419437
])
420438
})
421439
})
440+
441+
describe("Keybind.parseOne", () => {
442+
test("should parse a single keybind", () => {
443+
expect(Keybind.parseOne("ctrl+x")).toEqual({
444+
ctrl: true,
445+
meta: false,
446+
shift: false,
447+
leader: false,
448+
name: "x",
449+
})
450+
})
451+
452+
test("should reject multiple keybinds", () => {
453+
expect(() => Keybind.parseOne("ctrl+x,ctrl+y")).toThrow("Expected exactly one keybind")
454+
})
455+
})
456+
457+
describe("Keybind.fromParsedKey", () => {
458+
test("should preserve baseCode from ParsedKey", () => {
459+
const result = Keybind.fromParsedKey({
460+
name: "ㅊ",
461+
ctrl: true,
462+
meta: false,
463+
shift: false,
464+
option: false,
465+
number: false,
466+
sequence: "ㅊ",
467+
raw: "\x1b[12618::99;5u",
468+
eventType: "press",
469+
source: "kitty",
470+
baseCode: 99,
471+
})
472+
473+
expect(result).toEqual({
474+
name: "ㅊ",
475+
ctrl: true,
476+
meta: false,
477+
shift: false,
478+
super: false,
479+
leader: false,
480+
baseCode: 99,
481+
})
482+
})
483+
484+
test("should ignore leader unless explicitly requested", () => {
485+
const key = {
486+
name: "ㅊ",
487+
ctrl: true,
488+
meta: false,
489+
shift: false,
490+
option: false,
491+
number: false,
492+
sequence: "ㅊ",
493+
raw: "\x1b[12618::99;5u",
494+
eventType: "press" as const,
495+
source: "kitty" as const,
496+
baseCode: 99,
497+
}
498+
499+
expect(Keybind.matchParsedKey("ctrl+c", key)).toBe(true)
500+
expect(Keybind.matchParsedKey("ctrl+x,ctrl+c", key)).toBe(true)
501+
expect(Keybind.matchParsedKey("ctrl+x,ctrl+y", key)).toBe(false)
502+
expect(Keybind.matchParsedKey("ctrl+c", key, true)).toBe(false)
503+
})
504+
})

0 commit comments

Comments
 (0)