Export (0) Print
Expand All

My.Computer.Keyboard.SendKeys Method 

Sends one or more keystrokes to the active window, as if typed on the keyboard.


' Usage
My.Computer.Keyboard.SendKeys(keys)
My.Computer.Keyboard.SendKeys(keys ,wait)
' Declaration
Public Sub SendKeys( _
   ByVal keys As String _
)
' -or-
Public Sub SendKeys( _
   ByVal keys As String, _
   ByVal wait As Boolean _
)

Parameters

keys

A String that defines the keys to send.

wait

Optional. A Boolean that specifies whether or not to wait for keystrokes to get processed before the application continues. True by default.

The following condition can cause an exception:

  • A partial-trust situation exists in which the user lacks necessary permissions (SecurityException).

The My.Computer.Keyboard.SendKeys method provides functionality similar to the Send and SendWait methods.

The wait argument is useful if the other application must finish before your application can continue.

NoteNote

Because there is no managed method to activate another application, you can either use this class within the current application, manually select the window to send the keys to, or use Windows API methods, such as FindWindow and SetForegroundWindow, to force focus on other applications. For more information, see Walkthrough: Calling Windows APIs.

The keys argument can specify any single key or any key combined with ALT, CTRL, or SHIFT (or any combination of those keys). Each key is represented by one or more characters, such as a for the character "a", or {ENTER} for the ENTER key.

To combine a key with SHIFT, precede the key code with + (plus sign). To combine a key with CTRL, precede the key code with ^ (caret). To combine a key with ALT, precede the key code with % (percent sign). To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means "press the LEFT ARROW key 42 times"; {h 10} means "press 'h' 10 times."

The following table lists the codes that can be used to specify characters that are not displayed when you press the corresponding key (such as ENTER or TAB).

Key Code

BACKSPACE

{BACKSPACE} or {BS}

BREAK

{BREAK}

CAPS LOCK

{CAPSLOCK}

CLEAR

{CLEAR}

DELETE

{DELETE} or {DEL}

DOWN ARROW

{DOWN}

END

{END}

ENTER (numeric keypad)

{ENTER}

ENTER

~

ESC

{ESCAPE} or {ESC}

HELP

{HELP}

HOME

{HOME}

INS

{INSERT}

LEFT ARROW

{LEFT}

NUM LOCK

{NUMLOCK}

PAGE DOWN

{PGDN}

PAGE UP

{PGUP}

RETURN

{RETURN}

RIGHT ARROW

{RIGHT}

SCROLL LOCK

{SCROLLLOCK}

TAB

{TAB}

UP ARROW

{UP}

F1 through F15

{F1} through {F15}

This example uses the My.Computer.Keyboard.SendKeys method to send keystrokes to an external application, the Calculator application, started by the Shell function.

Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
' Send the keystrokes to the Calculator application.
My.Computer.Keyboard.SendKeys("22", True)
My.Computer.Keyboard.SendKeys("*", True)
My.Computer.Keyboard.SendKeys("44", True)
My.Computer.Keyboard.SendKeys("=", True)
' The result is 22 * 44 = 968.

A ArgumentException exception is raised if an application with the requested process identifier cannot be found.

The call to the Shell function requires full trust (SecurityException class).

Namespace: Microsoft.VisualBasic.Devices

Class: Keyboard

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

No

Windows Service

Yes

Web Site

No

The following permissions may be necessary:

Permission Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

UIPermission

Controls the permissions related to user interfaces and the clipboard. Associated enumeration: AllWindows.

For more information, see Code Access Security and Requesting Permissions.

Community Additions

ADD
Show:
© 2014 Microsoft