Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a keyboard key press event without pressing the key from keyboard

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 });
like image 886
ridoy Avatar asked Mar 22 '13 16:03

ridoy


People also ask

How to track keypress on the keyboard using keyboardevent?

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.

How can I trigger a keystroke from a button?

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.

How to trigger the keyDown event with a keyboard event?

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.

What are the events related to keypresses?

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.


2 Answers

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)").

like image 126
ridoy Avatar answered Oct 18 '22 04:10

ridoy


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)
{

}
like image 36
Sami Franka Avatar answered Oct 18 '22 04:10

Sami Franka