SendKeys Statement

See Also9HQLBN              Example13LIR6B>Low

Sends one or more keystrokes to the active window as if they had been entered at the keyboard.

Syntax

 SendKeys keytext [,wait]

Remarks

The SendKeys statement has these parts:

Part               Description

 

keytext          String expression1330R89 sent to the active window.

wait               Numeric expression71RISN. If wait is True, keystrokes must be processed before control is returned to the procedure.  If wait is False, control is returned to the procedure immediately after the keys are sent.  If wait is omitted, False is assumed as the default.

Each key is represented by one or more characters.  To specify a single keyboard character, use the character itself.  For example, to represent the letter A, use "A" for keytext.  If you want to represent more than one character, append each additional character to the one preceding it.  To represent the letters A, B, and C, use "ABC" for keytext.

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) have special meanings to SendKeys.  To specify one of these characters, enclose it inside braces.  For example, to specify the plus sign, use { +}.  Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces as well, because in other applications for Microsoft Windows, brackets do have special meaning that may be significant when dynamic data exchangeDEFDDE (DDE) occurs.  To send brace characters, use { { } and { }}.

To specify characters that aren't displayed when you press a key (such as Enter or Tab) and keys that represent actions rather than characters, use the codes shown below:

Key

Code

Key

Code

 

 

Backspace

{ BACKSPACE} or { BS} or { BKSP}

Break

{ BREAK}

Caps Lock

{ CAPSLOCK}

Clear

{ CLEAR}

Del

{ DELETE} or { DEL}

Down Arrow

{ DOWN}

End

{ END}

Enter

{ ENTER} or ~

Esc

{ ESCAPE} or { ESC}

Help

{ HELP}

Home

{ HOME}

Ins

{ INSERT}

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 specify keys combined with any combination of Shift, Ctrl, and Alt keys, precede the regular key code with one or more of the following codes:

Key               Code

 

Shift              +

Control          ^

Alt                 %

 

To specify that Shift, Ctrl, and/or Alt should be held down while several other keys are pressed, enclose the keys' code in parentheses.  For example, to have the Shift key held down while E and C are pressed, use "+(EC)".  To have Shift held down while E is pressed, followed by C being pressed without Shift, use "+EC".

To specify repeating keys, use the form key number};  you must put a space between key and number.  For example, { LEFT 42} means press the Left Arrow key 42 times; { h 10} means press h 10 times.

 

Note   SendKeys can't send keystrokes to an application that is not designed to run in Microsoft Windows.  Sendkeys also can't send the Print Screen (PRTSC) key to any application.

 


See Also

DoEvents Function, DoEvents StatementNPKPD0


SendKeys Statement Example

The example uses Shell to run the Calculator application included with Microsoft Windows; it then uses the SendKeys statement to send keystrokes to either quit the Calculator or add some numbers, depending on the user's choice.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim I, Msg, X                         ' Declare variables.

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

   For I = 1 To 100                      ' Set up counting loop.

      SendKeys I & "{ +}", True           ' Send keystrokes to Calculator

   Next I                                '  to add each value of I.

   SendKeys "=", True                    ' Get grand total.

   AppActivate "Microsoft Visual Basic"  ' Return focus to Visual Basic.

   Msg = "Choose OK to close Calculator."      ' Stop to see results.

   MsgBox Msg

   AppActivate "Calculator"              ' Return focus to Calculator.

   SendKeys "%{ F4}", True                ' Send Alt+F4 to close Calculator.

End Sub