Fix Escape key handling for Kitty keyboard protocol

Add isEscape() helper that handles both raw (\x1b) and Kitty protocol
(\x1b[27u) Escape sequences. Update all components that check for
Escape key to use the new helper.
This commit is contained in:
Mario Zechner 2025-12-19 02:09:51 +01:00
parent 314ef34ebc
commit f8b6164ecd
11 changed files with 38 additions and 18 deletions

View file

@ -1,5 +1,5 @@
import type { AutocompleteProvider, CombinedAutocompleteProvider } from "../autocomplete.js";
import { isAltBackspace, isCtrlA, isCtrlC, isCtrlE, isCtrlK, isCtrlU, isCtrlW, Keys } from "../keys.js";
import { isAltBackspace, isCtrlA, isCtrlC, isCtrlE, isCtrlK, isCtrlU, isCtrlW, isEscape, Keys } from "../keys.js";
import type { Component } from "../tui.js";
import { visibleWidth } from "../utils.js";
import { SelectList, type SelectListTheme } from "./select-list.js";
@ -267,7 +267,7 @@ export class Editor implements Component {
// Handle autocomplete special keys first (but don't block other input)
if (this.isAutocompleting && this.autocompleteList) {
// Escape - cancel autocomplete
if (data === "\x1b") {
if (isEscape(data)) {
this.cancelAutocomplete();
return;
}

View file

@ -1,4 +1,4 @@
import { isCtrlC } from "../keys.js";
import { isCtrlC, isEscape } from "../keys.js";
import type { Component } from "../tui.js";
import { truncateToWidth } from "../utils.js";
@ -163,7 +163,7 @@ export class SelectList implements Component {
}
}
// Escape or Ctrl+C
else if (keyData === "\x1b" || isCtrlC(keyData)) {
else if (isEscape(keyData) || isCtrlC(keyData)) {
if (this.onCancel) {
this.onCancel();
}

View file

@ -31,6 +31,7 @@ export {
isCtrlT,
isCtrlU,
isCtrlW,
isEscape,
isShiftTab,
Keys,
} from "./keys.js";

View file

@ -28,6 +28,7 @@ const CODEPOINTS = {
w: 119,
// Special keys
escape: 27,
tab: 9,
enter: 13,
backspace: 127,
@ -194,3 +195,12 @@ export function isAltBackspace(data: string): boolean {
export function isShiftTab(data: string): boolean {
return data === RAW.SHIFT_TAB || data === Keys.SHIFT_TAB;
}
/**
* Check if input matches the Escape key (raw byte or Kitty protocol).
* Raw: \x1b (single byte)
* Kitty: \x1b[27u (codepoint 27 = escape)
*/
export function isEscape(data: string): boolean {
return data === "\x1b" || data === `\x1b[${CODEPOINTS.escape}u`;
}