我的关注:有人可以帮我直接通过游戏吗?
这是简单的代码。
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SendKeys.Send(TextBox1.Text) 'Sends the message you typed in the textbox1
SendKeys.Send(" ") 'presses the SPACE key from your keyboard
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = TextBox2.Text
Timer1.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = TextBox2.Text
Timer1.Enabled = False
End Sub
End Class
感谢先生!:D
答案 0 :(得分:0)
我认为问题在于你没有将焦点转移到游戏窗口。因此,您的程序将空间发送到您编写的vb.net程序而不是游戏。
您需要做一些事情来将焦点转移到游戏窗口。查看this question,因为它包含有关使用Win32 API更改哪个窗口具有焦点所需的信息。
修改强>:
根据您的要求,您可以在此处将其用于代码。请注意,编写此代码所需的所有信息都在我提供的链接中。请阅读有人在将来回答您的问题时提供的来源。我留下了大量的评论,以帮助您了解每条线的作用。
Imports System.Runtime.InteropServices 'Needed to import the Win32 API functions
Public Class Form1
'Make sure the programTitle const is set to the _EXACT_ title of the
'program you are trying to set focus to or this won't work.
Private Const programTitle As String = "Title of program"
Private zero As IntPtr = 0 'Required for FindWindowByCaption first parameter
'Import the SetForegroundWindow Function
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
'Import the FindWindowByCaption Function, called as a parameter to SetForegroundWindow
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption( _
ByVal zero As IntPtr, _
ByVal lpWindowName As String) As IntPtr
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Call SetForegroundWindow whenever you want to send keys to the specified window.
SetForegroundWindow(FindWindowByCaption(zero, programTitle))
SendKeys.Send(TextBox1.Text) 'Sends the message you typed in the textbox1
SendKeys.Send(" ") 'presses the SPACE key from your keyboard
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = TextBox2.Text
Timer1.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = TextBox2.Text
Timer1.Enabled = False
End Sub
End Class
请注意:我测试了此代码并验证了它可以设置窗口焦点并发送空格键和任何其他键。如果此代码对您不起作用,那么programTitle
常量中的名称错误。例如,如果我想将焦点设置到此记事本窗口:
如果我将programTitle
设置为"Notepad"
试图让它将密钥发送到记事本窗口,这将不起作用。这是因为&#34;记事本&#34;只是标题的一部分。要使其发挥作用,programTitle
应设置为"Untitled - Notepad"
。