【问题标题】:VB.net send keypress to gameVB.net 发送按键到游戏
【发布时间】:2017-07-04 18:09:42
【问题描述】:

我看到了关于这个问题的其他问题,但没有一个为我解决了这个问题,所以我开始了。我正在尝试向游戏发送按键以保存进度(它是 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(";") 但没有运气,游戏在“GameName”下运行,但是按键需要在游戏下的窗口中发送: Blacked out is the game and under the first window is where the keypress needs to be sent

提前感谢您的帮助

【问题讨论】:

  • 您需要将击键作为硬件扫描码发送,以便某些游戏允许它们。这样做有点“欺骗”游戏,让他们相信击键来自实际的键盘而不是程序。我在上面链接中的回答中对此进行了描述。

标签: vb.net process keyboard keypress


【解决方案1】:

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

https://inputsimulator.codeplex.com/

发送密钥就这么简单。活动窗口将看到就像用户实际使用了这些键一样。

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

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

【讨论】:

  • 但是 codeplex 页面说这是针对 C# 的,这不会导致我在 VB.NET 中进行编码吗?
  • 您可以将 c# 编译成可以在项目中引用的 DLL。该项目应该已经设置为编译为 DLL。
  • 对不起,如果我有点笨,但如果 .dll 是用 C# 编写的,我怎么能在 VB.net 程序中使用它,我尝试添加 dll 作为参考,但它不起作用。
  • 什么意思?如果您有一个已编译的 c# 库,它将作为 VB.net 程序中的参考。你有错误吗?您可以发布您尝试过的内容吗?
  • 我已将其设置为发送按键,但它仅将焦点设置在游戏窗口上,并且不会继续执行按下该键时会发生的情况
【解决方案2】:

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

    <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

【讨论】:

  • 这在我试图将按键发送到的游戏中不起作用
  • 您确定您拥有要发送密钥的确切窗口的句柄吗?尝试使用一些 WinAPI 来获取确切窗口的句柄,而不是使用 GetProcessByName。
猜你喜欢
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 2019-03-21
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多