fix(terminal): recover dropped keyCode 229 input

Android/GBoard-style keyboards fire keydown with keyCode 229 and, on some
paths, never mutate xterm's helper textarea. xterm has nothing to diff, so
it emits no data and the typed character is silently dropped: it never
reaches the PTY and never appears on screen.

terminal-keycode229-recovery.js is a standalone controller that re-emits
exactly those keys, and only once. xterm stays authoritative throughout:

- Only an explicit keyCode 229 keydown carrying a single printable key (or
  Enter) is eligible; Process/Unidentified/Dead, modifiers, AltGraph and a
  live composition are all left alone.
- The re-emit is scheduled from a microtask and then a zero-delay timer, so
  xterm's own textarea diff always gets the first opportunity; canonical
  data for the same key cancels the pending fallback.
- compositionstart and blur drop every pending candidate, so a real IME
  composition lifecycle is never second-guessed.
- After a recovery, one late canonical value attributed to that key token
  (via beforeinput/input on the helper textarea) is suppressed so the
  character cannot be delivered twice; the record expires after 250ms and
  an unattributed byte is never suppressed.

terminal-ui.js wires it at the two existing choke points — the custom key
handler and the onData registration, the latter now a named handler so the
recovery path can re-enter it — with both hooks wrapped so a failure in the
fallback can never break canonical input.

Unit coverage drives the module directly in a vm; the wiring itself is
covered end-to-end in the (browser-only) terminal-copy-shortcut suite.
This commit is contained in:
Aamer Akhter
2026-09-07 12:27:36 -04:00
parent 61d22eee1c
commit 82b090c74a
6 changed files with 618 additions and 2 deletions
+84
View File
@@ -218,6 +218,90 @@ describe('terminal Ctrl+C smart copy', () => {
expect(res.data.join('')).not.toContain('\x16');
});
it('recovers explicit keyCode 229 input once when the helper textarea never mutates', async () => {
await setup('KEYCODE-229-RECOVERY', false);
const result = await page.evaluate(async () => {
const app = (window as any).app;
const textarea = document.querySelector('.xterm-helper-textarea') as HTMLTextAreaElement;
const originalSessionId = app.activeSessionId;
const originalLocalEcho = app._localEchoEnabled;
const originalSendInput = app._sendInputAsync;
const originalPendingInput = app._pendingInput;
const originalLastKeystrokeTime = app._lastKeystrokeTime;
const sent: string[] = [];
const dispatch229 = (key: string) => {
for (const type of ['keydown', 'keyup']) {
const event = new KeyboardEvent(type, {
key,
bubbles: true,
cancelable: true,
composed: true,
});
Object.defineProperties(event, { keyCode: { value: 229 }, which: { value: 229 } });
textarea.dispatchEvent(event);
}
};
try {
app.activeSessionId = 'cod388-browser-regression';
app._localEchoEnabled = false;
app._pendingInput = '';
app._lastKeystrokeTime = 0;
app._sendInputAsync = (_sessionId: string, data: string) => sent.push(data);
textarea.focus();
dispatch229('x');
await new Promise((resolveWait) => setTimeout(resolveWait, 30));
const afterRecovery = [...sent];
// A browser that supplies its canonical input late must not duplicate
// the character already recovered for this key token.
textarea.dispatchEvent(
new InputEvent('beforeinput', { data: 'x', inputType: 'insertText', bubbles: true, composed: true })
);
textarea.value = 'x';
textarea.dispatchEvent(
new InputEvent('input', { data: 'x', inputType: 'insertText', bubbles: true, composed: true })
);
await new Promise((resolveWait) => setTimeout(resolveWait, 0));
const afterLateInput = [...sent];
dispatch229('Enter');
await new Promise((resolveWait) => setTimeout(resolveWait, 30));
const final = [...sent];
// Two 229 candidates can overlap while the main thread is busy. A
// canonical value for the first must resolve that candidate without
// cancelling the second candidate's fallback.
const overlapStart = sent.length;
dispatch229('a');
dispatch229('b');
const busyUntil = performance.now() + 25;
while (performance.now() < busyUntil) {
// Deliberately hold the browser task so both xterm/fallback timers
// remain queued while canonical input for `a` is prepared.
}
app.terminal._core.coreService.triggerDataEvent('a', true);
await new Promise((resolveWait) => setTimeout(resolveWait, 30));
return { afterRecovery, afterLateInput, final, overlap: sent.slice(overlapStart) };
} finally {
app.activeSessionId = originalSessionId;
app._localEchoEnabled = originalLocalEcho;
app._sendInputAsync = originalSendInput;
app._pendingInput = originalPendingInput;
app._lastKeystrokeTime = originalLastKeystrokeTime;
textarea.value = '';
}
});
expect(result).toEqual({
afterRecovery: ['x'],
afterLateInput: ['x'],
final: ['x', '\r'],
overlap: ['a', 'b'],
});
});
it('forwards full-width punctuation after a Chinese IME composition', async () => {
await setup('IME-PUNCTUATION', false);
const desktopChunks = await captureImeInput(page);