Send, SendWait Methods

   
Send, SendWait Methods

Class

System. Windows .Forms.SendKeys

Syntax

 SendKeys.Send(   keys   ) SendKeys.SendWait(   keys   ) 
keys (required; String)

String describing keys to send to the active window

Description

Sends keystrokes to the active window of the foreground application. For SendKeys.Send, further execution continues without waiting for the keys to be processed . For SendKeys.SendWait, further execution is suspended until the keystrokes have been processed.

Rules at a Glance

  • To send normal alphabetical or numeric characters, simply use the character or characters enclosed in quotation marks. For example, "SOME Text 123" .

  • The following characters represent special keys or have special meaning within the Keys string:

    Character

    Special key representation

    +

    SHIFT

    ^

    CTRL

    %

    ALT

    ~ or {ENTER}

    ENTER

    To use these characters literally, you must surround the character with braces. For example, to specify the percentage key, use {%} .

  • Preceding a string with the special characters described in the previous table allows you to send a keystroke combination beginning with Shift, Ctrl, or Alt. For example, to specify Ctrl followed by "M," use ^M .

  • If you need to specify that the Shift, Ctrl, or Alt key is held down while another key is pressed, you should enclose the key or keys in parentheses and precede the parentheses with the special character code. For example, to specify the M key being pressed while holding down the Alt key, use %(M) .

  • The following table describes how to specify nondisplaying (action) characters in the Keys string:

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} 

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} 
  • Special formatting syntax allows you to specify a key being repeatedly pressed. The syntax is:

     {   key     numberoftimes   } 

    For example, {M 3} represents pressing the M key three times.

Example

The following program launches Notepad, loads a text file whose name is passed as a parameter, gives the focus to Notepad, then uses its File Exit menu option to close the application:

 Private Sub LaunchNotepad(strFN As String) Dim intTaskID As Integer Dim strCmdLine As String strCmdLine = "C:\windows\notepad.exe " & strFN intTaskID = Shell(strCmdLine, vbNormalNoFocus) ' timing delay DelayLoop(200000) ' Activate notepad by task ID AppActivate(intTaskID) ' timing delay DelayLoop(200000) SendKeys.SendWait("%Fx") End Sub Private Sub DelayLoop(n As Integer) Dim iCtr As Integer For iCtr = 1 to iCtr    if iCtr/10 = iCtr \ 10 Then        Application.DoEvents     End If Next End Sub 

Programming Tips and Gotchas

  • 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 use Send and SendWait to send the Print Screen key to any application. You also cannot send the Alt and Tab keys ( "%{Tab}" ).

  • Typically, Send or SendWait is used as a "convenience" feature to send an occasional keystroke to its application or to another application. It can also be used to add a keystroke-macro capability to an application. In some cases, it is even used for remotely controlling an application. In this latter case, Send or SendWait is often combined with the Shell function (to start an instance of another application) or the AppActivate procedure (to give it the focus before Send or SendWait is used). The example program illustrates this.

  • It's worthwhile mentioning the difficulties of using Send or SendWait as a method for controlling a program remotely. Windows is an event-driven operating system. Consequently, the order of events is controlled primarily by the user , and the precise order of events is difficult or even impossible to anticipate. Remote control of an application using Send or SendWait, however, typically makes a number of assumptions about that application, the most basic of which is that it has the focus when Send or SendWait is called. Given that Send and SendWait do not offer close control over a remote application in the same way as OLE automation does, the event-driven character of Windows can easily intervene to invalidate those assumptions. This makes Send and SendWait less than optimal choices as tools for remote control of an application.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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