为什么PreviewTextInput不处理空格? [英] Why does PreviewTextInput not handle spaces?

查看:569
本文介绍了为什么PreviewTextInput不处理空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的窗口上的PreviewTextInput事件来处理磁卡读卡器的刷卡。我处理窗口上的事件,所以不管哪个控件都集中在哪里。



一旦处理程序确定刷卡已经开始(输入'%'或';'字符),它处理所有事件,直到刷卡完成。该系统通常工作相当不错,除了一些重要的例外:



当从读写器输入空格字符(可能是一个\\\
字符)时,它们不是由PreviewTextInput处理,但直接发送到任何控件集中。奇怪的是,处理程序确实接收到\r个字符。这会导致不良行为。



我想要的是在窗口级别捕获所有键事件的方法,并且有机会在我想要的时候处理它们。我已经试过PreviewKeyDown,发现使用它有点麻烦,并从中获取char值。 PreviewTextInput更好,因为我可以简单地阅读Text属性。



是否有原因PreviewTextInput不处理某些字符?有没有可比较的方法来获取所有事件,包括空格?

解决方案

我在这个WPF论坛问题


由于一些IME将会将空格击键作为文本合成过程的一部分,这就是为什么Avalon通过TextInput事件报告正确的合成文本。


还有一些来自 TextInput事件的MSDN文档


...对于键盘输入,WPF首先发送适当的KeyDown / KeyUp事件。如果没有处理这些事件,并且密钥是文本(而不是诸如方向箭头或功能键的控制键),则会引发TextInput事件。在KeyDown / KeyUp和TextInput事件之间并不总是有一个简单的一对一映射,因为多个按键可以生成单个文本输入字符,单击可以生成多个字符串。对于使用输入法编辑器(IME)的汉语,日语和韩语等语言,在相应的字母表中生成数千个可能的字符,尤其如此。 WPF发送一个KeyUp / KeyDown事件,如果按键可以成为TextInput事件的一部分(例如按ALT + S),Key将设置为Key.System。这允许KeyDown事件处理程序中的代码检查Key.System,如果找到,则为随后提取的TextInput事件的处理程序留下处理。在这些情况下,TextCompositionEventArgs参数的各种属性可用于确定原始按键。类似地,如果IME处于活动状态,Key具有Key.ImeProcessed的值,ImeProcessedKey给出了原始的按键或击键。


BTW这里有两个相关问题:




I am handling the PreviewTextInput event on my window to handle swipes from a magnetic card reader. I handle the event on the window, so that it does not matter which individual control is focused.

Once the handler determines a swipe has started (a '%' or ';' character is input), it handles all events until the swipe is finished. This system generally works quite nicely, with a few important exceptions:

When a space character (and possibly a \n character) are input from the reader, they are not handled by PreviewTextInput, but are sent directly into whatever control is focused. Oddly enough, the handler does receive \r characters. This causes undesired behavior.

What I want is a way to capture all key events at the window level, and have an opportunity to handle them if I want to. I have tried PreviewKeyDown and found it a bit cumbersome to use, and to get char values from. PreviewTextInput is much nicer because I can simply read the Text property.

Is there a reason PreviewTextInput does not handle certain characters? Is there any comparable method to get all events, including spaces?

解决方案

I found some sort of explanation in this WPF forum question:

Because some IMEs will treat whitespace keystroke as part of the text composition process, that's why it eats up by Avalon to report correct composited text through TextInput event.

And some more info from the MSDN documentation of the TextInput event:

... For keyboard input, WPF first sends the appropriate KeyDown/KeyUp events. If those events are not handled and the key is textual (rather than a control key such as directional arrows or function keys), then a TextInput event is raised. There is not always a simple one-to-one mapping between KeyDown/KeyUp and TextInput events because multiple keystrokes can generate a single character of text input and single keystrokes can generate multi-character strings. This is especially true for languages such as Chinese, Japanese, and Korean which use Input Method Editors (IMEs) to generate the thousands of possible characters in their corresponding alphabets.

When WPF sends a KeyUp/KeyDown event, Key is set to Key.System if the keystrokes could become part of a TextInput event (if ALT+S is pressed, for example). This allows code in a KeyDown event handler to check for Key.System and, if found, leave processing for the handler of the subsequently raised TextInput event. In these cases, the various properties of the TextCompositionEventArgs argument can be used to determine the original keystrokes. Similarly, if an IME is active, Key has the value of Key.ImeProcessed, and ImeProcessedKey gives the original keystroke or keystrokes.

BTW, Here are two related questions:

这篇关于为什么PreviewTextInput不处理空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆