What the browser sends during composition#
While a composition is active, key events carry a flag. In a keydown fired during composition, event.isComposing is true, and in Chromium event.keyCode is 229. MDN: KeyboardEvent.isComposing · MDN: compositionstart
We reproduced it in Chromium through the DevTools Protocol, without an IME installed. We started a composition of にほん in an input, then sent an Enter key with the virtual key code 229. The input's keydown listener logged:
keydown key=Enter isComposing=true keyCode=229
A handler written as if (e.key === 'Enter') submit() counted it as a submit.
Reproduce it#
With a real IME. Add a Japanese input source in the operating system's keyboard settings, type nihon in the field and press Enter once to confirm. If the form submits, or the field clears, the bug is there. This is the strongest evidence and needs no code.
With the DevTools Protocol in Playwright and Chromium:
const cdp = await page.context().newCDPSession(page);
await page.focus('#search');
await cdp.send('Input.imeSetComposition', { text: 'にほん', selectionStart: 3, selectionEnd: 3 });
await cdp.send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 229 });
await cdp.send('Input.insertText', { text: 'にほん' }); // commits the composition
Protocol: Input.imeSetComposition
With a synthetic event in a unit test, to check only the handler's logic:
input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', isComposing: true, bubbles: true }));
The order of events differs between browsers and operating systems. Safari, for example, has been reported to fire the confirming keydown after compositionend, where isComposing is already false. Test in the browser the reporter used.
The usual fix, and its edge#
Ignore Enter while composing:
input.addEventListener('keydown', e => {
if (e.key === 'Enter' && !e.isComposing && e.keyCode !== 229) submit();
});
The keyCode check covers browsers that fire the confirming keydown with isComposing already false. Some editors also track compositionstart and compositionend and ignore Enter until a short moment after composition ends.
What to put in the report#
- The language and input method, such as "Japanese, macOS Kotoeri, Romaji input"
- The OS and browser versions
- The exact keys: "type
nihon, press Enter once to confirm" - What happened: submitted
にほn, submitted nothing, or cleared the field
Worked example: a chat that sent fragments#
This example is fictional. Japanese-speaking users of a support chat report that messages arrive cut off, as if sent halfway through a word. Nobody on the team can reproduce it.
A tester adds the Japanese input source, types arigatou, and presses Enter to confirm the conversion. The chat sends ありがとu immediately. The team then writes the DevTools Protocol version as an automated test, which fails the same way. The chat's keydown handler submitted on any Enter. Adding the composition check fixed it, and the protocol test now runs in CI.
Questions#
Does this affect only East Asian languages?
No. Any input method that composes characters, including some for Vietnamese and some accent input methods, can produce composition events.
Does Playwright's keyboard.press('Enter') reproduce it?
No. It sends a normal Enter without a composition. Use the protocol calls or a real IME.
Are mobile keyboards affected?
Mobile keyboards often compose text even for Latin languages, for autocorrect and suggestions, and some report keyCode 229 for many keys. Test on the device when the report comes from a phone.
Return to reproducing what someone else saw for the other layers a replay needs.