SendKeys Method

SendKeys Method:

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

(for Example:

Dim objSendKey
Set objSendKey = CreateObject(“WScript.shell”)

objSendKey.SendKeys “a”

)

Key Argument
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}
PRINT SCREEN {PRTSC}
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}

To send keyboard characters that are comprised of a regular keystroke in combination with a SHIFT, CTRL, or ALT, create a compound string argument that represents the keystroke combination. You do this by preceding the regular keystroke with one or more of the following special characters:

Key Special Character
SHIFT +
CTRL ^
ALT %
Note
When used this way, these special characters are not enclosed within a set of braces.

To specify that a combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, create a compound string argument with the modified keystrokes enclosed in parentheses. For example, to send the keystroke combination that specifies that the SHIFT key is held down while:

  • e and c are pressed, send the string argument “+(ec)”.
  • e is pressed, followed by a lone c (with no SHIFT), send the string argument “+ec”.

You can use the SendKeys method to send a pattern of keystrokes that consists of a single keystroke pressed several times in a row. To do this, create a compound string argument that specifies the keystroke you want to repeat, followed by the number of times you want it repeated. You do this using a compound string argument of the form {keystroke number}. For example, to send the letter “x” ten times, you would send the string argument “{x 10}”. Be sure to include a space between keystroke and number.

Note
The only keystroke pattern you can send is the kind that is comprised of a single keystroke pressed several times. For example, you can send “x” ten times, but you cannot do the same for “Ctrl+x”.
Note
You cannot send the PRINT SCREEN key {PRTSC} to an application.

Example

Description

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). Each job runs the Windows calculator and sends it keystrokes to execute a simple calculation.

Copy Code
<package>

<job id=”vbs”>

<script language=”VBScript”>

set WshShell = WScript.CreateObject(“WScript.Shell”)

WshShell.Run “calc”

WScript.Sleep 100

WshShell.AppActivate “Calculator”

WScript.Sleep 100

WshShell.SendKeys “1{+}”

WScript.Sleep 500

WshShell.SendKeys “2”

WScript.Sleep 500

WshShell.SendKeys “~”

WScript.Sleep 500

WshShell.SendKeys “*3”

WScript.Sleep 500

WshShell.SendKeys “~”

WScript.Sleep 2500

</script>

</job>

<job id=”js”>

<script language=”JScript”>

var WshShell = WScript.CreateObject(“WScript.Shell”);

WshShell.Run(“calc”);

WScript.Sleep(100);

WshShell.AppActivate(“Calculator”);

WScript.Sleep(100);

WshShell.SendKeys (“1{+}”);

WScript.Sleep(500);

WshShell.SendKeys(“2”);

WScript.Sleep(500);

WshShell.SendKeys(“~”);

WScript.Sleep(500);

WshShell.SendKeys(“*3”);

WScript.Sleep(500);

WshShell.SendKeys(“~”);

WScript.Sleep(2500);

</script>

</job>

</package>

Applies To:

WshShell Object