如何:启动应用程序并向其发送击键 (Visual Basic)
此示例使用 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.
可靠编程
,当与请求的应用程序进程标识符无法找到, ArgumentException 将引发异常。
安全性
为 Shell 函数的调用要求完全信任 (SecurityException 类)。