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

更新:2007 年 11 月

此示例使用 Shell 函数启动计算器应用程序,然后使用 My.Computer.Keyboard.SendKeys 方法发送击键将两个数字相乘。

示例

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.

此代码示例也可用作 IntelliSense 代码段。在代码段选择器中,此代码示例位于“Windows 窗体应用程序”>“窗体”中。有关更多信息,请参见如何:在代码中插入代码段 (Visual Basic)

可靠编程

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

安全性

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

请参见

参考

My.Computer.Keyboard.SendKeys 方法

Shell 函数

AppActivate 函数