Vb.net InputSimulator在调试时工作,但在构建程序时不工作

Vb.net InputSimulator在调试时工作,但在构建程序时不工作,vb.net,keyboard,keystrokes,Vb.net,Keyboard,Keystrokes,在我的项目中,我使用inputsimulator,当VisualStudio以管理员身份运行时效果很好,但当我将其构建到.exe中时,即使我以管理员身份运行它也不起作用。这是我的密码 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click AutoSaveTimer.Enabled = True Try System.Threading.Thread.Sl

在我的项目中,我使用inputsimulator,当VisualStudio以管理员身份运行时效果很好,但当我将其构建到.exe中时,即使我以管理员身份运行它也不起作用。这是我的密码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    AutoSaveTimer.Enabled = True

    Try
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.OemSemicolon)
        System.Threading.Thread.Sleep(2000)
        GameConnection.SendKeyTo(Keys.K)
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.Enter)

    Catch AutoSaveExeption As GameException
        If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then
            ' Send message error - connection to Game lost.
            ' 
            MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            SimConnectionBar.BackColor = Color.Red
        End If
    End Try
End Sub

它确实会将焦点发送到我指定的窗口,但不会发送击键

请在发送任何输入之前尝试使用SetForeGroundIndow,以确保您的游戏确实具有焦点。在发送输入之前,应在您的方法中调用SetForeGroundIndow

    <DllImport("user32.dll")> _
Public Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click `
AutoSaveTimer.Enabled = True
Try
    'Find the handle to the game. This can do it by searching for the process.
        Dim p As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("notepad")
        'search for process notepad
        If p.Length > 0 Then
            'check if window was found
            'bring notepad to foreground
            SetForegroundWindow(p(0).MainWindowHandle)
        End If

        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.OemSemicolon)
        System.Threading.Thread.Sleep(2000)
        GameConnection.SendKeyTo(Keys.K)
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.Enter)

    Catch AutoSaveExeption As GameException
        If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then
            ' Send message error - connection to Game lost.
            ' 
            MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            SimConnectionBar.BackColor = Color.Red
        End If
    End Try
end sub
_
公共共享函数SetForegroundWindow(hWnd作为IntPtr)作为布尔值
端函数
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击`
AutoSaveTimer.Enabled=True
尝试
“找到游戏的手柄。这可以通过搜索流程来实现。
Dim p作为System.Diagnostics.Process()=System.Diagnostics.Process.GetProcessByName(“记事本”)
'搜索进程记事本
如果p.长度>0,则
'检查是否找到窗口
'将记事本带到前台
SetForegroundWindow(p(0).MainWindowHandle)
如果结束
系统线程线程睡眠(50)
GameConnection.SendKeyTo(key.OemSemicolon)
系统线程线程睡眠(2000)
GameConnection.SendKeyTo(Keys.K)
系统线程线程睡眠(50)
GameConnection.SendKeyTo(Keys.Enter)
将AutoSaveException捕获为GameException
如果AutoSaveException.GameErrorCode=GameError.GAME\u ERR\u SENDMSG,则
'发送消息错误-与游戏的连接丢失。
' 
MessageBox.Show(“无法建立连接…无法自动保存”,AppTitle,MessageBoxButtons.OK,MessageBoxIcon.叹号)
SimConnectionBar.BackColor=Color.Red
如果结束
结束尝试
端接头

在发送任何输入之前,请尝试使用SetForeGroundIndow,以确保您的游戏确实具有焦点。在发送输入之前,应在您的方法中调用SetForeGroundIndow

    <DllImport("user32.dll")> _
Public Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click `
AutoSaveTimer.Enabled = True
Try
    'Find the handle to the game. This can do it by searching for the process.
        Dim p As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("notepad")
        'search for process notepad
        If p.Length > 0 Then
            'check if window was found
            'bring notepad to foreground
            SetForegroundWindow(p(0).MainWindowHandle)
        End If

        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.OemSemicolon)
        System.Threading.Thread.Sleep(2000)
        GameConnection.SendKeyTo(Keys.K)
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.Enter)

    Catch AutoSaveExeption As GameException
        If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then
            ' Send message error - connection to Game lost.
            ' 
            MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            SimConnectionBar.BackColor = Color.Red
        End If
    End Try
end sub
_
公共共享函数SetForegroundWindow(hWnd作为IntPtr)作为布尔值
端函数
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击`
AutoSaveTimer.Enabled=True
尝试
“找到游戏的手柄。这可以通过搜索流程来实现。
Dim p作为System.Diagnostics.Process()=System.Diagnostics.Process.GetProcessByName(“记事本”)
'搜索进程记事本
如果p.长度>0,则
'检查是否找到窗口
'将记事本带到前台
SetForegroundWindow(p(0).MainWindowHandle)
如果结束
系统线程线程睡眠(50)
GameConnection.SendKeyTo(key.OemSemicolon)
系统线程线程睡眠(2000)
GameConnection.SendKeyTo(Keys.K)
系统线程线程睡眠(50)
GameConnection.SendKeyTo(Keys.Enter)
将AutoSaveException捕获为GameException
如果AutoSaveException.GameErrorCode=GameError.GAME\u ERR\u SENDMSG,则
'发送消息错误-与游戏的连接丢失。
' 
MessageBox.Show(“无法建立连接…无法自动保存”,AppTitle,MessageBoxButtons.OK,MessageBoxIcon.叹号)
SimConnectionBar.BackColor=Color.Red
如果结束
结束尝试
端接头

通过将每行缩进四个空格来格式化代码。使用反勾号`仅适用于
内联代码
,例如在提到
按钮1
列表框
等时——有关更多信息,请参阅。这应该是可行的,但另一种选择是挂接到游戏的消息循环中,或只是将消息发布到游戏的消息循环中。有了这段代码,我看不出为什么它可以在调试中工作,但不能在发布中工作。您正在调用winAPI“SetForegroundWindow”吗?你怎么知道游戏有焦点?您应该在发送输入之前调用API。好的,这是一个按钮单击事件,意味着您的应用程序在运行此代码时具有焦点。您需要在此方法中调用SetForegoundWindow。你能控制游戏吗?或者在开始时创建一个延迟,让你有时间将窗口切换回游戏。不要使用按钮单击,当单击按钮时,你的应用程序会获得焦点,而要尝试使用热键。当你的应用程序收到一条消息,表示某个热键被按下时,你可以执行此代码。这意味着你可以在游戏中按一个热键,告诉你的应用程序做些什么。你的应用程序永远不需要关注这个问题。如果您想尝试热键,请告诉我,我可以给您发送一个示例。将代码的格式设置为每行缩进四个空格。使用反勾号`仅适用于
内联代码
,例如在提到
按钮1
列表框
等时——有关更多信息,请参阅。这应该是可行的,但另一种选择是挂接到游戏的消息循环中,或只是将消息发布到游戏的消息循环中。有了这段代码,我看不出为什么它可以在调试中工作,但不能在发布中工作。您正在调用winAPI“SetForegroundWindow”吗?你怎么知道游戏有焦点?您应该在发送输入之前调用API。好的,这是一个按钮单击事件,意味着您的应用程序在运行此代码时具有焦点。您需要在此方法中调用SetForegoundWindow。你能控制游戏吗?或者在开始时创建一个延迟,让你有时间将窗口切换回游戏。不要使用按钮单击,当单击按钮时,你的应用程序会获得焦点,而要尝试使用热键。当你的应用程序收到一条消息,表示某个热键被按下时,你可以执行此代码。这意味着你可以在游戏中按一个热键,告诉你的应用程序做些什么。你的应用程序永远不需要关注这个问题。如果您想尝试热键,请告诉我,我可以给您发送一个示例。我得到3个错误:语句不能出现在方法体之外。。。十二月