Keyboard.SendKeys Keyboard.SendKeys Keyboard.SendKeys Keyboard.SendKeys Method

Definition

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

Overloads

SendKeys(String) SendKeys(String) SendKeys(String) SendKeys(String)

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

SendKeys(String, Boolean) SendKeys(String, Boolean) SendKeys(String, Boolean) SendKeys(String, Boolean)

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

SendKeys(String) SendKeys(String) SendKeys(String) SendKeys(String)

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

public:
 void SendKeys(System::String ^ keys);
[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void SendKeys (string keys);
member this.SendKeys : string -> unit
Public Sub SendKeys (keys As String)
Parameters
keys
String String String 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 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).

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

Security

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.

See Also

SendKeys(String, Boolean) SendKeys(String, Boolean) SendKeys(String, Boolean) 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 String String String

A String that defines the keys to send.

wait
Boolean Boolean Boolean 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 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).

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

Security

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.

See Also

Applies to