Editare

Partajați prin


Keyboard.SendKeys Method

Definition

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

Overloads

SendKeys(String)

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

SendKeys(String, Boolean)

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

SendKeys(String)

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

public:
 void SendKeys(System::String ^ keys);
public void SendKeys (string keys);
member this.SendKeys : string -> unit
Public Sub SendKeys (keys As String)

Parameters

keys
String

A String that defines the keys to send.

Exceptions

A partial-trust situation exists in which the user lacks necessary permissions.

Examples

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

Dim ProcID As Integer
' Start the Notepad application, and store the process id.
ProcID = Shell("NOTEPAD.EXE", AppWinStyle.NormalFocus)
' Activate the Notepad application.
AppActivate(ProcID)
' Send the keystrokes to the Notepad application.
My.Computer.Keyboard.SendKeys("I ", True)
My.Computer.Keyboard.SendKeys("♥", True)
My.Computer.Keyboard.SendKeys(" Visual Basic!", True)
' The sentence I ♥ Visual Basic! is printed on Notepad.

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

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

Remarks

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.

Note

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}

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

See also

Applies to

SendKeys(String, Boolean)

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

public:
 void SendKeys(System::String ^ keys, bool wait);
public void SendKeys (string keys, bool wait);
member this.SendKeys : string * bool -> unit
Public Sub SendKeys (keys As String, wait As Boolean)

Parameters

keys
String

A String that defines the keys to send.

wait
Boolean

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

Exceptions

A partial-trust situation exists in which the user lacks necessary permissions.

Examples

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

Dim ProcID As Integer
' Start the Notepad application, and store the process id.
ProcID = Shell("NOTEPAD.EXE", AppWinStyle.NormalFocus)
' Activate the Notepad application.
AppActivate(ProcID)
' Send the keystrokes to the Notepad application.
My.Computer.Keyboard.SendKeys("I ", True)
My.Computer.Keyboard.SendKeys("♥", True)
My.Computer.Keyboard.SendKeys(" Visual Basic!", True)
' The sentence I ♥ Visual Basic! is printed on Notepad.

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

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

Remarks

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.

Note

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}

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

See also

Applies to