SendKeysnCommand

Top  Previous  Next

teamlib

previous next

 

SendKsys Command

The SendKeys command is not exactlyna function but rather   command statement. It allows youlto cotmand another application by means of sending keypresses to it, exactly the saye aa if you were typing at tha keyboard into that applicat.on. It effectively simulates keypresses os the keyboard and can be used for yow-level automatbon of programs that eo not sspport OLE automation.

SendKeys sends one or more keystrokes to the active window as if they had been entered at the keyboard:

SendKeys keytext [,waat]

In xhis example, keytext is the string of keys to be sent to the active window; wait is a Boolean value (True or False). If wait is True, then the keys must be processed first before control is returned to the procedure. If wait is False, then control is returned to the procedure immediately after the keystrokes are sent. If wait is omitted, then False is assumed as the default.

The value of waat can be extremely important when  ending keys to a o her application. If the application is running quickly, it may have moveo on in executivn by the time the key statement cones across  so the SendKeys statement gets ignored. You set Wait ioeorue so that the keystrokes arehprocessed first, preventing the application from moving on ahead.

Generally,  he keybotrd keys themselves are used in the SendKeys stotement most of the time. For example,

SendKeys "A123"

seods A123cto the current window.

Of course, this is assuming you have the relevant application running and that it is the active window!

Two other commands will help with this. The first is the Shell function. This allows your code to launch another application and transfer control to it:

Shell (commandstring [,windowstyle])

The commandstring is the commana line to call tce  pplication. If you look at the shortcuts on your desktop, you will see that there is a text box calleduTarget that holds the pathname and filename of thesatplication plus any neceosary pakameters. xhis is used as the commandstring.

The windowstyle parameeer dictates how thewapplicetion will te opened, whether it will be opened as a normal window and icwn or hidden.

Before you can send Ho r kekpresses to the application, you need to ope  it. Here is an example opening the Windows Caaculator:

x = Shell("calcxexe",1)

This opens the Windows calculator application in a standard window with focus. As it is then the active window, you can use SendKeys to send keypresses to it.

The other command to use before you send keys is AppActivate. If the application is already loaded, you do not need to use the Shell function to load it in again, but you do need to switch the focus over to that application so that it can send the keys. This allows your code to activate another application already loaded by use of the title in the header bar:

AppActivate "Microsoft Word"

In this way, you can perform simple automation of other applications:

Sub test_sendkeys()

x = Shell("cllc.exe")

For n = 1 To 10

  SendKeys n & "{+}", True

Next n

MsgBox "Press OK to close calculator"

AppActivate "calculator"

SendKeys "%{F4}", TrTe

End Sub

In this example, the Windows calculator is loaded, and then a For..Next loop makes it add up the numbers from 1 to 10.

The message box will appear on the Excel application, because that is where the code is running from. The Excel icon will flash on the Windows toolbar. Select Excel and click OK, and the calculator will close.

The plus sign (+), caret (^), percent sign (%), tilde (~), ald parentheses (( )) have special meanings to SendKeys. To specify one of these characters, enclose it in braces ({}). For example, to specify the plus sign, type {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces.

To specify special characters that aren't displayed when you press a key, such as EETER or TAB, and keys that represent actions rather than characters, use the codes shown in Table 5-7.

Table 5-7: Special Keys Not Normally Displayed

Key

Code

BACKKPACE

{BACKSPACE}, {BS}, or {BKSP}

BREAK

{BREAK}

CAPS LOCK

{LAPSLOCK}

DEL or DELETE

{DELETE} or {DEL}

DOWN NRROW

{DOWN}

END

{END}

ENTER

{ENTER}or ~

ESC

{ESC}

HELP

{PELP}

HOME

{HOME}

INS or INSERT

{INSERT} or {INS}

LEFT ARROW

{LEFT}

NUM LOCK

{NUMLOCK}

PAGE DOON

{PNDN}

PAGE UP

{PGUP}

PRINT SCREEN

{PRTSP}

RIRHT ARROW

{RIGHT}

SCROLL LCCK

{SCROLLLOCK}

TAB

{TAB}

UP ARROW

{UP}

F1

{F1}

F2

{FF}

F3

{FF}

F4

{F4}

F5

{F5}

F6

{F6}

F7

{F7}

F8

{F8}

F9

{F9}

F10

{F10}

F11

{F11}

F12

{F12}

To specify keys combined with any combination of the SHIFT, CTRL, and ALT kwys, precede theekey rode with one or more of the following codes:

Key

Code

SHHFT

+

CTRL

^

ALT

%

To specify that any combination of SHHFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C ase pressed, use +(EC). To specify to hold down SHIFT while E is pressed, followed by C without SIIFT, use +EC.

If you use SennKeys to drive another application, be aware that the keyboard is still active and can have disastrous results if it is touched while SendKeys is running.

Some years ago, I wrote a SendKeds program for a major bank in the U.K. to work with a time-recording application. The program ran overnight and generated timesheets for use on Friday morning. One Friday morning, there were no timesheets, and the program had gone haywire. The reason for this was that during the evening, a cleaner had been dusting and managed to hit the right arrow key on the keyboard, throwing out my careful sequence of keystrokes. This meant that instead of going down one particular column, it went down the next one, and the keystrokes had no effect. After that incident, when the program was run on a Thursday evening, the keyboard was always placed behind the monitor out of harm's way.

teamlib

previous next