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 });
Use keyCode property of the KeyboardEvent helps to track the keypress on the keyboard. It will catch and enter the key and trigger button. HTML example code. Get the element of the input field. Execute the keyup function with addEventListener when the user releases a key.
What I want is to click a button and have the keystroke for example ALT+N or CTRL+G triggered. Thanks. You can trigger any event by it's code that is event.code Take a look at the KeyboardEvent constructor. You could use it like this: When the browser parses your HTML and reaches a <script> tag, it immediately executes the JavaScript in it.
to listen to the keydown event and trigger it. We call addEventListener with 'keydown' to listen to the keydown event. Then to trigger the keydown event, we call window.dispatchEvent with a KeyboardEvent instance. We pass in the type of keyboard event to trigger into the first argument of the constructor.
The events related to keypresses are as follows : 1 keydown: This event is triggered when a key is pressed down. 2 keypress: This event is triggered when a key is pressed. This event fails to recognise keys such as tab, shift, ctrl, backspace etc. 3 keyup: This event is triggered when a key is released.
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)")
.
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)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With