EditCategory
Input
EditQuestion
How can I simulate text input?
EditAnswer
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);
EditRelated Links
TODO - Delete if none
EditSource Link
TODO - Delete if none
EditKeywords
TODO - Delete if none