Skip to content

Commit 69712f6

Browse files
committed
fix: normalize numpad key names to standard equivalents
Numpad keys (kp0-kp9, kpenter, kpdecimal, kpdivide, kpmultiply, kpminus, kpplus, kpequal, kpseparator) are reported with distinct names by the kitty keyboard protocol. This causes them to not match keybindings or direct name checks (e.g. evt.name === "return" fails for numpad enter). Normalize numpad key names in KeyHandler.processParsedKey() before emitting events, so all downstream handlers see standard key names regardless of whether the key was pressed on the main keyboard or numpad. The sequence is also unconditionally set to the mapped text since the raw escape sequence would otherwise remain as the sequence value. Fixes anomalyco/opencode#7698 Fixes anomalyco/opencode#17457 Fixes anomalyco/opencode#27138
1 parent c381b40 commit 69712f6

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎packages/core/src/lib/KeyHandler.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,74 @@ test("KeyHandler - internal handler error with preventDefault still respects pre
636636
expect(internalCalled).toBe(false)
637637
})
638638

639+
test("KeyHandler - numpad keys are normalized to standard names", () => {
640+
const handler = createKeyHandler()
641+
642+
let receivedKey: KeyEvent | undefined
643+
handler.on("keypress", (key: KeyEvent) => {
644+
receivedKey = key
645+
})
646+
647+
// Simulate a kitty protocol numpad 5 key (codepoint 57404)
648+
dispatchInput(handler, "\x1b[57404u", { useKittyKeyboard: true })
649+
650+
expect(receivedKey).toBeDefined()
651+
expect(receivedKey?.name).toBe("5")
652+
expect(receivedKey?.sequence).toBe("5")
653+
})
654+
655+
test("KeyHandler - numpad enter is normalized to return", () => {
656+
const handler = createKeyHandler()
657+
658+
let receivedKey: KeyEvent | undefined
659+
handler.on("keypress", (key: KeyEvent) => {
660+
receivedKey = key
661+
})
662+
663+
// Simulate a kitty protocol numpad enter (codepoint 57414)
664+
dispatchInput(handler, "\x1b[57414u", { useKittyKeyboard: true })
665+
666+
expect(receivedKey).toBeDefined()
667+
expect(receivedKey?.name).toBe("return")
668+
})
669+
670+
test("KeyHandler - numpad decimal is normalized to dot", () => {
671+
const handler = createKeyHandler()
672+
673+
let receivedKey: KeyEvent | undefined
674+
handler.on("keypress", (key: KeyEvent) => {
675+
receivedKey = key
676+
})
677+
678+
// Simulate a kitty protocol numpad decimal (codepoint 57409)
679+
dispatchInput(handler, "\x1b[57409u", { useKittyKeyboard: true })
680+
681+
expect(receivedKey).toBeDefined()
682+
expect(receivedKey?.name).toBe(".")
683+
expect(receivedKey?.sequence).toBe(".")
684+
})
685+
686+
test("KeyHandler - numpad operators are normalized", () => {
687+
const handler = createKeyHandler()
688+
689+
const receivedKeys: KeyEvent[] = []
690+
handler.on("keypress", (key: KeyEvent) => {
691+
receivedKeys.push(key)
692+
})
693+
694+
// kpdivide=57410, kpmultiply=57411, kpminus=57412, kpplus=57413
695+
dispatchInput(handler, "\x1b[57410u", { useKittyKeyboard: true })
696+
dispatchInput(handler, "\x1b[57411u", { useKittyKeyboard: true })
697+
dispatchInput(handler, "\x1b[57412u", { useKittyKeyboard: true })
698+
dispatchInput(handler, "\x1b[57413u", { useKittyKeyboard: true })
699+
700+
expect(receivedKeys).toHaveLength(4)
701+
expect(receivedKeys[0]?.name).toBe("/")
702+
expect(receivedKeys[1]?.name).toBe("*")
703+
expect(receivedKeys[2]?.name).toBe("-")
704+
expect(receivedKeys[3]?.name).toBe("+")
705+
})
706+
639707
test("KeyHandler - error in one event type does not prevent other event types from working", () => {
640708
const handler = createKeyHandler()
641709

‎packages/core/src/lib/KeyHandler.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,37 @@ export type KeyHandlerEventMap = {
9696
paste: [PasteEvent]
9797
}
9898

99+
// Map numpad key names to their standard equivalents so that keybindings
100+
// and direct name checks (e.g. `evt.name === "return"`) work uniformly
101+
// regardless of whether the key was pressed on the main keyboard or numpad.
102+
const numpadKeyNameMap: Record<string, string> = {
103+
kpenter: "return",
104+
kp0: "0",
105+
kp1: "1",
106+
kp2: "2",
107+
kp3: "3",
108+
kp4: "4",
109+
kp5: "5",
110+
kp6: "6",
111+
kp7: "7",
112+
kp8: "8",
113+
kp9: "9",
114+
kpdecimal: ".",
115+
kpdivide: "/",
116+
kpmultiply: "*",
117+
kpminus: "-",
118+
kpplus: "+",
119+
kpequal: "=",
120+
kpseparator: ",",
121+
}
122+
99123
export class KeyHandler extends EventEmitter<KeyHandlerEventMap> {
100124
public processParsedKey(parsedKey: ParsedKey): boolean {
125+
const mappedName = numpadKeyNameMap[parsedKey.name]
126+
if (mappedName) {
127+
parsedKey.name = mappedName
128+
parsedKey.sequence = mappedName
129+
}
101130
try {
102131
switch (parsedKey.eventType) {
103132
case "press":

0 commit comments

Comments
 (0)