First IME-committed character is dropped when input fires before keydown (macOS)
xterm.js 6.0.0 · macOS 26.5 · WKWebView (system WebKit 26.5, via Tauri 2) · macOS Simplified Chinese (Pinyin) input source
Problem
Typing full-width punctuation (?, !, () with the macOS Chinese input source drops
the first character. Subsequent ones work. Symptom: "the first ? needs two presses".
Cause
_inputEvent guards on (!ev.composed || !this._keyDownSeen). For a browser-dispatched
input event ev.composed is always true, so the guard is effectively !this._keyDownSeen.
_keyDownSeen is set in _keyDown and cleared in _keyUp — which assumes input arrives
after the keydown of the same keystroke. With macOS IME punctuation the order is inverted:
beforeinput → input → keydown (1–4 ms apart). So _keyDownSeen still reflects the
previous key. These characters require Shift, whose keydown set the flag and whose
keyup hasn't happened yet — so the first one is dropped. Each character's own keyup then
clears the flag, which is why the rest get through.
Evidence
Capture-phase log from a real run; value is textarea.value at log time. Shift held down,
! pressed twice:
t=56898 keydown key="Shift" code=16 value=""
t=57158 beforeinput data="!" value=""
t=57162 input data="!" value="!" ← dropped, no onData
t=57163 keydown key="!" code=229 value="!"
t=57275 keyup key="!" code=49 value="!" ← flag cleared here
t=57651 beforeinput data="!" value="!"
t=57653 onData "!" value="!!" ← passes now
t=57654 input data="!" value="!!"
Also worth noting:
- No
composition*events fire at all for these characters —CompositionHelperis not involved. textarea.valueis never cleared and keeps accumulating.- Ordinary keys are unaffected: they're handled on the
keydownpath and_keyPressHandledalready prevents the double-send (same log showsonData " "atkeydowntime, with the followinginputproducing nothing).
Direction
_keyPressHandled already covers the double-send for printable characters, so _keyDownSeen
looks like a second line of defence whose ordering assumption doesn't hold universally.
Options: clear it when an insertText beforeinput arrives without an intervening keydown;
or scope it to the case where the keydown actually produced data.
No PR yet — the right fix depends on which double-send _keyDownSeen was added to prevent.
Happy to write one if a maintainer points at a preferred direction.
Workaround
Capture-phase beforeinput on the element containing xterm's textarea (ancestor capture runs
before the textarea's own capture listeners, where _inputEvent is registered):
host.addEventListener('beforeinput', (ev: InputEvent) => {
if (ev.inputType !== 'insertText' || !ev.data) return
const core = (term as unknown as { _core?: { _keyDownSeen?: boolean } })._core
if (core && '_keyDownSeen' in core) core._keyDownSeen = false
}, true)
Touches a private field, so guard it and pin the assumption with a test.
Another data point, and one that I think narrows this: 5.5.0 is affected too, so this is not a 6.0.0 regression.
Host: macOS, WKWebView (Tauri 2), @xterm/xterm 5.5.0 pinned, Chinese input method (Apple Pinyin). Pressing the key for
? once is swallowed; the second press inserts it. Composed CJK text — the kind that goes through the candidate window —
is unaffected and lands on the first press. Native macOS text fields in the same session take the punctuation on the
first press.
What we ruled out on our side before looking at xterm.js, in case it saves anyone repeating it:
- plain shifted ASCII symbols typed into the terminal reach the PTY intact;
- the same bytes sent directly to our backend, bypassing the terminal, arrive intact;
- our application installs no custom key handler on the terminal.
The reason 5.5.0 behaves identically is visible in the shipped bundle — the fallback guard in _inputEvent is the same
code as on current master:
if (e.data && "insertText" === e.inputType && (!e.composed || !this._keyDownSeen) && !screenReaderMode)
composed is always true here, so the condition rests entirely on _keyDownSeen, which a modifier-only keydown (the
Shift still held for ?) is enough to arm. That matches what this issue reports about the event ordering under
WebKit.
#6200 and #6054 both propose the same one-line change for this. I have not tested either patch, so this is a report of the symptom and the version range, not a verification of the fix.
#6200 fixed this: a modifier-only keydown such as Shift no longer arms the _keyDownSeen gate, so an IME commit that arrives before its own keydown isn't dropped anymore. It's on master and will ship in the next release. Dropped characters during fast typing with no modifier held are tracked in #5887.