C# 和 IME - 获取当前输入文本
我使用日语 IME 作为示例,但在使用 IME 进行输入的其他语言中可能也是如此。
当用户使用 IME 在文本框中键入文本时,将触发 KeyDown 和 KeyUp 事件。但是,在用户使用 Enter 键验证 IME 中的输入之前,TextBox.Text 属性不会返回键入的文本。
因此,例如,如果用户输入 5 次 あ 然后进行验证,我将获得 5 个 keydown/keyup 事件,每次 TextBox.Text 返回“”(空字符串),最后我将获得一个用于输入的 keydown/keyup 事件。键,TextBox.Text会直接变成“あああああ”。
在用户最后验证之前,如何在用户键入时获取用户输入?
(我知道如何在网页上的 字段上用 javascript 执行此操作,因此在 C# 中一定可以!)
I'm using the Japanese IME as an example but it is probably the same in other languages which use an IME for input.
When the user types text into a textbox using the IME, the KeyDown and KeyUp events are fired. However the TextBox.Text property does not return the typed text until the user has validated the input in the IME using the Enter key.
So for example if the user types 5 times あ then validates, I will get 5 keydown/keyup events, with each time TextBox.Text returning "" (the empty string) and at the end I will get a keydown/keyup for the enter key and the TextBox.Text will directly become "あああああ".
How can I get the user input while the user is typing, before the user validates at the end?
(I know how to do this in javascript on an <input> field on a webpage, so it must be possible in C#!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用它来获取当前的合成。这适用于任何组合状态,并且适用于日语、中文和韩语。我只在 Windows 7 上测试过它,所以不确定它是否也适用于其他版本的 Windows。
至于事情是相同的,那么,三者之间的事情实际上是非常不同的。
我见过的其他实现使用了 StringBuilder,但使用字节数组要好得多,因为 SB 通常也会在其中包含一些垃圾。字节数组以 UTF16 编码。
通常,正如 Dian 所说,每当收到“WM_IME_COMPOSITION”消息时,您都会想要调用 GetComposition。
在调用 ImmGetContext 之后调用 ImmReleaseContext 非常重要,这就是它位于 finally 块中的原因。
You can use this to get the current composition. This will work in any composition state, and for Japanese, Chinese, and Korean. I've only tested it on Windows 7, so not sure if it'll work on other versions of Windows.
As for things being the same, well, things are actually horribly different between the three.
Other implementations I've seen used a StringBuilder, but it is much better to use a byte array, because the SB will usually end up with some rubbish in it as well. The byte array is encoded in UTF16.
And usually, you would want to call GetComposition whenever you receive a "WM_IME_COMPOSITION" message as Dian said.
It is very important to call ImmReleaseContext after you call ImmGetContext, which is why it is in a finally block.