1

在我的项目中,我使用了 inputsimulator,当 Visual Studio 以管理员身份运行时效果很好,但是当我将它构建成 .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

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

4

1 回答 1

0

在发送任何输入之前尝试使用 SetForegroundWindow 以确保您的游戏确实具有焦点。对 SetForegroundWindow 的调用应该在发送输入之前在您的方法中进行。

    <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
于 2017-07-09T16:51:41.093 回答