Trigger a keyboard key press event without pressing the key from keyboard
Solution 1
System.Windows.Forms.SendKeys.Send()
has done the trick for me.
For advanced instructions and the list of available codes, consult the docs for the method.
For example, to send Shift+A I used SendKeys.Send("+(a)")
.
Solution 2
You need to cast to Visual
:
var key = Key.A; // Key to send
var target = Keyboard.FocusedElement; // Target element
var routedEvent = Keyboard.KeyDownEvent; // Event to send
target.RaiseEvent(new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice,
System.Windows.PresentationSource.FromVisual((Visual)target), 0, key) { RoutedEvent = routedEvent });
You could also specify that your target is a specific textbox for example :
var target = textBox1; // Target element
What the code actually does is trigger the keydown event for a specific element. So it makes sense to have a keydown handler for it :
private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
}
![ridoy](https://i.stack.imgur.com/63hJ3.jpg?s=256&g=1)
ridoy
Team Lead, Artificial Intelligence. Zantrik Dreams to change the world and use technology for mankind. Interested in AI, Robotics and Cooking! Likes to develop new ideas and help others with their ideas to build.
Updated on January 24, 2020Comments
-
ridoy over 4 years
How can I trigger a key press event without pressing the key from Keyboard? I tried with the solution from here, but I got the following exception:
The best overloaded method match for 'System.Windows.PresentationSource.FromVisual(System.Windows.Media.Visual)' has some invalid arguments.
Consider Shift+A contains 2 key press event from keyboard,how can I do it without keyboard pressing?(let, just print capital A in a textbox on a button click)
My code
var key = Key.A; // Key to send var target = Keyboard.FocusedElement; // Target element var routedEvent = Keyboard.KeyDownEvent; // Event to send target.RaiseEvent(new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice, System.Windows.PresentationSource.FromVisual(target), 0, key) { RoutedEvent = routedEvent });
-
ridoy over 11 yearsBut a get a new exception while trying your above code,Value cannot be null.Parameter name: v ...
-
ridoy over 11 yearsI put this code into a button click event(is this a problem?).And the full exception was that what i mentioned.
-
Sami Franka over 11 years@ridoy I also have it in a button click, but I'm not using this line
var target = Keyboard.FocusedElement;
because the focused element would be the button, you need to specify the target as the name of your textbox. e.g :var target = YourTextBox;
-
ridoy over 11 yearsI see,thanks for pointing the error.But i need to do it for any case,not for a single textbox.Assume,i have 10 textboxes in a form,then what should be the criteria?
-
Sami Franka over 11 yearsYou can set the button
Focusable="False"
. And you can usevar target = Keyboard.FocusedElement;
This way the textbox will be focused even if you click the button. -
ridoy over 11 yearsThanks for your effort. System.Windows.Forms.SendKeys.Send("A") has done the trick for me :)