VB.net向游戏发送按键

时间:2017-07-04 18:09:43

标签: vb.net process keyboard keypress

我已经看到了关于这个问题的其他问题,但他们都没有为我解决这个问题,所以我去了。我试图向游戏发送按键以保存进度(它是64位游戏)。我到目前为止编码的是:

Dim p() As Process
Dim GameID As Integer = 0
 Dim p As Process() = Process.GetProcessesByName("Gamename")

    If p.Length > 0 Then
        For i As Integer = 0 To p.Length - 1
            GameID = (p(i).Id)
        Next
    End If
AutoSaveTimer.Enabled = True
    Dim Test As Integer = 0
    GetAsyncKeyState(Test)
    AppActivate("GameName")
    My.Computer.Keyboard.SendKeys(";", True)

现在我已经尝试过Sendkeys.Send(&#34 ;;")但没有运气,游戏运行在" GameName"但是按键需要在游戏的窗口中发送: Blacked out is the game and under the first window is where the keypress needs to be sent

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我在我的应用中使用InputSimulator,效果很好。

https://inputsimulator.codeplex.com/

发送密钥可能很容易。活动窗口将看到用户实际使用这些密钥。

var sim = new InputSimulator();
sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
sim = null;

以下是可用于键盘的方法的屏幕截图。

enter image description here

答案 1 :(得分:0)

您可以模拟这样的程序的键盘输入:

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

Public Sub Send()
        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

        SendKeys.SendWait("a")
        'send key "a" to notepad
    End Sub