Monday, 14 July 2014

Simulating Key Press in C#

Simulating a key press or a combination of keys in c# can be done using the keybd_event() function .  This function is useful to simulate Key presses to the window with focus .


C# Signature:

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

Parameters

bVk [in]
Type: BYTE
A virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual Key Codes.
bScan [in]
Type: BYTE
A hardware scan code for the key.
dwFlags [in]
Type: DWORD
Controls various aspects of function operation. This parameter can be one or more of the following values.

ValueMeaning

KEYEVENTF_EXTENDEDKEY
0x0001
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).

KEYEVENTF_KEYUP
0x0002
If specified, the key is being released. If not specified, the key is being depressed.


dwExtraInfo [in]
Type: ULONG_PTR
An additional value associated with the key stroke.

Return value

This function does not return a value.


Example 1 : The following code will simulate the pressing of keyboard button "A" . 
//import the dll 
//put this code when the class starts
[DllImport("user32.dll")]  
  public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);  
 const uint KEYEVENTF_EXTENDEDKEY = 0x0001;  
 // call this function when you want to simulate the key press .  
 // presses the key  
  keybd_event((byte)Keys.A, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);  
  Thread.Sleep(200);  
 //releases the key  
  keybd_event((byte)Keys.A, 0, 2, 0);  



Example 2 : The following code will simulate the pressing of keyboard button "\" (backslash). 
VkKeyScan function : This function function translates a character to the corresponding virtual-key code and shift state for the current keyboard .

C# Signature:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern short VkKeyScan(char ch);



//import the dll   
 //put this code when the class starts  
 [DllImport("user32.dll")]  
  public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);  
 const uint KEYEVENTF_EXTENDEDKEY = 0x0001;  
 [DllImport("user32.dll")]  
  public static extern short VkKeyScan(char z);  
 // call this function when you want to simulate the key press .  
 // presses the key  
  keybd_event((byte)VkKeyScan('\\'), 0, KEYEVENTF_EXTENDEDKEY | 0, 0);  
 Thread.Sleep(200);  
 //releases the key  
  keybd_event((byte)VkKeyScan('\\'), 0, 2, 0);  

Simulating combination of key presses .

The following code will Open task manager by pressing control , shift and escape and then releasing all the keys .

//pressing keys

 keybd_event((byte)Keys.RControlKey, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);  
 keybd_event((byte)Keys.RShiftKey, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);  
 keybd_event((byte)Keys.Escape, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);  
 //releasing all keys
 keybd_event((byte)Keys.Escape, 0, 2, 0);  
  keybd_event((byte)Keys.RShiftKey, 0, 2, 0);  
  keybd_event((byte)Keys.RControlKey, 0, 2, 0); 

2 comments:

  1. The necessity and purpose of specifying a hardware scan code or KEYEVENTF_EXTENDEDKEY flag aren't clear at all in this code. When and why are these necessary?

    ReplyDelete
  2. Decode: Simulating Key Press In C >>>>> Download Now

    >>>>> Download Full

    Decode: Simulating Key Press In C >>>>> Download LINK

    >>>>> Download Now

    Decode: Simulating Key Press In C >>>>> Download Full

    >>>>> Download LINK eM

    ReplyDelete