在 Javascript 中按下 Enter 之前检测 IME 输入
我什至不确定这是否可能,所以如果这是一个愚蠢的问题,我深表歉意。
我通过 jQuery 设置了一个 keyup
回调,以便在用户在输入框中键入内容时运行一个函数。对于英语来说效果很好。
但是,当输入日语/韩语/中文文本时,只有在用户确认其文本后才会调用该函数。
是否有可能检测到他们已经开始打字,并访问他们尚未完成的文本?
我想这可能是操作系统级别的事情,所以 Javascript 无法访问它。
编辑:我刚刚意识到这在 Chrome 和 Safari 中有效,但在 Firefox 中无效(还没有机会在 Windows 上尝试)。所以 Chrome 调用 keyup 就可以获取文本了。但我在 Firefox 中仍然遇到上述问题。
I'm not even sure if this is possible, so apologies if it's a stupid question.
I've set up an keyup
callback through jQuery to run a function when a user types in an input box. It works fine for English.
However when inputting text in Japanese/Korean/Chinese, the function isn't called until the user confirms their text.
Is it possible to detect that they've started typing, and access their as-yet unfinished text?
I'm thinking maybe it's an OS-level thing so Javascript doesn't have access to it.
Edit: I just realised that this works in Chrome and Safari, but not in Firefox (not had a chance to try it on Windows yet). So Chrome calls keyup and it's possible to get the text. But I'm still having the above problem in Firefox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
compositionstart
、compositionupdate
和compositionend
事件可能会有所帮助。它们可以帮助您检测何时使用 IME 输入。
例如,考虑使用 IME 输入日语中的 か (ka)。
将触发以下事件(按所示顺序):
请注意,仅当 IME 可见时(按下 enter 之前)才会触发合成事件。另请注意,keypress 事件未触发。这只针对非 IME 输入触发。
要访问用户未完成的文本,您可以使用事件的
data
属性。有关详细信息,请参阅 MDN:compositionstart、组合更新,组合结束,InputEvent。
The
compositionstart
,compositionupdate
andcompositionend
events might be helpful.They help you detect when IME input is being used.
For example, consider using an IME to type か (ka) in Japanese.
The following events (in the order shown) would be fired:
Notice that the compositon events are only fired when the IME is visible (before enter is pressed). Also notice that the keypress event is not fired. This is only fired for non-IME input.
To access the user's unfinished text, you can use the
data
property of the event.For more info see MDN: compositionstart, compositionupdate, compositionend, InputEvent.
这是 Firefox 中的已知问题,以及浏览器应该采取的措施 em>正在做不清楚。
解决此问题的一种可能方法是此处演示,其中文本字段轮询文本更改(而不是依赖事件)。
This is a known issue in Firefox, and what browsers should be doing isn't clear.
A possible method for working around this problem is demonstrated here, where the text field is polled for changes to the text (rather than relying on events).
如果 IME 在 React 应用程序中处于活动状态,请防止提交,请
向 @james-lawson 大声喊叫,谢谢你!
Prevent submit if IME is active in a React App
big shout to @james-lawson, thank you man!