Send Keys Statement

Named Arguments


SendKeys string[, wait] string

Use: Required

Data Type: String

The keystrokes to send.


Use: Optional

Data Type: Boolean

Expression evaluating to True or False denoting the wait mode. Description

Programmatically simulates specified keys being typed at the keyboard. Rules at a Glance

• SendKeys sends its keystrokes to the application and application window that has the focus.

• One or more characters represent each key.

• The default setting for wait is False. Setting wait to True informs the application to wait until the keystrokes have been processed before passing control back to the current procedure. A False setting returns control back to the current procedure as soon as the keys are sent.

508 Chapter 7 - The Language Reference

• To send normal alphabetical or numeric characters, simply use the character or characters enclosed in quotation marks. For example, "SOME Text 123".

• The following characters represent special keys or have special meaning within the SendKeys string:


Special Key Representation









[ ]

May be used by Dynamic Data Exchange (DDE)

To use these characters literally, you must surround the character with braces. For example, to specify the percentage key, use {%}.

• Preceding a string with the special characters described in the table above allows you to send a keystroke combination beginning with Shift, Ctrl, or Alt. For example, to specify Ctrl followed by M, use AM.

• If you need to specify that the Shift, Ctrl, or Alt key is held down while another key is pressed, you should enclose the key or keys in parentheses and precede the parentheses with the special character code. For example to specify the M key being pressed while holding down the Alt key use %(M).

• The following table describes how to specify nondisplaying (action) characters in the SendKeys string:



Back Space

{Backspace}, {Bs}, or {Bksp}



Caps Lock


Del or Delete

{Del} or {Delete}

Down arrow





{Enter} or ~







Ins or Insert

{Ins} or {Insert}

Left arrow


Num Lock


Page Down


Page Up


Right arrow


Scroll Lock




SendKeys Statement 509


Up arrow

F15 F16

• Special formatting syntax allows you to specify a key being repeatedly pressed. The syntax is:

{key numberoftimes}

For example, "{M 3}" represents pressing the M key three times. Example

The following program launches Notepad, loads a text file whose name is passed as a parameter, gives the focus to Notepad, then uses its File Exit menu option to close the application:

Private Sub LaunchNotepad(strFN As String)

Dim lngTaskID As Long Dim strCmdLine As String strCmdLine = "C:\windows\notepad.exe " & strFN lngTaskID = Shell(strCmdLine, vbNormalNoFocus) ' timing delay DelayLoop 100000

AppActivate lngTaskID, False DoEvents ' timing delay DelayLoop 100000

SendKeys "%Fx", False

End Sub

510 Chapter 7- The Language Reference

• SendKeys works directly only with applications designed to run in Microsoft Windows. To send keystrokes to an MS-DOS application or to the console window, you must use the Clipboard as an intermediary. For example, the following subroutine uses the Clipboard and SendKeys to launch a command or program from the DOS window:

Private Sub RunDOSCommand(strCmd As String) Dim lngCtr As Long

Shell "", vbNormalNoFocus Clipboard.Clear

Clipboard.SetText strCmd & Chr(13) AppActivate "MS-DOS Prompt", False SendKeys "% ep". True For lngCtr = 0 To 700000 Next

SendKeys "% c". True AppActivate Me.Name End Sub

• You may find that some keys or key combinations can't be sent successfully. For example, you can't use SendKeys to send the Print Screen key to any application. And you can't send the Alt-Tab keys (%{Tab}) under Windows 9x.

• Typically, SendKeys is used as a "convenience" feature to send an occasional keystroke to its application or to another application. It can also add a keystroke macro capability to an application. In some cases, it's used for remotely controlling an application. In this latter case, SendKeys is often combined with the Shell function to start an instance of another application and with the AppActivate statement to give it the focus before SendKeys is used; the previous example illustrates this.

• It's worthwhile mentioning the difficulties of using SendKeys as a method for controlling a program remotely. Windows is an event-driven operating system. Direct consequences of this are that the order of events is controlled primarily by the user, and the precise order of events is difficult or even impossible to anticipate in advance. Remote control of an application using SendKeys, however, typically makes a number of assumptions about that application, the most basic of which is that it has the focus when SendKeys is called. Given that SendKeys doesn't offer close control over a remote application in the same way that, for instance, OLE automation does, the event-driven character of Windows can easily intervene to invalidate those assumptions. This makes SendKeys a less than optimal tool for remotely controlling an application.

SendKeys Statement 511

+1 0

Post a comment