
.NET 1.1+Using SendKeys
Some software that needs to be automated provides no application programming interface (API) to permit this and no direct access to data to control the program indirectly. In these cases, one solution to automation is the SendKeys class.
SendKeys Class
The SendKeys class provides several static methods that allow you to control the contents of the keyboard buffer. You can add keystrokes to the buffer and wait for them to be processed by the application that receives them.
The receiver is always the window that has the focus. SendKeys does not permit you to set the foreground window, except by sending the Alt+Tab key combination. You need to use another method to achieve this if you wish to send keystrokes to control a specific application or utility.
Sending Basic Keystrokes
The simplest way to add keystrokes to the keyboard buffer is to provide a string to the Send method's single parameter. Each character in the string is converted into the keypresses required to produce it, with the characters being processed in the order in which they appear. For example, specifying a capital letter "A" sends messages for the Shift key and the A key.
To demonstrate, create a new Windows Forms application and add a button to the automatically generated form. Double-click the button in the designer to add a Click event. Add the code below to the click event's method.
System.Threading.Thread.Sleep(5000);
SendKeys.Send("Hello, world!~");
The above code pauses for five seconds before adding some keystrokes to the keyboard buffer. The five second pause is to give enough time to select an application to receive the keypresses.
Run the program but do not click the button just yet. First, start Notepad. Once Notepad is loaded, click the button then switch the active window to Notepad within the five second period. After the pause the text, "Hello, world" will be typed into Notepad automatically. NB: The tilde character (~) tells SendKeys to send an Enter keypress.
Sending Other Keystrokes
There are many keys on a standard keyboard that can't be represented by a single character in a string. They include the arrow keys, backspace, delete and others. To send such keystrokes you must include a special key code in the string argument. This takes the form of an alphanumeric code within brace characters ({}).
The list of available codes and the keys they represent is as follows:
Code | Key |
---|
Add | Keypad Add |
BS, Bksp or Backspace | Backspace |
Break | Break |
CapsLock | Caps Lock |
Del or Delete | Delete |
Divide | Keypad Divide |
Down | Down Arrow |
End | End |
Enter | Enter |
Esc | Escape |
F1 - F16 | Function Keys |
Help | Help |
Home | Home |
Ins or Insert | Insert |
Left | Left Arrow |
Multiply | Keypad Multiply |
NumLock | Num Lock |
PgDn | Page Down |
PgUp | Page Up |
Prtsc | Print Screen |
Right | Right Arrow |
ScrollLock | Scroll Lock |
Subtract | Keypad Subtract |
Tab | Tab |
Up | Up Arrow |
To demonstrate we can send one of the codes to Notepad. When you press F5 whilst editing a file in Notepad the current date and time is inserted automatically. Let's use SendKeys to do this. Add the following code to the event method then run the program again. Press the button and switch to Notepad before the five second delay completes to see the results.
Sending Key Modifiers
It's common to send commands to Windows programs using a combination of the Control, Alt and Shift keys with another key. Control, Alt and Shift are modifier keys. Generally they change the behaviour of another key, rather than generating an action by themselves.
To send a modifier key you must include a special character in the Send method's argument. This appears before the character or code for key that you wish to modify. To change the keystroke with Control, include a caret symbol (^). For Alt, prefix the key with a plus character (+). For shift, use the percentage (%).
For example, to send the keyboard combination of Ctrl-A to Notepad in order to select all of the current text, use the following code:
NB: If you wish to apply the same modifier key to a series of keypresses, surround the affected characters or codes with parentheses. The following example sends the key combination Ctrl-A followed by Ctrl-X.
10 May 2013