9

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 });
Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60
  • 1
    have you tried the sendkeys example in your link? – RandomUs1r Mar 22 '13 at 16:10
  • @RandomUs1r,Yes i tried,but a need a target to send that key.If a have 10 textboxes in a form,then what should be the criteria?I want to write A where there is current focus for which i used Keyboard.FocusedElement in above code.Do you have any idea? – ridoy Mar 22 '13 at 17:31
  • I would use the focusmanager class instead: http://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.aspx . On a higher level though, I would probably try to implement something like this in jQuery. – RandomUs1r Mar 22 '13 at 21:33
  • 1
    @RandomUs1r jQuery... in WPF?!? – ANeves Jan 23 '15 at 17:10

3 Answers3

13

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

ANeves
  • 6,219
  • 3
  • 39
  • 63
ridoy
  • 6,274
  • 2
  • 29
  • 60
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)
{

}
Sami Franka
  • 146
  • 3
  • But a get a new exception while trying your above code,Value cannot be null.Parameter name: v ... – ridoy Mar 22 '13 at 17:06
  • I put this code into a button click event(is this a problem?).And the full exception was that what i mentioned. – ridoy Mar 22 '13 at 17:17
  • @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;` – Sami Franka Mar 22 '13 at 17:19
  • I 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? – ridoy Mar 22 '13 at 17:22
  • You can set the button `Focusable="False"`. And you can use `var target = Keyboard.FocusedElement;` This way the textbox will be focused even if you click the button. – Sami Franka Mar 22 '13 at 17:29
  • Thanks for your effort. System.Windows.Forms.SendKeys.Send("A") has done the trick for me :) – ridoy Mar 22 '13 at 19:09
0

Easy and reliable way to simulate the keyboard event is to use the keybd_event api

You can get reference to it here

http://www.pinvoke.net/default.aspx/user32.keybd_event

There are further articles on it here

http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd_event-funct

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34