javascript-tutorial / javascript-tutorial/en.javascript.info

Keyboard events: keydown filtering example can't block IME-composed input (Korean/Japanese/Chinese)

Open Beginner friendly
#3,978 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
25.5k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

Article

Keyboard: keydown and keyup — the "Default actions" section, phone-number filter example:

<input onkeydown="return checkPhoneKey(event.key)" placeholder="Phone, please" type="tel">

Problem

The example (and the surrounding text) implies that returning false from keydown reliably prevents unwanted characters from being entered. That holds for direct (Latin) keyboard input, but not for languages typed through an IME (Input Method Editor), such as Korean, Japanese and Chinese.

With an IME active:

  • keydown fires with event.key == "Process" (and keyCode 229), not the actual character.
  • More importantly, calling preventDefault() / returning false on that keydown does not cancel the text insertion, because the character is produced by the IME composition, not by the key's default action.

As a result, Korean/Japanese/Chinese characters pass straight through the filter and appear in the field. This was reported by a Korean reader of the Korean translation (ko.javascript.info), and is easy to reproduce by switching to a Korean IME and typing into the demo.

Suggestion

Add a short note after the example, along these lines:

Please note: this filter only works for direct keyboard input. Text composed via an IME (used for languages such as Korean, Japanese or Chinese) is inserted by the input method, not by the key's default action, so it can't be blocked in keydown. That's one more reason why, for tracking or validating what actually gets into the field, the input event is a better choice.

This ties in nicely with the introduction of the article, which already recommends the input event for tracking input from any source.

Happy to send a PR if the note sounds good.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the Keyboard: keydown and keyup article and review the Default actions section, especially the phone-number filter example and the introduction’s discussion of the input event. Add a short note explaining the IME limitation and why input is preferable for tracking or validating inserted text, then verify the wording against the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
html, javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.