javascript-tutorial / javascript-tutorial/en.javascript.info
Keyboard events: keydown filtering example can't block IME-composed input (Korean/Japanese/Chinese)
還沒有人認領這個 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:
keydownfires withevent.key == "Process"(andkeyCode229), not the actual character.- More importantly, calling
preventDefault()/ returningfalseon thatkeydowndoes 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, theinputevent 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.
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
開啟 Keyboard: keydown and keyup 文章並檢視 Default actions 區段,特別是電話號碼篩選器範例,以及導言中對 input event 的討論。加入一則簡短註記,說明 IME 的限制,以及為什麼在追蹤或驗證插入的文字時應優先使用 input,然後根據範例核對措辭。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- html, javascript
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 76/100