VB.NET Language in a Nutshell

   
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

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}

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

   

Категории