Tuesday, December 04, 2007

Send Keys Constants

VB: SendKeys Statement

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:

String expression (required) 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

To represent ABC abc 123 use "ABC abc 123" for string.

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) are considered special characters. 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 non printing keys such as ENTER or TAB, and function keys:

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-F16 {F1}-{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:

SHIFT = +
CTRL = ^
ALT = %

Combination Keys: 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 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".

Repeating Keys: To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 25} means press the LEFT ARROW key 25 times; {h 15} means press the H key 15 times.
Note: You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows. SendKeys also can not send the PRINT SCREEN key {PRTSC} to any application.

List Commands with SendKeys using:

UtilityProvider.ContextValue(_ValueIndex)
ListVarx
ValueIndex is a zero based index for parameter retrieval, x is a one based index for parameter retrieval.
Examples for "Move <1to5>" command where contains UP, DOWN, RIGHT, LEFT and <1to5> contains 1,2,3,4,5

UtilityProvider.ContextValue(ValueIndex) example
SendKeys "{"+UtilityProvider.ContextValue(0)+" "+UtilityProvider.ContextValue(1)+"}"
ListVarx example
SendKeys "{"+ListVar1+" "+ListVar2+"}"

Parameter Conversion and Control Blocks

Convert Parameter to Number Value: block& = val(_arg1)
Convert Number Value to text (CStr(): SendKeys "{Enter}{Up "+CStr(block&)+"}"

If Statement:

if block& = 0 then
sline = "{Shift+End}"
else
sline = "{Shift+Down "+CStr(block&)+"}{Shift+End}"
end if

While Statement

block& = val(_arg1)
loop& = block&
while ( loop& )
Sendkeys "{End}{Enter}"
loop& = loop& - 1
wend

Substring Search Syntax
for list format "Written\Spoken"

Lc% = Instr(_arg1,"\") - 1
TextVal$=MID$(_arg1,0,Lc%)
SendKeys TextVal$

Diagnostic Example: Test List
SendKeys "1. Selected value:{Enter}"
SendKeys ListVar1
SendKeys "{Enter}2. Count to divisor:{Enter}"
Lc% = InStr(ListVar1,"\")
SendKeys CStr(Lc%)
SendKeys "{Enter}3. Count to cutoff:{Enter}"
Lc% = Lc% - 1
SendKeys CStr(Lc%)
SendKeys "{Enter}4. Resulting Value:{Enter}"
TextVal$=Mid$(ListVar1,1,Lc%)
SendKeys TextVal$
SendKeys "{Enter}Command Complete.{Enter}"

1 comment:

Web Page Scraper said...

Hi all,

The sendkey sub sends keydown and keyup events to the keyboard buffer separately. If you set a break point after a keydown for the Alt, Ctrl or Shift keys or step through the code to such a point, your VB will become unusable and you will have to reboot to clear the keyboard buffer. Thanks for sharing it!