This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.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.

Sending Repeating Keystrokes

If you want to send the same keystroke many times, you can add an integer value to your character or key code. For codes, add a space and the number of repetitions between the code and the closing brace. For other characters, add braces around the character and include the number in the same manner.

For example, the following code sends ten hyphens followed by ten presses of the Enter key.

SendKeys.Send("{- 10}");
SendKeys.Send("{Enter 10}");

Timing Issues

Using SendKeys for integration between two programs is inelegant but can be the last resort when there's no other way to automate software. One of the biggest problems with SendKeys is dealing with timing issues. Depending upon the hardware being used and the resource and processor utilisation, the speed at which keypresses are processed by the target application can vary greatly. If you send keys too quickly, it is possible that some keypresses will not have the desired effect.

Some timing problems can be alleviated using the SendWait method instead of Send. You call this method using the same string parameter and key codes. The difference is that after the keystrokes are sent, the method waits for the keys to be processed before returning.

SendKeys.SendWait("Hello, world!");

Flush Method

The final method provided by the SendKeys class is Flush. When you call this method your software waits until the keyboard buffer has been flushed and other operating system messages have been processed.

10 May 2013