.net随笔-vb.net打开外部程序发送键盘信号(1)

2019-04-13 21:51发布

class="markdown_views prism-atom-one-light"> Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim notepadID As Integer ' 下面可激活一个运行中的程序 'AppActivate("无标题 - 记事本") ' 通过shell函数运行一个新实例 notepadID = Shell("NOTEPAD.EXE", AppWinStyle.NormalFocus) AppActivate(notepadID) My.Computer.Keyboard.SendKeys("您好,世界!", True) End Sub End Class 在这里插入图片描述
C#
public void SendKeys (string keys, bool wait);
Parameters
keys
String
A String that defines the keys to send.
wait
Boolean
Optional. A Boolean that specifies whether or not to wait for keystrokes to get processed before the application continues. True by default.
Exceptions
SecurityException
A partial-trust situation exists in which the user lacks necessary permissions
因为没有托管方法来激活另一个应用程序,所以您可以在当前应用程序中使用该类,手动选择要向其发送密钥的窗口,或者使用Windows API方法(如FindWindow和SetForegroundWindow)强制将焦点移动到其他应用程序上