SendKey

Sends keystrokes to the currently active window.

Syntax:

SendKey (char-string [,delay])

Parameters:

(s) char-string string of regular and/or special characters.

(f) delay [optional] number of seconds to delay between keystrokes sent to the currently active or targeted window.

Returns:

(i) always 0.

 

Note: SendKey will send keystrokes to the currently active window. For many applications, the related functions, SendKeysChild, SendKeysTo or SendMenusTo may be better alternatives.

This function is used to send keystrokes to the active window, just as if they had been entered from the keyboard. Any alphanumeric character, and most punctuation marks and other symbols which appear on the keyboard, may be sent simply by placing it in the "char-string". In addition, the following special characters, enclosed in "curly" braces, may be placed in "char-string" to send the corresponding special characters:

Key 

SendKey equivalent

~

{~}  ; This is how to send a ~

{!}  ; This is how to send a !

^

{^}  ; This is how to send a ^

{+}  ; This is how to send a +

{

{ { }  ; This is how to send a {

{ } }  ; This is how to send a }

Alt

{ALT}

Backspace 

{BACKSPACE} or {BS}

Clear 

{CLEAR}

Ctrl-Break

{CANCEL}

Delete 

{DELETE} or {DEL}

Down Arrow 

{DOWN}

End 

{END}

Enter 

{ENTER} or ~

Escape 

{ESCAPE} or {ESC}

F1 through F16

{F1} through {F16}

Help 

{HELP}

Home 

{HOME}

Insert 

{INSERT} or {INS}

Left Arrow 

{LEFT}

Page Down 

{PGDN}

Page Up 

{PGUP}

Pause  

{PAUSE}

Right Arrow

{RIGHT}

Space 

{SPACE} or {SP}

Tab 

{TAB}

Up Arrow 

{UP}

 

Additional special characters to the SendKey (and SendKeysTo and SendKeysChild) functions:

Key 

SendKey equivalent

0 on numeric keypad

{NUMPAD0}

1 on numeric keypad

{NUMPAD1}

2 on numeric keypad

{NUMPAD2}

3 on numeric keypad

{NUMPAD3}

4 on numeric keypad

{NUMPAD4}

5 on numeric keypad

{NUMPAD5}

6 on numeric keypad

{NUMPAD6}

7 on numeric keypad

{NUMPAD7}

8 on numeric keypad

{NUMPAD8}

9 on numeric keypad

 {NUMPAD9}

* on numeric keypad 

{NUMPAD*}

+ on numeric keypad 

{NUMPAD+}

- on numeric keypad

{NUMPAD-}

. on numeric keypad

{NUMPAD.}

/ on numeric keypad 

{NUMPAD/}

Enter on numeric keypad 

{NUMPAD~}

Print Screen

{PRTSC}

%%

 

In the following, "Standalone" means the key is pressed and then released. "Combination" means the key is held down while the following key is pressed. These may or may not work for any particular application or situation:

Key 

SendKey equivalent

Left Alt key, standalone

{LALT}

Right Alt key, standalone

{RALT}

Left Control key, standalone

{LCONTROL}

Right Control key, standalone 

{RCONTROL}

Left Shift key, standalone 

{LSHIFT}

Right Shift key, standalone

{RSHIFT}

Left Windows key, standalone 

{LWIN}

Right Windows key, standalone 

{RWIN}

Applications key, standalone

{APPS}

Left Alt key, combination 

{*LALT}

Right Alt key, combination

{*RALT}

Left Control key, combination

{*LCONTROL}

Right Control key, combination 

{*RCONTROL}

Left Shift key, combination 

{*LSHIFT}

Right Shift key, combination 

{*RSHIFT}

Left Windows key, combination

{*LWIN} ;see notes below

Right Windows key, combination

{*RWIN} ;see notes below

Applications key, combination

{*APPS}

 

Notes: The left and right Alt, Control, and Shift keys can only be distinguished on Windows NT+. On other platforms they are treated as plain Alt, Control, and Shift keys, respectively.

The "Windows" and Applications" keys are on the Microsoft Natural keyboard.

In the specific case of "{*LWIN}l" or "{*RWIN}l" which would be the keystrokes to swap a currently logged-in user (in Windows XP and newer), a problem occurs where the user session that executed the SendKey misses the key-up of the Windows key. When returning to that user session, the system thinks the Windows key is still depressed, and thus keyboard input does not work properly. The problem can be manually cleared by pressing the Windows key again and hitting Escape to clear the Start Menu that pops up. Thus it is recommended that you do not attempt to use these key combinations in a script to swap users. As an alternative you may wish to look into the RunWithLogon function which allows a program to be run as another user.

To enter an Alt, Control, or Shift key combination, precede the desired character with one or more of the following symbols:

Key 

SendKey equivalent

Alt

 !

Control 

^

Shift 

+

 

To enter Alt-S:

SendKey("!s")

 

To enter Ctrl-Shift-F7:

SendKey("^+{F7}")

 

You may also repeat a key by enclosing it in braces, followed by a space and the total number of repetitions desired.

To type 20 asterisks:

SendKey("{* 20}")

 

To move the cursor down 8 lines:

SendKey("{DOWN 8}")

 

To toggle a checkbox:

SendKey("{+}") ;always checks a checkbox

SendKey("{-}") ;always unchecks a checkbox

 

To type a single percent sign:

SendKey("%%") ;types a single percent sign

 

The optional "delay" parameter can be a floating point number to indicate partial second values.  When the parameter is used in a call, the value overrides any keystroke timing value set using IntControl 35 for the duration of the function call.  If the “char-string” parameter contains only one key value, the delay will occur after the keystroke is sent to the keyboard driver, but before the function returns.

Examples:

; start Notepad, and use *.* for filenames
Run("notepad.exe", "")
SendKey("!fo*.*~")

 

In those cases where you have an application which can accept text pasted in from the clipboard, it will often be more efficient to use the ClipGet function:

 

Run("notepad.exe", "")
; copy some text to the clipboard
ClipPut("Dear Sirs:":@crlf:@crlf)
; paste the text into Notepad (using Ctrl-v)
SendKey("^v")

 

A WIL program cannot send keystrokes to its own WIL Interpreter window.

 

Notes:

You should, in general, use lower-case letters to represent Alt-key combinations and other menu shortcut keys as that is the normal keys used when typing to application. For example "!fo" is interpreted as Alt-f-o, as one might expect. However "!FO" is interpreted as Alt-Shift-f-o, which is not a normal keystroke sequence.

If your SendKey statement doesn't seem to be working (e.g., all you get are beeping noises), you may need to place a WinActivate statement before the SendKey statement to insure that you are sending the keystrokes to the correct window, or you may try using the SendKeysTo or SendKeysChild function.

When sending keystrokes to a DOS box, the DOS box must be in windowed mode (not full screen). Most keystrokes can be sent to a full screen DOS box, however, SendKey can only send the ENTER key to a Windowed DOS Box. In order to assure that all keystrokes are sent ( including the enter key ), the DOS application should be run in windowed mode.  

The function IgnoreInput may be used to block user input during a sendkey operation. If IgnoreInput is enabled, it will be temporarily disabled when using SendKey (and SendKeysTo and SendKeysChild) to send an Alt key combination in Windows 2000 or newer.

See Also:

SendKeysTo, SendKeysChild, SendMenusTo, SnapShot, IgnoreInput, WinActivate, IntControl 35, IntControl 43, SysParamInfo 4107