ClassSystem.Windows.Forms.SendKeys SyntaxSendKeys.Send(keys) or: SendKeys.SendWait(keys)
DescriptionThe Send and SendWait methods simulate the typing of one or more keys in the active window. With SendKeys.Send, further execution continues without waiting for the keys to be processed. With SendKeys.SendWait, further execution is suspended until the keystrokes have been processed. 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, and then a numeric value, all within braces). For example, "{a 25}" will send the "a" key 25 times. These methods support 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 standard keys. The following table lists all of these special keys and special-use standard keys.
Usage at a Glance
ExampleThe following program uses the Notepad application to add some text to the clipboard. Dim notepadID As Integer ' ----- Start and activate the Notepad. notepadID = Shell("notepad.exe", AppWinStyle.NormalFocus) AppActivate(notepadID) Windows.Forms.Application.DoEvents( ) ' ----- Add some text. SendKeys.SendWait("+visual +basic~") SendKeys.SendWait("{- 12}~") SendKeys.SendWait("+it's fun{!}") ' ----- Select all text with Control+A, then copy with Control+C. SendKeys.SendWait("^(a)") SendKeys.SendWait("^(c)") ' ----- Quit Notepad. SendKeys.SendWait("%{F4}") SendKeys.SendWait("n") ' ----- See if we copied the text correctly. MsgBox(My.Computer.Clipboard.GetText( )) Version DifferencesVisual Basic 2005 includes the My.Computer.Keyboard.SendKeys method, which provides equivalent functionality. See AlsoAppActivate Procedure |