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

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

未关闭 适合新手
#3,978 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
HTML
星标
25.5k
派生
4k
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

打开 Keyboard: keydown and keyup 文章并查看 Default actions 部分,尤其是电话号码过滤器示例以及引言中对 input event 的讨论。添加一条简短说明,解释 IME 的限制,以及为什么在跟踪或验证插入的文本时应优先使用 input,然后根据示例核对措辞。

由索引模型根据 Issue 内容生成。

评估

技术栈
html, javascript
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
冷清
描述清晰度
基本清楚
新手友好度
76/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。