DEV Community

Leo
Leo

Posted on Originally published at news.html.to

Your Enter-key handler submits forms mid-word for CJK typists

You wired up a search field. Enter submits. A keydown listener, an event.key === "Enter" check, done. It works on your keyboard.

Now type 標準化団体 with a Japanese IME. You type the sounds, the IME offers the characters, and you press Enter to accept the conversion. Your handler hears that Enter too. The form goes out before the user is finished.

That's the bug mehm8128 takes apart in day 3 of the HTMHell Advent Calendar. It affects Chinese, Japanese and Korean users, anyone typing through an Input Method Editor. And the obvious fix has a hole in it, thanks to event ordering in one engine.

The handler that causes it

This is the pattern the article starts from:

inputElement.addEventListener("keydown", (event) => {
  if (event.key === "Enter") {
    submit();
  }
});
Enter fullscreen mode Exit fullscreen mode

The mechanism is short. An IME runs at the OS level and converts phonetic input into characters, a step called composition. Enter is usually the key that confirms it. But keydown can fire while composition is still in progress. So the confirming Enter reaches your listener, the check passes, and submit() runs.

Enter isn't the only key at risk. The article names Tab, the arrow keys and Esc as keys people use to operate the IME. Bind any of them as a page shortcut and you get the same collision.

isComposing, and the Safari wrinkle

The standard guard is event.isComposing, which tells you whether an IME composition is in progress:

inputElement.addEventListener("keydown", (event) => {
  if (event.isComposing) {
    return;
  }
  if (event.key === "Enter") {
    submit();
  }
});
Enter fullscreen mode Exit fullscreen mode

Per the spec, the keydown of the Enter that confirms the composition fires before compositionend. So isComposing is true at that moment, and the guard returns early. Safari reverses the order. In the article's words: "Safari fires compositionend first, so event.isComposing is already false and the guard doesn't work."

The guard you write straight from the spec fails in one browser. Ordering bugs are the nasty kind. Nothing in the code looks wrong.

The guard to ship today

Add a keyCode check. According to the article, 229 is the keyCode browsers use for key events handled by an IME:

inputElement.addEventListener("keydown", (event) => {
  if (event.isComposing || event.keyCode === 229) {
    return;
  }
  if (event.key === "Enter") {
    submit();
  }
});
Enter fullscreen mode Exit fullscreen mode

Why not wait for WebKit? The article says WebKit landed a fix in April 2026, then disabled it again in June 2026 because of too many regression reports. The correct ordering is still behind a feature flag that's off by default. The author's advice is to keep the keyCode check for now.

One more platform detail from the post. On Windows, the keydown fired during composition usually has event.key === "Process", so even the naive Enter check avoids the problem there. That's also how a bug like this can pass a test run done only on a Windows machine.

Or stop listening to keydown

My favourite fix deletes code. The article's alternative is to use the native submit event of the <form> element instead of keydown, or to move the shortcut from Enter to Ctrl + Enter. Either way you stop hand-parsing keystrokes to decide when a form is done.

The catch, which the author flags: this only helps with Enter. A Tab or arrow-key shortcut still needs the guard.

It's the same lesson I keep hitting in CSS. Hand-rolled behaviour on a generic element is a re-implementation, and re-implementations pick up edge cases you never thought to test. Reach for the form first.

Who is working on it

The post lists efforts mostly started by Japanese developers. There's an ESLint rule, eslint-plugin-ime-safe-form, and a discussion about adding a similar rule to Biome. There's an issue and a PR to add a sufficient technique to WCAG 2, and a whatwg/html issue asking whether IME composition should be treated as a distinct input context. Another issue asks for a page on it in the W3C Internationalization resources. Per the article, some projects, WordPress among them, have started to handle it.

The author is open about not knowing where this should ultimately be fixed: the OS, the IME, the browser, or developer code. Until someone settles that, it's on you. Next time you put a key listener on a form field, switch on a Japanese IME, type a word, press Enter. Then do it again in Safari.

Top comments (0)