SK.SendKeys

Previous  Next

 

result = SK.SendKeys( strKeyString AS STRING,

                      bWait        AS INTEGER = 0 ) AS INTEGER

 


 

SendKeys sends one or more characters to an applications window or to the desktop window.  The sent keys are sent to no specific application, instead they are just pressed and whatever application has the keyboard input will take the keys.

 

The function returns 1 (TRUE) if successful otherwise 0 (FALSE).

 

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', pass in the string "a" to the method. Naturally to represent a string of characters just pass them in order as "hello".

 

If you want to send modifier keys such as the SHIFT, ALT, CONTROL or WINKEY keys in addition to normal keys, you might want to use any of the characters defined in Table 3.

 

For example, if you want to send "A" you usually press Shift+A, which is equivalent to sending these key strokes: "+a" , similarly to send the "~" you would press Shift+` which is equivalent to key strokes "+`" or simply "{TILDE}" (Table 1.b).

 

All characters in Table 3 are reserved and have special meaning in addition to the left/right parenthesis/braces.

 

The parenthesis are used to associate a given modifier or modifiers with a group of characters, for example to send the "HELLO", you would describe as "+(hello)" which informs SendKeys to depress the SHIFT key while sending the following keys group. Whereas the braces are used to enclose any of the keys displayed in Table 1 and 2.

 

The following table is a slightly modified version of the MSDN/SendKeys help:

 

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                        {INS}

LEFT ARROW                           {LEFT}

NUM LOCK                             {NUMLOCK}

PAGE DOWN                            {PGDN}

PAGE UP                              {PGUP}

PRINT SCREEN                         {PRTSC} (reserved for future use)

RIGHT ARROW                          {RIGHT}

SCROLL LOCK                          {SCROLL}

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}

Keypad add                           {ADD}

Keypad subtract                      {SUBTRACT}

Keypad multiply                      {MULTIPLY}

Keypad divide                        {DIVIDE}

 

The following are SendKeys additions:

 

Key                                  Code                                        

+                                    {PLUS}

@                                    {AT}

APPS                                 {APPS}

^                                    {CARET}

~                                    {TILDE}

{ }                                  {LEFTBRACE} {RIGHTBRACE}

( )                                  {LEFTPAREN} {RIGHTPAREN}

Left/Right WINKEY                    {LWIN} {RWIN}

WINKEY                               {WIN} equivalent to {LWIN}

 

In addition to this, there are some special keys that act like commands:

 

Command                              Actione                                     

{VKEY X}                             Sends the VKEY of value X.

                                     Very useful if you don't want to recompile

                                     SendKeys and add new Vkey to the hardcoded

                                     special keys table.

 

                                     For example, {VKEY 13} is equivalent to

                                     VK_RETURN.

 

{BEEP X Y}}                          Beeps with a frequency of X and a duration of

                                     Y milliseconds.

 

{DELAY X}                            Delays sending the next key of X milliseconds.

                                     After the delaying the following key, the

                                     subsequent keys will not be further delayed

                                     unless there is a default delay value

                                     (see DELAY=X).

                                     Example: {DELAY 1000} <-- delays subsequent

                                     key stroke for 1 second.

 

{DELAY=X}                            Sets the default delay value to X milliseconds.

                                     This will cause every key to be delayed X ms.

                                     If a value is already set and you specify

                                     {DELAY Y} you will have your following key

                                     delay Y ms but the subsequent keys will be

                                     delayed X ms.

 

                                     Example: {DELAY=1000} <-- all subsequent keys

                                     will be delayed for 1 second.

 

{APPACTIVATE WindowTitle}            Activates an application using is WindowTitle.

 

                                     Very useful if you want to send different keys

                                     to different applications.

 

Key                                  Code                                        

WINKEY                               @

SHIFT                                +

CTRL                                 ^

ALT                                  %

 

Here are some examples:

 

Keystrokes                           Description                                 

{DELAY=50}@rnotepad~hello world%ha   1) Set delay after each character to 50 ms

                                     2) WINKEY+R to invoke the run dialog

                                     3) Type "notepad" and press ENTER

                                     4) Type "hello world"

                                     5) Invoke Alt+H then press "A" to invoke

                                        the about dialog of notepad

 

{DELAY=100}{APPACTIVATE Calculator}

{ESC}5*7~{BEEP 1000 500}

^c{APPACTIVATE Notepad}

^a{DEL}Result of 5*7 is: ^v          Given that "Calc.exe" and "Notepad.exe" are

                                     running:

                                     1) Set delay to 100 ms

                                     2) Activate calculatr

                                     3) Press ESC to clear previous result

                                     4) Type in 5*7 then press ENTER

                                     5) Beep for 500ms with a frequency of 1000

                                     6) Press CTRL+C to copy result

                                     7) Activate notepad

                                     8) Press CTRL+A then DEL in notepad to

                                        delete previously written text

                                     9) Type in a phrase then press CTRL+V to

                                        paste the copied result

 

{DELAY=500}{NUMLOCK}{CAPSLOCK}

{SCROLL}{SCROLL}{CAPSLOCK}{NUMLOCK}  1) Press NUM,CAPS,SCROll lock in order

                                     2) Turn them off in reverse order

 

{DELAY=500}% {DOWN 5}                1) Press ALT+SPACE

                                     2) Press DOWN key 5 times

 

Example:

 

' Start Notepad and insert "hello world"

SK.SendKeys( "{DELAY=50}@rnotepad~hello world%ha" )

 

' Activate Window with title SendKeys

SK.AppActivate( "SendKeys" )

 

' Send Tools->Options to the Explorer Window SendKeys

SK.SendKeys( "{DELAY=50}%to{DELAY=2000}{ENTER}" )

 

' Set new default delay 100 ms

SK.SetDelay( 100 )

 

' Send Edit->Select All to Explorer Window SendKeys

SK.SendKeys( "{DELAY}%ea" )

 

' Scroll through the Keyboard LEDs 5 times

FOR i = 1 TO 5

  SK.SendKeys( "{DELAY=500}{NUMLOCK}{CAPSLOCK}{SCROLL}{SCROLL}{CAPSLOCK}{NUMLOCK}" )

ENDFOR