帮助WPF TextCompositionManager事件 [英] Help with the WPF TextCompositionManager events

查看:175
本文介绍了帮助WPF TextCompositionManager事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章很不错。有许多事件可以通过 TextCompositionManager 访问的文本输入进行监控和控制。如果你想要做一些像卡片刷卡数据,这是你要做的事情。

The docs on this are pretty shoddy. There are a number of events you can hook into to monitor and take control of text input that are accessed via the TextCompositionManager. If you're wanting to do something like snag card swipe data, this is where you'd do it.

有三个事件涉及文本输入: TextInput TextStart TextUpdate 。每个事件有两个版本,一个事件是隧道(从窗口向下移动到具有焦点的控件),以及何时冒泡(从焦点UI元素到窗口):

There are three events that concern text input: TextInput, TextStart, and TextUpdate. There are two versions of each event, one where the event is tunneling (traveling down from the Window to the control which has focus) and when it is bubbling (traveling up from the focused UI element to the window):

隧道:


  • PreviewTextInputEvent

  • PreviewTextInputStartEvent

  • PreviewTextInputUpdateEvent

冒泡:


  • TextInputEvent

  • TextInputStartEvent

  • TextInputUpdateEvent

因此,根据逻辑树中的您粘贴到TextCompositionManager的位置,您可以修改这些在事件发生之前的文字事件,或者稍后查看。所有这一切在文档和使用中都非常简单明了。

So, depending where in the logical tree you hook into the TextCompositionManager, you can modify these text events before they get to the focus of the event, or just view them afterwards. All this is pretty simple and clear in the docs and in use.

TL; DR

我找不到三个事件的体面定义。一个可以接受的答案不仅可以定义三个事件(TextInput,TextInputStart和TextInputUpdate),还可以比较和对比它们。没有分享的答案,请参考您的来源和维基百科是不受限制的。 25%的成绩取决于这一点。

I can't find a decent definition of the three events. An acceptable answer will not only define the three events (TextInput, TextInputStart, and TextInputUpdate), but also will compare and contrast them. No sharing of answers, please reference your sources and Wikipedia is off limits. 25% of your grade depends on this.

推荐答案

它们之间的区别取决于您输入的字符类型。

The differences between them depend on what type of character you're typing.


  • 标准密钥:可打印字符,如'a','A','5','%',退格等。 >
  • 控制键: Ctrl + C,Ctrl + H,Ctrl + M等。

  • 十进制键码:例如Alt + numpad 2 5 5(对于IBM扩展ASCII代码255,这是Unicode不间断空间U + 00A0)和Alt + numpad 0 2 5 5(对于Windows ANSI代码255,它是Unicode U + 00FF)。如果您有注册表设置启用它;我没有证实这一点。 (我设置了注册表设置,但没有立即生效,可能需要重新启动。)

  • 死锁:我知道多语言键盘有额外的东西叫做死键修改以下按键。例如,您可以按umlaut死钥,然后按o,这将键入一个ö (o与变音器)。 (我可能会有细节错误,我一直使用美国键盘,我不知道使用死锁的方式如何)。我怀疑这些键将与十进制键码类似;

  • IME:表意语言使用一种称为输入法编辑器的东西。我知道这些比我关于死钥匙更少,我不知道他们是否启动这些事件。

  • Standard key: printable characters like 'a', 'A', '5', '%', Backspace, and so on.
  • Control key: Ctrl+C, Ctrl+H, Ctrl+M, etc.
  • Decimal key code: things like Alt+numpad 2 5 5 (for IBM extended-ASCII code 255, which is Unicode nonbreaking space U+00A0) and Alt+numpad 0 2 5 5 (for Windows ANSI code 255, which is Unicode U+00FF). Would probably also apply to Alt+numpad plus 2 6 3 B (for U+263B) if you have the Registry setting to enable it; I haven't confirmed this. (I set the Registry setting but it had no immediate effect; probably requires a reboot.)
  • Dead keys: I understand that multilingual keyboards have extra things called "dead keys" that modify the following keystroke. E.g., you might press the "umlaut" dead key, followed by 'o', which would "type" an ö (o with an umlaut). (I may have the details wrong. I've always used an en-US keyboard and I don't know how what it takes to use dead keys.) I suspect these would behave similarly to decimal key codes; see below.
  • IMEs: Ideographic languages use something called an "Input Method Editor". I know even less about these than I do about dead keys, and I have no idea whether they fire these events or not.

注意像Shift和Ctrl这样的修饰键不会直接触发这些事件(不像KeyDown那里你看到Shift被按下,然后按5等等)。例如,Shift + 5获取%,只生成一个事件序列(即一个TextInputStart和一个TextInput),同时接收字符串%。

Note that modifier keys like Shift and Ctrl do not fire these events directly (unlike KeyDown where you see the Shift being pressed, then the 5 being pressed, etc). For example, Shift+5, to get "%", only generates one sequence of events (i.e., one TextInputStart and one TextInput), with both receiving the string "%".

TextInputStart 。当您按下标准键,控制键或十进制键代码的第一位时,它将触发。当这被触发时,系统有时(但不总是)已经知道你正在按下什么键(如在标准键和控制键的情况下)。如果知道的话,它会在TextCompositionEventArgs中告诉你;如果不知道,TextCompositionEventArgs是空的,并且完全没有告诉你。

TextInputStart is fired whenever you begin typing a character or character code. It's fired when you press a standard key, a control key, or the first digit of a decimal key code. When this is fired, the system sometimes, but not always, already knows what key you're pressing (as in the case of standard keys and control keys). If it knows, it will tell you in the TextCompositionEventArgs; if it doesn't know, the TextCompositionEventArgs is empty and tells you nothing at all.

TextInputUpdate 在您进入第二个和之后被触发数字的十进制键代码。我还没有看到这个事件的空白TextCompositionEventArgs(尽管可能会用死键或IME进行更改)。

TextInputUpdate is fired when you enter the second and subsequent digits of a decimal key code. I have yet to see anything but an empty TextCompositionEventArgs for this event (though it's possible that that changes with dead keys or IMEs).

TextInput 当您完成输入密钥时,系统将被触发,系统确定您输入的密钥是否确定,因此它始终在TextCompositionEventArgs中具有有用的信息。这个事件意味着这个角色现在实际上是打字的(也就是说,如果你正在输入一个TextBox,这个角色会显示出来)。

TextInput is fired when you're done entering the key, and the system knows for sure what key you entered, so it always has useful information in the TextCompositionEventArgs. This event means the character is actually being "typed" now (i.e. it corresponds to when the character would show up if you were typing into a TextBox).

所以这里的事件序列如何适用于不同类型的字符:

So here's how the sequences of events work for different types of characters:

标准键:您按键,您将立即获得一个TextInputStart,后跟一个TextInput。它们在TextCompositionEventArgs中都有相同的内容:e.Text和e.TextComposition.Text均设置为您按下的键。 (请注意,这并不总是可打印的字符,如果按Backspace,则在e.Text中。)如果按住该键,则会为每个键重复获取一对事件(TextInputStart / TextInput)。

Standard key: As soon as you press the key, you get a TextInputStart immediately followed by a TextInput. Both have the same content in their TextCompositionEventArgs: e.Text and e.TextComposition.Text are both set to the key you pressed. (Note that this is not always a printable character. If you press Backspace, it's in e.Text.) If the key is held down, you get the pair of events (TextInputStart/TextInput) for each key-repeat.

控制键:一旦按下字母键,您将立即获得一个TextInputStart,后跟一个TextInput。它们的TextCompositionEventArgs中都有相同的内容:e.ControlText和e.TextComposition.ControlText都设置为您按下的控制键。如果按住字母键,您将获得每个键重复的一对事件(TextInputStart / TextInput)。

Control key: As soon as you press the letter key, you get a TextInputStart immediately followed by a TextInput. Both have the same content in their TextCompositionEventArgs: e.ControlText and e.TextComposition.ControlText are both set to the control key you pressed. If the letter key is held down, you get the pair of events (TextInputStart/TextInput) for each key-repeat.

十进制键码:假设你正在键入Alt + numpad 0 2 5 5.一旦你按数字键0,你会得到一个TextInputStart事件,这告诉你绝对没有用。对于每个键盘数字键盘2,数字键盘5和数字键盘5,您都可以获得一个TextInputUpdate事件,而且没有任何有用的信息(您无法知道到目前为止已按数字)。当您释放Alt键(实际上键入您输入的代码的密钥)时,您将获得TextInput事件,并在e.Text和e.TextComposition.Text属性中输入密钥。 (这可能是不可打印的字符,例如,如果您输入Alt + numpad 0 8.)十进制键代码不能进行重复键。

Decimal key code: Let's say you're typing Alt+numpad 0 2 5 5. As soon as you press numpad 0, you get a TextInputStart event, which tells you absolutely nothing useful. For each of the keystrokes numpad 2, numpad 5, and numpad 5, you get a TextInputUpdate event, again with no useful information (you can't tell what digits have been pressed so far). When you release the Alt key (which actually "types" the key whose code you entered), then you get the TextInput event, with the key you entered in the e.Text and e.TextComposition.Text properties. (This may be a nonprintable character, e.g. if you entered Alt+numpad 0 8.) Key-repeat isn't possible for decimal key codes.

死键:如上所述,我不知道如何测试。如果有人有答案,让我知道,我会把它包括在这里。

Dead key: As I mentioned above, I don't know how to test this. If anyone has the answer, let me know, and I'll include it here.

IME:再次,我不知道如何

我的印象是,对大多数用途而言,TextInput是其中唯一的事件之一感觉使用(因为其他两个并不总是告诉你什么)。这可能是为什么它是唯一一个在UIElement,UIElement3D和ContentElement上作为标准路由(非附加)事件重新暴露的三个。

My impression is that, for most uses, TextInput is the only one of these events that it makes sense to use (since the other two don't always tell you anything). That's probably why it's the only one of the three that's re-exposed as a standard routed (non-attached) event on UIElement, UIElement3D, and ContentElement.

这篇关于帮助WPF TextCompositionManager事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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