简体   繁体   中英

c# / wpf simulate arrow keys (and other control keys) through events

I am attempting to simulate several keyboard actions in a wpf textbox, including arrow presses (to move caret etc) from another event (eg a button click). I have no problem with adding text by raising the

TextCompositionManager.TextInputEvent

event, but attempting to send keys through Keyboard events is not working:

Keyboard.Focus(targetTextBox);

KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(targetTextBox), 0, Key.UP);

ke.RoutedEvent = Keyboard.KeyDownEvent;
targetTextBox.RaiseEvent(ke);

Is there a way to send control keys through the TextInputEvent, or making the Keyboard event work - i have tried using previewDownEvent and pairing with keyUp events.

EDIT: Also i would prefer to do this through wpf if possible, without using windows forms.

Thanks

I believe this is what you're looking for, though it doesn't use events.

...
    SendKeys.Send("{LEFT}");
    SendKeys.Send("{RIGHT}");
...

Remember to set focus to the control you want to input in first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM