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

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

Offen Anfängerfreundlich
#3,978 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
HTML
Sterne
25.5k
Forks
4k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Öffne den Artikel Keyboard: keydown und keyup und überprüfe den Abschnitt Default actions, insbesondere das Beispiel für den Filter von Telefonnummern und die Erläuterung des input event in der Einleitung. Füge eine kurze Anmerkung hinzu, die die Einschränkung durch IME erklärt und erläutert, warum input zum Verfolgen oder Validieren eingefügter Texte vorzuziehen ist, und überprüfe anschließend die Formulierung anhand des Beispiels.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
html, javascript
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
76/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.