Hidden deep within the Windows Script Host’s object model is a small but powerful method called SendKeys that allows you to send keystrokes to the active window just as if you had manually typed them on the keyboard. Programming the SendKeys method is relatively easy once you understand its rules and how to use it, and it can literally save you hours of time that you used to spend performing repetitive tasks.
In this Daily Drill Down, I’ll explain in detail how the SendKeys method works, and I’ll show you how to create a SendKeys macro to automate repetitive tasks. To conclude, I’ll show you how to bypass one of the biggest complaints IT pros have against using the SendKeys method.
To learn about the history of native macro capability, click here.
SendKeys comes onto the scene
When Microsoft introduced Windows Script Host 2.0 in 1999, they included a new method in the object model called SendKeys. As the name implies, the SendKeys method is designed to allow you to send any keystrokes you can imagine from within a script to an active window just as if they were typed from the keyboard. While SendKeys is limited to the keyboard, keep in mind that you can use the keyboard to manually perform many of the operations you normally perform with the mouse.
The SendKeys method provides you with a very easy-to use, macro-like tool, and best of all, it’s built right into the Windows operating system. Windows XP comes with Windows Script Host 5.6, which, like its predecessor, includes the SendKeys method.
Downloading the Windows Script Host package
Windows Script Host 5.6 is built into Windows XP, but if you’re using Windows 98/ME or Windows 2000, you’ll definitely want to upgrade to the most recent version to take advantage of all the latest improvements. You can download the Windows Script Host 5.6 package from the Windows Script portion of Microsoft’s MSDN Library site. Just follow the link to the Windows Script 5.6 Download section. (Keep in mind that there are two versions of the package, one for Windows 2000 and one for Windows 98/ME and Windows NT 4.0.) While you’re there, make sure that you also download the Windows Script Host 5.6 Documentation. It’s an executable installation file, which will install a compiled Help file.
Before you can begin using the SendKeys method to create macros, you need to have a grasp of its syntax. Fortunately, the syntax for the SendKeys method is very straightforward:
In this case, WshShell is the standard Windows Script Host Shell object variable that you need to instantiate at the beginning of every script, and string is the keystroke that you want to send to the application.
Sending any of the standard characters on the keyboard is just a matter of enclosing the characters in quotes as a string. For example, if you wanted to send the single character z, you’d use the format:
Similarly, if you wanted to send a phrase, you’d use the format:
WshShell.SendKeys “Hello World!”
In most cases, you’ll be using the SendKeys method to generate compound keystrokes that make use of the special keys [Ctrl], [Alt], and [Shift]. To represent these special keys, the SendKeys method uses the characters shown in Table A.
|Characters for representing special keys|
For example, if you wanted to send the command [Ctrl]S to an application in order to save the current file, you’d use the format:
In addition to standard characters and the special keys, there are a host of other keys you’ll want to use, such as [Enter] or the function keys. To send these types of keys, you’ll use a set of codes that you’ll then enclose in brackets. These codes are listed in Table B.
|Codes that represent special keys|
Oddly enough, Microsoft didn’t add a special code to the SendKeys method that would allow you to access the Windows key.
Although the SendKeys method is powerful tool, it can’t provide all of the features you need to run macros. Fortunately, Windows Script Host provides you with several other powerful companion methods that fill in the gaps.
The AppActivate method
To begin with, it’s important to understand that the SendKeys method will simply blast keystrokes out to whatever window currently has the focus, which may or may not be the task that you want to automate via a SendKeys macro. To ensure that the intended window receives the keystrokes, you’ll use the AppActivate method. As its name implies, the AppActivate method is designed to shift the focus to a specific application or to any open window (including dialog boxes) that you specify. To do its job, the AppActivate method uses the syntax:
Here, title would be a string containing the name that appears in the title bar of the application or dialog box that you want to bring into focus. You can use a portion of the title or the whole title.
If the window is in the background, the AppActivate method will bring it to the foreground so that you can see what is happening. However, if the window is minimized, the AppActivate method will leave it where it is but will still shift the focus to that window.
There are a couple of snags that you need to be aware of when you use the AppActivate method. If an application has several windows open and all of them contain the same title, the AppActivate method will grab the first window it encounters. If the application isn’t running or a window with the title you specify doesn’t exist, the AppActivate method will either grab any window that contains a word from that title or simply flounder in the background until the script terminates. As such, it’s important that you be very detailed when specifying a title with the AppActivate method.
The Sleep method
The Sleep method is designed to temporarily suspend script execution for a specific length of time. You’ll use the Sleep method in between selecting a window with the AppActivate method and automating it with the SendKeys method. This will ensure that the AppActivate method has grabbed the window before the SendKeys method begins sending keystrokes.
The Sleep method uses the syntax:
Here, time is an integer value that represents milliseconds. For example, a value of 1000 would equal one second, which is a pretty long period of time to have a script pause when creating a SendKeys macro. In general, using a value in the range of 100 to 500 is sufficient.
The Run method
Now, if you want to automate an application via a SendKeys macro that currently isn’t running, you can use the Run method to actually launch the application.
The Run method uses the syntax:
WshShell.Run(executable, [WindowStyle], [Wait])
Here, executable is the path to and the name of the application’s executable file. The WindowStyle and Wait parameters are optional and allow you to control the size and location of the window and whether the script is to pause until the application is closed, respectively. In most cases, simply using the executable parameter is sufficient.
A typical SendKeys macro will only use Windows Script Host methods. In other words, it won’t necessarily include any statements from any scripting languages such as VBScript or Jscript. However, you’ll need to use either the VBScript or Jscript file format for your SendKeys macro, and then save the file with either a VBS or JS extension.
Creating SendKeys macros
Once you decide on a task that you want to automate using a SendKeys macro, you need to manually perform the task using the keyboard to note each keystroke you use and any window/dialog box titles that you encounter while performing the task.
For example, suppose that you want to automate the procedure of clearing Windows XP’s My Recent Documents list. As you know, the Clear List button is buried deep within the Taskbar and Start Menu Properties dialog box. As such, automating this procedure using a SendKeys macro would be a big time saver.
Now, if you map out the keystrokes necessary to reach the Clear List button and to carry out the task, you’d come up with a list like the one shown in Table C.
|Keystrokes for clearing the My Recent Documents list|
Using this information, you could then create the script shown in Listing A.
Final tip: bypassing the errant keystrokes complaint
One of the biggest complaints I’ve heard about using the SendKeys method for macros is that if the window focus somehow changes in the middle of the script execution, the keystrokes will be sent to whatever window receives the focus. Now the danger of this is that the keystrokes intended for one window might have disastrous effects (like causing the loss of data) when applied to another window.
You can prevent this by using the AppActivate command line before each and every SendKeys command line, which will ensure that the right window receives the keystrokes. To implement this solution, you can create a subroutine that contains the AppActivate, Sleep, and SendKeys commands, and then use a series of Call statements to send the subroutine the keystroke to perform and the title of the window.
For example, if you were to apply this subroutine technique to the script I mentioned above that clears Windows XP’s My Recent Documents list, the script would look like the one shown in Listing B.