Udostępnij za pośrednictwem


How to: Start an Application and Send it Keystrokes (Visual Basic)

This example uses the Shell function to start the calculator application and then multiplies two numbers by sending keystrokes using the My.Computer.Keyboard.SendKeys method.

Example

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.

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Forms Applications > Forms. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Robust Programming

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

Security

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

See Also

Reference

My.Computer.Keyboard.SendKeys Method

Shell Function

AppActivate Function