WPF: I don't understand class TextCompositionEventArgs
Asked Answered
S

1

12

I don't understand the class TextCompositionEventArgs.

There are members of type string named ControlText,SystemText,Text. Then there is a field TextConmposistion which itself contains the members ControlText, SystemText and Text again and additionally the fields SystemCompositionText and CompositionText.

public class TextCompositionEventArgs : InputEventArgs
{
  ..
  public string ControlText { get; }
  public string SystemText { get; }
  public string Text { get; }
  public TextComposition TextComposition { get; }
}

public class TextComposition : DispatcherObject
{
  ..
  public string CompositionText { get; protected set; }
  public string ControlText { get; protected set; }
  public string SystemCompositionText { get; protected set; }
  public string SystemText { get; protected set; }
  public string Text { get; protected set; }
}

Both Text members seem to contain the text typed with the keyboard, all other fields contain empty strings.

In which way do these fields differ and what are they good for?

Slesvig answered 9/9, 2009 at 20:6 Comment(0)
P
16

TextCompositionEventArgs deals with changes while composing text, so it has many properties dealing with the text and what specifically is changing, and how you use it depends on what events you're handling.

The basic things to understand:

  • Text: This contains that actual text that caused the event - normally the user's typed text

  • SystemText: This contains system text events, ie: if you hit Alt+letter, you'll see the event here. This is normally keystrokes that wouldn't effect text in a control like a text box.

  • ControlText: This is control text events, ie: if you hit Ctrl+letter, you'll see it here. Similar to SystemText.

Normally, if you're just looking for standard "text" events, you'll just want to look at the "Text" property. For details, see the Input Overview.

Pemba answered 9/9, 2009 at 20:33 Comment(4)
Do you know why ControlText,SystemText,Text are duplicated/redundant in the TextCompositionEventArgs class?Slesvig
They're not. For example, ControlText is test that happens when CTRL is pressed. Those keystrokes won't show up in text.Pemba
I edited my question and inserted code so you can see what I mean with duplicated/redundant.Slesvig
Oh, I think I understand. I can't look this up ATM, but from what I remember, I think this was done because the TextComposition is a DispatcherObject (for thread safety in the composition itself), but they decided to duplicate the properties to avoid locking when subscribers aren't on the dispatcher thread. I am not sure, however, why they bothered to include the TextComposition object in the eventargs, though.Pemba

© 2022 - 2024 — McMap. All rights reserved.