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

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

Aberta Para iniciantes
#3,978 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Linguagem predominante
HTML
Estrelas
25.5k
Forks
4k
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

## Article

[Keyboard: keydown and keyup](https://javascript.info/keyboard-events) — the "Default actions" section, phone-number filter example:

```html

```

## 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.

Guia de contribuição

Nenhum guia de contribuição indexado para este repositório

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Abra o artigo Keyboard: keydown and keyup e revise a seção Default actions, especialmente o exemplo do filtro de número de telefone e a discussão do input event na introdução. Adicione uma nota curta explicando a limitação de IME e por que input é preferível para acompanhar ou validar o texto inserido; em seguida, verifique a redação com base no exemplo.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
html, javascript
Domínio
documentation
Tipo de issue
Documentação
Dificuldade
2/5
Tempo estimado
1-3 horas
Status de atividade
Pouca atividade
Clareza
Razoavelmente clara
Facilidade para iniciantes
76/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.