SendKeys Statement |
Yes
SendKeys string[, wait]
string
Use: Required
Data Type: String
The keystrokes to send.
wait
Use: Optional
Data Type: Boolean
Expression evaluating to True or False denoting the wait mode.
Programmatically simulates specified keys being typed at the keyboard.
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.
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:
Character | Special Key Representation |
---|---|
+ | Shift |
^ | Ctrl |
% | Alt |
~ | Enter |
[ ] | 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 ^M.
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:
Key | Code |
---|---|
Back Space | {Backspace}, {Bs}, or {Bksp} |
Break | {Break} |
Caps Lock | {CapsLock} |
Del or Delete | {Del} or {Delete} |
Down arrow | {Down} |
End | {End} |
Enter | {Enter} or ~ |
Esc | {Esc} |
Help | {Help} |
Home | {Home} |
Ins or Insert | {Ins} or {Insert} |
Left arrow | {Left} |
Num Lock | {Numlock} |
Page Down | {Pgdn} |
Page Up | {Pgup} |
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} |
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.
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 otepad.exe " & strFN lngTaskID = Shell(strCmdLine, vbNormalNoFocus) ' timing delay DelayLoop 100000 AppActivate lngTaskID, False DoEvents ' timing delay DelayLoop 100000 SendKeys "%Fx", False End Sub
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 "Command.com", 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.