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

36,661

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

}
Share:
36,661
ridoy
Author by

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, 2020

Comments

  • ridoy
    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
    ridoy over 11 years
    But a get a new exception while trying your above code,Value cannot be null.Parameter name: v ...
  • ridoy
    ridoy over 11 years
    I put this code into a button click event(is this a problem?).And the full exception was that what i mentioned.
  • Sami Franka
    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
    ridoy over 11 years
    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?
  • Sami Franka
    Sami Franka over 11 years
    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.
  • ridoy
    ridoy over 11 years
    Thanks for your effort. System.Windows.Forms.SendKeys.Send("A") has done the trick for me :)