Send, SendWait Methods


Send, SendWait Methods

Class

System.Windows.Forms.SendKeys

Syntax

     SendKeys.Send(keys) 

or:

     SendKeys.SendWait(keys) 


keys (required; String)

String that describes the keystrokes to send to the active window

Description

The Send and SendWait methods simulate the typing of one or more keys in the active window. With SendKeys.Send, further execution continues without waiting for the keys to be processed. With SendKeys.SendWait, further execution is suspended until the keystrokes have been processed.

To send plain text, simply include that text in the keys argument. For instance, using "abc" for the keys argument will send the characters a, b, and c, one at a time, to the active window. To have the Shift key held down with a key, precede that key with a plus sign (+). To use the Control key with another key, precede that key with the caret (^). To use the Alt key with another key, precede that key with the percent sign (%). To use one of these special keys with multiple other keys, enclose those other keys in parentheses. For instance, "+(abc)" sends a, b, and c with the Shift key held down.

You can repeat a key multiple times by using the syntax "{key count}" (that is, the character, then a space, and then a numeric value, all within braces). For example, "{a 25}" will send the "a" key 25 times.

These methods support several special keys, such as the Left Arrow key. Also, some standard keys must be enclosed in a set of braces to be recognized as standard keys. The following table lists all of these special keys and special-use standard keys.

To include

Use this text

Backspace

{BACKSPACE} or {BS} or {BKSP}

Break

{BREAK}

Caps Lock

{CAPSLOCK}

Caret (^)

{^}

Clear

{CLEAR}

Close Brace (})

{}}

Close Bracket (])

{]}

Close Parenthesis (")")

{)}

Delete

{DELETE} or {DEL}

Down Arrow

{DOWN}

End

{END}

Enter

~

Escape

{ESCAPE} or {ESC}

F1 through F16

{F1} tHRough {F16}

Help

{HELP}

Home

{HOME}

Insert

{INSERT} or {INS}

Keypad Add

{ADD}

Keypad Divide

{DIVIDE}

Keypad Enter

{ENTER}

Keypad Multiply

{MULTIPLY}

Keypad Subtract

{SUBTRACT}

Left Arrow

{LEFT}

Num Lock

{NUMLOCK}

Open Brace ({)

{{}

Open Bracket ([)

{[}

Open Parenthesis ("(")

{(}

Page Down

{PGDN}

Page Up

{PGUP}

Percent Sign (%)

{%}

Plus (+)

{+}

Print Screen

{PRTSC}

Return

{RETURN}

Right Arrow

{RIGHT}

Scroll Lock

{SCROLLLOCK}

Tab

{TAB}

Tilde (~)

{~}

Up Arrow

{UP}


Usage at a Glance

  • Send and SendWait will only work directly with applications designed to run in Microsoft Windows.

  • You may find that some keys or key combinations cannot be sent successfully. For example, you cannot send the Print Screen key to any application. Also, you cannot send the Alt+Tab key combination ("%{Tab}").

  • Because of the event-driven nature of Windows, and because the user can change to a different active window at any time, there is no guarantee that the keys you intend to send to a window will ever arrive at that window.

Example

The following program uses the Notepad application to add some text to the clipboard.

     Dim notepadID As Integer     ' ----- Start and activate the Notepad.     notepadID = Shell("notepad.exe", AppWinStyle.NormalFocus)     AppActivate(notepadID)     Windows.Forms.Application.DoEvents(  )     ' ----- Add some text.     SendKeys.SendWait("+visual +basic~")     SendKeys.SendWait("{- 12}~")     SendKeys.SendWait("+it's fun{!}")     ' ----- Select all text with Control+A, then copy with Control+C.     SendKeys.SendWait("^(a)")     SendKeys.SendWait("^(c)")     ' ----- Quit Notepad.     SendKeys.SendWait("%{F4}")     SendKeys.SendWait("n")     ' ----- See if we copied the text correctly.     MsgBox(My.Computer.Clipboard.GetText(  )) 

Version Differences

Visual Basic 2005 includes the My.Computer.Keyboard.SendKeys method, which provides equivalent functionality.

See Also

AppActivate Procedure




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net