2007/06/17

How to use SendKeys to imitate the keyboard

http://www.tek-tips.com/faqs.cfm?fid=5037

How to use SendKeys to imitate the keyboard


faq707-5037
Posted: 15 Apr 04

If you perform the process using the keyboard you will note that things do not happen instantly on screen - especially the initial opening of a dialog box where the action takes place. The main problem with SendKeys is that the code usually runs faster than Windows can carry out the required activity - this is especially so if manipulating an external (not the one with the code) application or opening a dialog box or menu. We therefore need to slow the action down by using Wait statements. SendKeys also has its own "Wait" argument True or False which needs to be used correctly. One example of using it set to True is when manipulating external applications that require extra time to carry out actions. This is not always sufficient, so extra code is required.

A big key to success in this area is to break down the process into small steps. It does need a bit of trial and error to get it right. Experience shows that operators are prepared to wait a bit longer if it works. Sometimes the speed of external applications varies so much that the job is impossible using SendKeys, when using API calls with KeyUp and KeyDown work better. This area is never an exact science. See VBA Help 'SendKeys' and 'Shell' for more information.

This is some sample code to demonstrate the principle :-


    '------------------------------------------
    Sub SET_A3()
    '- change Printer setting to A3 size
    '- nb. assumes current setting is A4
    Dim AltKey As String
    Dim CtrlKey As String
    Dim ShiftKey As String
    Dim TabKey As String
    Dim EnterKey As String
    '--------------------------
    AltKey = "%"
    CtrlKey = "^"
    ShiftKey = "+"
    TabKey = "{TAB}"
    EnterKey = "~"
    '--------------------------
    '- open print dialog
    SendKeys AltKey & "(FP)", False
    '- delay 1 second
    Application.Wait Now + TimeValue("00:00:01")
    '- open printer properties
    SendKeys AltKey & "(R)", False
    '- tab to paper size
    SendKeys TabKey, False
    SendKeys TabKey, False
    '- change A4 to A3
    SendKeys "{UP}", False
    '- Enter twice
    SendKeys EnterKey, False
    SendKeys EnterKey, False
    End Sub
    '----------------------------------------------



SendKeys Statement, (VBA help)


Sends one or more keystrokes to the active window as if typed at the keyboard.

Syntax

SendKeys string[, wait]

The SendKeys statement syntax has these named arguments:

Part Description
string Required. String expression specifying the keystrokes to send.
Wait Optional. Boolean value specifying the wait mode. If False (default), control is returned to the procedure immediately after the keys are sent. If True, keystrokes must be processed before control is returned to the procedure.

Remarks

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 string. 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 string.

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that may be significant when dynamic data exchange (DDE) occurs. To specify 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
    BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
    BREAK {BREAK}
    CAPS LOCK {CAPSLOCK}
    DEL or DELETE {DELETE} or {DEL}
    DOWN ARROW {DOWN}
    END {END}
    ENTER {ENTER}or ~
    ESC {ESC}
    HELP {HELP}
    HOME {HOME}
    INS or INSERT {INSERT} or {INS}
    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 the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes:


    Key Code
    SHIFT +
    CTRL ^
    ALT %


To specify that any combination of SHIFT, 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 are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C 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 You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows or Macintosh. Sendkeys also can't send the PRINT SCREEN key {PRTSC} to any application.

SendKeys Statement Example


This example uses the Shell function to run the Calculator application included with Microsoft Windows. It uses the SendKeys statement to send keystrokes to add some numbers, and then quit the Calculator. (To see the example, paste it into a procedure, then run the procedure. Because AppActivate changes the focus to the Calculator application, you can't single step through the code.). On the Macintosh, use a Macintosh application that accepts keyboard input instead of the Windows Calculator.


    Dim ReturnValue, I
    ReturnValue = Shell("CALC.EXE", 1) ' Run Calculator.
    AppActivate ReturnValue ' Activate the 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.
    SendKeys "%{F4}", True ' Send ALT+F4 to close Calculator.





No comments: