Send Keystrokes to the Active Window using SendKeys (WSH)


WSH (Windows Scripting Host) is a powerful scripting environment that comes with Windows Operating System. By default, there are two scripting languages supported, which is JScript and VBScript. You could actually open any text editor and write the scripts and save as *.vbs or *.js. Double click them invokes the interpreter, which is cscript.exe or wscript.exe depending on the settings. The cscript.exe will output to console (e.g. using WScript.Echo) while the wscript.exe will treat WScript.Echo as MsgBox (output using dialog). Sometimes, you could choose other languages as the host languages in WSH, but this is not installed by default, and you have to download corresponding installer.

The advantages of writing scripts to run in WSH are: (1) you do not need to compile them, just save and run immediately, which are suitable for lightweight small and daily tasks. (2) the scripts can be edited/changed and distributed easily. (3) the same scripts can be interpreted for both 32 and 64 bit, just specify the corresponding cscript.exe or wscript.exe respectively.

You could actually use WScript.Shell object (COM Automation) to SendKeys to the active window, in which case, you could do something as powerful as macros. The keystrokes are being sent to the active window.

The following runs the notepad.exe and bring the notepad to the front. The second parameter 9 means: to active and display the window. If the window is minimized or maximized, the system restores it to its original size and position.

1
2
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad", 9 
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad", 9 

Now the following will type in Hello, World! one character after another, with a short pause 200ms between characters. And finally, it will simulate Alt+F4 to exit. At this time, the dialog will pop up for saving, and press Tab to navigate to Don’t Save button and press Enter to exit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
' Give Notepad time to load
WScript.Sleep 500 
 
Dim Msg: Msg = "Hello, World!"
 
' Type in One Character at a time
For i = 1 To Len(Msg)
    WScript.Sleep 200
    WshShell.SendKeys Mid(Msg, i, 1)
Next
 
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "%{F4}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
' Give Notepad time to load
WScript.Sleep 500 

Dim Msg: Msg = "Hello, World!"

' Type in One Character at a time
For i = 1 To Len(Msg)
	WScript.Sleep 200
	WshShell.SendKeys Mid(Msg, i, 1)
Next

WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "%{F4}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"

notepad-sendkey-vbs Send Keystrokes to the Active Window using SendKeys (WSH) code code library implementation javascript programming languages VBA vbscript windows windows scripting host

The specified keys, functional keys, ctrl, alt are enclosed by {braces}.

vbs-sendkeys Send Keystrokes to the Active Window using SendKeys (WSH) code code library implementation javascript programming languages VBA vbscript windows windows scripting host

You can combine keys, for example, + (plus) is the prefix for shift, Ctrl is denoted using prefix ^ and % (percentage) is for ALT.

Let’s review the above example one more time, but in JScript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(function () {
          var WshShell = WScript.CreateObject("WScript.Shell");
          WshShell.Run("notepad", 9);
          var msg = "Hello, World!";
          // Give Notepad time to load
          WScript.Sleep(500); 
          for (i = 0; i < msg.length; i ++) {
              WScript.Sleep(200);
              WshShell.SendKeys(msg.charAt(i));
          }
          WshShell.SendKeys("{ENTER}");
          // Alt + F4
          WshShell.SendKeys("%{F4}");
          // Naviagate to Don't Save
          WshShell.SendKeys("{TAB}");
          // Exit
          WshShell.SendKeys("{ENTER}");          
})();
(function () {
          var WshShell = WScript.CreateObject("WScript.Shell");
          WshShell.Run("notepad", 9);
          var msg = "Hello, World!";
          // Give Notepad time to load
          WScript.Sleep(500); 
          for (i = 0; i < msg.length; i ++) {
              WScript.Sleep(200);
              WshShell.SendKeys(msg.charAt(i));
          }
          WshShell.SendKeys("{ENTER}");
          // Alt + F4
          WshShell.SendKeys("%{F4}");
          // Naviagate to Don't Save
          WshShell.SendKeys("{TAB}");
          // Exit
          WshShell.SendKeys("{ENTER}");          
})();

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
612 words
Last Post: Simple Loading Bar Made in Processing
Next Post: Paste Command/Utility in Linux Shell

The Permanent URL is: Send Keystrokes to the Active Window using SendKeys (WSH)

One Response

  1. Ed Grossheim

Leave a Reply