SendKeys (statement)

<< Click to Display Table of Contents >>

SendKeys (statement)

SendKeys(Keys, [Wait])

Simulates keystrokes.

Keys is a string containing the keys to be pressed.

If the optional parameter Wait is True, control returns to the script only when the receiving application has completed processing of the keystroke.

To pass "regular" keys, just type them – for example, "Test". Special keys such as the Enter or Alt key can be added as follows:

The keys + ^ ~ % ( ) [ ] { and } have a special meaning. If you want to use them verbatim, they must be enclosed by curly braces – for example: "{%}" or {(}.

Special keys such as the Enter key must be also enclosed by curly braces – for example: {Enter}. You can find a list of the special keys in the next section Special keys supported by the SendKeys command.

Key combinations containing the Shift, Alt and Ctrl keys can be added using one of the following prefixes (+, ^ or %):

Shift+Enter:        "+{Enter]"
Alt+F4:        "%{F4}"
Strg+C:        "^c" (not ^C!)
Pay attention to case: For example, "^c" represents the key combination Ctrl+C, but "^C" represents Ctrl+Shift+C.

If quotation marks need to be passed, they must be doubled – for example, "Arthur ""Two Sheds"" Jackson".

A sequence of keys can be added by following the keystrokes with the number of repetitions, all enclosed by curly braces: "{a 10}" repeats the key a ten times, {Enter 2} repeats the Enter key twice.

The Enter key can be also expressed with the code ~. The code "ab~cd", for example, is identical to "ab{Enter}cd"

Example:

Sub Main

 X = Shell("Calc.exe", 1)        ' Invoke the Calculator application

 For i = 1 To 5

         SendKeys i & ".+}", True    ' Send keystrokes

 Next i

 Msg = "The calculator will be closed now."

 MsgBox Msg

 AppActivate "Calculator"        ' Set the focus to the calculator

 SendKeys "%{F4}", True          ' Send Alt+F4 to close the application

End Sub