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

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

Aperta Adatta ai principianti
#3,978 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
HTML
Stelle
25.5k
Fork
4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Apri l’articolo Keyboard: keydown e keyup e rivedi la sezione Default actions, in particolare l’esempio del filtro per i numeri di telefono e la spiegazione dell’input event nell’introduzione. Aggiungi una breve nota che spieghi la limitazione di IME e perché input è preferibile per monitorare o convalidare il testo inserito, quindi verifica la formulazione rispetto all’esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
html, javascript
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
76/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.