WPFwiki, wpf, wiki, .net 3.0, windows presentation foundation, FAQ, free resources, solution, development, microsoft Home of the world's largest WPF FAQ
Edit

Category

Input

Edit

Question

How can I simulate text input?

Edit

Answer

This is done by building a TextCompositionEventArgs object and sending it either directly to the object on which the input needs to be simulated or through the InputManager.

To build a TextCompositionEventArgs, you need to specify the following at construction:

1. An InputDevice: This can be a KeyboardDevice, MouseDevice, StylusDevice or TabletDevice. A common method for getting an InputDevice is to ask for the Keyboard.PrimaryDevice (this is quite consistent with the type of input, i.e., Text).

2. A TextComposition object. This can be either TextComposition or one of the derived types. But be aware that some controls might not support the derived types. Refer to the MSDN documentation for the derived types.

To build the TextComposition object, you need to specify at a minimum, at its construction:

1. The InputManager: You can pass InputManager.Current.

2. The InputSource: This can typically be set to the value of Keyboard.FocusedElement.

3. Text: This is the Text argument for the text input. Set this to the text you want to "input" to the element.

The TextCompositionEventArgs then only needs the RoutedEvent member to be set. The value can be either PreviewTextInput or TextInput.

Once the RoutedEvent is set, the event args can then be sent, for processing by the appropriate element:

TextCompositionEventArgs e = new TextCompositionEventArgs(InputManager.Current, myTarget, "foo");
e.RoutedEvent = UIElement.TextInputEvent;

myTarget.RaiseEvent(e);


or

TextCompositionEventArgs e = new TextCompositionEventArgs(InputManager.Current, Keyboard.FocusedElement, "foo");
e.RoutedEvent = UIElement.TextInputEvent;

InputManager.Current.ProcessInput(e);




Edit

Related Links

TODO - Delete if none

Edit

Source Link

TODO - Delete if none

Edit

Keywords

TODO - Delete if none

All content is Copyright ©2007 Xceed Software Inc. unless otherwise indicated. See the Terms of Service. Contributors must read and agree to the Contribution Policy. WPFwiki is brought to you by Xceed, makers of the powerful yet free Xceed DataGrid for WPF.