如何:启动应用程序并向其发送击键 (Visual Basic)

本示例使用 Shell 方法启动记事本应用程序,然后使用 My.Computer.Keyboard.SendKeys 方法发送击键来打印句子。

示例

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.

可靠编程

如果找不到具有请求的进程标识符的应用程序,则会引发 ArgumentException 异常。

.NET Framework 安全性

Shell 函数的调用需要完全信任(SecurityException 类)。

另请参阅