fix(tui): add modifyOtherKeys fallback for modified enter keys in tmux

When Kitty keyboard protocol is not available (e.g. inside tmux), fall
back to xterm modifyOtherKeys mode 2 so that Shift+Enter, Ctrl+Enter,
and other modified keys are distinguishable from plain Enter.

tmux users need to add to ~/.tmux.conf:
  set -g extended-keys on
  set -g extended-keys-format csi-u

closes #1872
This commit is contained in:
Mario Zechner
2026-03-06 16:02:17 +01:00
parent cb6aef2ffb
commit bdf482cefc
5 changed files with 65 additions and 1 deletions

View File

@@ -57,6 +57,7 @@ export class ProcessTerminal implements Terminal {
private inputHandler?: (data: string) => void;
private resizeHandler?: () => void;
private _kittyProtocolActive = false;
private _modifyOtherKeysActive = false;
private stdinBuffer?: StdinBuffer;
private stdinDataHandler?: (data: string) => void;
private writeLogPath = process.env.PI_TUI_WRITE_LOG || "";
@@ -158,6 +159,11 @@ export class ProcessTerminal implements Terminal {
* Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
* it supports the protocol and we enable it with CSI > 1 u.
*
* If no Kitty response arrives shortly after startup, fall back to enabling
* xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward
* modified enter keys as CSI-u when extended-keys is enabled, but may not
* answer the Kitty protocol query.
*
* The response is detected in setupStdinBuffer's data handler, which properly
* handles the case where the response arrives split across multiple stdin events.
*/
@@ -165,6 +171,12 @@ export class ProcessTerminal implements Terminal {
this.setupStdinBuffer();
process.stdin.on("data", this.stdinDataHandler!);
process.stdout.write("\x1b[?u");
setTimeout(() => {
if (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {
process.stdout.write("\x1b[>4;2m");
this._modifyOtherKeysActive = true;
}
}, 150);
}
/**
@@ -204,6 +216,10 @@ export class ProcessTerminal implements Terminal {
this._kittyProtocolActive = false;
setKittyProtocolActive(false);
}
if (this._modifyOtherKeysActive) {
process.stdout.write("\x1b[>4;0m");
this._modifyOtherKeysActive = false;
}
const previousHandler = this.inputHandler;
this.inputHandler = undefined;
@@ -240,6 +256,10 @@ export class ProcessTerminal implements Terminal {
this._kittyProtocolActive = false;
setKittyProtocolActive(false);
}
if (this._modifyOtherKeysActive) {
process.stdout.write("\x1b[>4;0m");
this._modifyOtherKeysActive = false;
}
// Clean up StdinBuffer
if (this.stdinBuffer) {