How to: start an application and send it keystrokes (Visual Basic)

This example uses the Shell method to start the Notepad application and then prints a sentence by sending keystrokes using the My.Computer.Keyboard.SendKeys method.


Dim ProcID As Integer
' Start the Notepad application, and store the process id.
ProcID = Shell("NOTEPAD.EXE", AppWinStyle.NormalFocus)
' Activate the Notepad application.
' 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.

Robust programming

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

.NET Framework Security

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

See also