SendKeys Method


SendKeys Method

Location

My.Computer.Keyboard.SendKeys

Syntax

 My.Computer.Keyboard.SendKeys(keys[, wait]) 


keys (required; String)

A string of standard and special keys to send to the active window.


wait (optional; Boolean)

Indicates whether to wait for the sent keys to be processed (true) or not (False) before continuing with the next source-code statement. The default is true.

Description

The SendKeys method simulates the typing of one or more keys in the active window.

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, then a numeric value, all within braces). For example, "{a 25}" will send the "a" key 25 times.

SendKeys supports 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 a standard key. 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

  • This method is only valid in non-server applications.

  • This method sends keystrokes to the current active window, whether that window is part of your application or not. Also, some keystrokes you send may cause a different window to become active, depending on the functionality of that window.

  • If you lack the necessary permissions to send the keystrokes to another application, an exception is raised.

Example

The following example starts up Notepad, adds some text to its editing area, copies that text to the clipboard, and exits the program without saving changes.

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

Related Framework Entries

  • Microsoft.VisualBasic.Devices.Keyboard.SendKeys Method

  • System.Windows.Forms.SendKeys.Send Method

  • System.Windows.Forms.SendKeys.SendWait Methods

See Also

Keyboard Object




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