Converting the Windows Script Host SendKeys Method

Definition: Sends one or more keystrokes to the active window (as if typed on the keyboard).

SendKeys

We have to admit that we have mixed emotions about finding a way to replicate the SendKeys method. There’s no doubt that SendKeys can be useful; it provides a way for you to programmatically send keystrokes to an application that otherwise could not be managed using a script. For example, Notepad does not have any sort of script-accessible object model; nevertheless, you could still bring up an instance of Notepad, type some data into the application, then close and save the file, all through the magic of SendKeys. Pretty cool, huh?

Well, maybe. As useful as SendKeys might be, it’s also a little … temperamental …. SendKeys works by bringing up the application in question and then sending it keystrokes as though a user was sitting at the keyboard working with the program. That’s fine, unless a new application pops up and steals the focus; at that point, SendKeys will be sending keystrokes to that program. Likewise you can run into timing issues: sometimes the application can’t keep up with the keystrokes sent its way. And then there’s the problem of unexpected dialog boxes and – well, you get the idea.

Still, if you really want to use SendKeys from within Windows PowerShell, well, here’s how you can do it:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate("Test.ps1 - Notepad")

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("ABCDEFGHIJKLM")

In our initial line of code we’re loading the .Net Framework class Microsoft.VisualBasic; we need to do that so that, in line 2, we can use the AppActivate method to give the focus to the window captioned Test.ps1 – Notepad. (For a more detailed explanation of what we’re doing here, see the AppActivate section of this document.)

In line 3 we load in another .NET Framework class: System.Windows.Forms. Once that class is loaded we then use the SendWait method to send the string ABCDEFGHIJKLM to Test.ps1. And what will happen when we do this? You got it: the letters ABCDEFGHIJKLM will be typed into Test.ps1. Just like magic. Or, at the least, just like a player piano.

Here’s another example, one that sends the F1 key to Notepad:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate("Test.ps1 - Notepad")

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("{F1}")

In turn, that will cause Notepad’s help file to appear onscreen.

As this example implies, you can send “special keys” (like the function keys) to an application. Here’s a list of those special keys and their SendKeys equivalents:

Key

Code

BACKSPACE

{BACKSPACE}, {BS}, or {BKSP}

BREAK

{BREAK}

CAPS LOCK

{CAPSLOCK}

DEL or DELETE

{DELETE} or {DEL}

DOWN ARROW

{DOWN}

END

{END}

ENTER

{ENTER}or ~

ESC

{ESC}

HELP

{HELP}

HOME

{HOME}

INS or INSERT

{INSERT} or {INS}

LEFT ARROW

{LEFT}

NUM LOCK

{NUMLOCK}

PAGE DOWN

{PGDN}

PAGE UP

{PGUP}

RIGHT ARROW

{RIGHT}

SCROLL LOCK

{SCROLLLOCK}

TAB

{TAB}

UP ARROW

{UP}

F1

{F1}

F2

{F2}

F3

{F3}

F4

{F4}

F5

{F5}

F6

{F6}

F7

{F7}

F8

{F8}

F9

{F9}

F10

{F10}

F11

{F11}

F12

{F12}

F13

{F13}

F14

{F14}

F15

{F15}

F16

{F16}

Keypad add

{ADD}

Keypad subtract

{SUBTRACT}

Keypad multiply

{MULTIPLY}

Keypad divide

{DIVIDE}

To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes.

Key

Code

SHIFT

+

CTRL

^

ALT

%

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page