Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically generate keypress events in C#?

How can I programmatically create an event that would simulate a key being pressed on the keyboard?

like image 893
Dan Vogel Avatar asked Oct 29 '09 18:10

Dan Vogel


People also ask

What is the difference between jquery's change () and keypress () events?

The change event occurs if the value has been changed when the element loses focus. The keypress event occurs every time you press down and release a (non control) key.

Which keyboard event is used for script runs when key is pressed?

keypress – fires when you press a character keyboard like a , b , or c , not the left arrow key, home, or end keyboard, … The keypress also fires repeatedly while you hold down the key on the keyboard.

What is a keypress event?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys.


2 Answers

The question is tagged WPF but the answers so far are specific WinForms and Win32.

To do this in WPF, simply construct a KeyEventArgs and call RaiseEvent on the target. For example, to send an Insert key KeyDown event to the currently focused element:

var key = Key.Insert;                    // Key to send var target = Keyboard.FocusedElement;    // Target element var routedEvent = Keyboard.KeyDownEvent; // Event to send      target.RaiseEvent(   new KeyEventArgs(     Keyboard.PrimaryDevice,     PresentationSource.FromVisual(target),     0,     key)   { RoutedEvent=routedEvent } ); 

This solution doesn't rely on native calls or Windows internals and should be much more reliable than the others. It also allows you to simulate a keypress on a specific element.

Note that this code is only applicable to PreviewKeyDown, KeyDown, PreviewKeyUp, and KeyUp events. If you want to send TextInput events you'll do this instead:

var text = "Hello"; var target = Keyboard.FocusedElement; var routedEvent = TextCompositionManager.TextInputEvent;  target.RaiseEvent(   new TextCompositionEventArgs(     InputManager.Current.PrimaryKeyboardDevice,     new TextComposition(InputManager.Current, target, text))   { RoutedEvent = routedEvent } ); 

Also note that:

  • Controls expect to receive Preview events, for example PreviewKeyDown should precede KeyDown

  • Using target.RaiseEvent(...) sends the event directly to the target without meta-processing such as accelerators, text composition and IME. This is normally what you want. On the other hand, if you really do what to simulate actual keyboard keys for some reason, you would use InputManager.ProcessInput() instead.

like image 130
Ray Burns Avatar answered Sep 25 '22 23:09

Ray Burns


To produce key events without Windows Forms Context, We can use the following method,

[DllImport("user32.dll")] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo); 

sample code is given below:

const int VK_UP = 0x26; //up key const int VK_DOWN = 0x28;  //down key const int VK_LEFT = 0x25; const int VK_RIGHT = 0x27; const uint KEYEVENTF_KEYUP = 0x0002; const uint KEYEVENTF_EXTENDEDKEY = 0x0001; int press() {     //Press the key     keybd_event((byte)VK_UP, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);     return 0; } 

List of Virtual Keys are defined here.

To get the complete picture, please use the below link, http://tksinghal.blogspot.in/2011/04/how-to-press-and-hold-keyboard-key.html

like image 42
Rajesh Avatar answered Sep 21 '22 23:09

Rajesh