Skip to content

Send a committed IME composition before the custom key handler sees the key - #6330

Open
wonjangcloud9 wants to merge 1 commit into
xtermjs:masterfrom
wonjangcloud9:fix/flush-committed-composition-before-custom-key-handler
Open

wonjangcloud9 wants to merge 1 commit into
xtermjs:masterfrom
wonjangcloud9:fix/flush-committed-composition-before-custom-key-handler

Conversation

@wonjangcloud9

Copy link
Copy Markdown

Problem

With the macOS Korean IME in VS Code's terminal (seen in Cursor, VS Code 1.128 base), pressing Shift+Enter while the last syllable is still being composed puts that syllable on the next line. Typing 안녕하세요 and pressing Shift+Enter sends 안녕하세, then the newline, then 요. VS Code's Shift+Enter keybinding (workbench.action.terminal.sendSequence, which tools like Claude Code install for multi-line input) is a common setup, and Korean users hit this on every line break.

Cause

When Enter ends the composition, the IME commits the syllable (compositionend) and then the Enter keydown arrives. CompositionHelper sends the committed text from a setTimeout(0) after compositionend. CompositionHelper.keydown already sends it right away for a key that reaches it, which is why a plain Enter works. But _keyDown calls the custom key event handler before CompositionHelper.keydown. When the embedder acts on the key there (VS Code runs the keybinding and the handler returns false), its data goes out before the committed text.

Fix

Before calling the custom key event handler, _keyDown asks CompositionHelper to send a composition that has ended but is still waiting to be sent (flushCommittedComposition). It runs the same send the timeout would run, so the text is the same and it is only sent once. It does nothing while a composition is still in progress or for the keys keydown leaves to the IME (229, CapsLock, modifiers), and it is skipped when composition is ignored for Alt with macOptionIsMeta.

Tests

  • CompositionHelper.test.ts: the ended composition is sent at once and not again by the timeout; 229 and modifier keys leave it to the timeout; a composition still in progress is not sent.
  • Terminal.test.ts: the flush runs before the custom handler (fails without the _keyDown change).

Verification

A page with the built lib/xterm.mjs and a handler shaped like VS Code's (the custom handler returns false for Shift+Enter and a window keydown listener records the "sendSequence"), typed on with the real Korean IME (2-Set Korean, macOS 27.0, Chrome 157). Typing 안녕하세요, then Shift+Enter while 요 is still being composed:

keydown   Shift+Enter keyCode=229 isComposing=true
compositionupdate "요"
compositionend    "요"
keydown   Shift+Enter keyCode=13 isComposing=false

master:   sendSequence "\\\r\n", then onData "요"
this PR:  onData "요", then sendSequence "\\\r\n"

A plain Enter at the same point sends "요" then "\r" with both builds.

The same page in headless Chromium 155 with the IME driven through the DevTools protocol (Input.imeSetComposition, then the 229 keydown, Input.insertText with the committed syllable and the Enter keydown sent back to back):

master  onData "안녕하세"   sendSequence "\\\r\n"   onData "고"
this PR onData "안녕하세"   onData "고"             sendSequence "\\\r\n"

plain Enter (no handler involved), both:  onData "안녕하세"  onData "고"  onData "\r"

The same order with the DOM events dispatched in one task: master sendSequence, "고"; this PR "고", sendSequence.

Related: #6275 fixes wrong text when keys arrive before the helper's pending timers run. This PR is about the order between the helper and the custom key handler; the two touch different parts of _finalizeComposition and don't depend on each other.

🤖 Generated with Claude Code

…he key

The macOS Korean IME commits the syllable being composed when Enter is
pressed, before the Enter keydown arrives, and CompositionHelper sends
the committed text from a setTimeout(0). The custom key event handler
runs before CompositionHelper.keydown, so a key an embedder acts on
there went out first. In VS Code, a Shift+Enter keybinding that sends a
newline sequence to the terminal put the last syllable on the next line
("안녕하세" "\\\r\n" "고" instead of "안녕하세" "고" "\\\r\n").

Run the pending send before calling the custom handler. It is the same
send the timeout would make, so a key that reaches the terminal itself
behaves as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant