我制作了一个工具,如果用户决定使用切换键等,它会持续点击,这在普通窗口(如google chrome)中工作得很好,但当涉及到游戏时,它并不总是正确工作。(游戏邦注:有些游戏是这样,有些游戏则不然)
代码被设计为在按住LButton时快速点击,然后在它被释放时停止作为自动点击器(用户可以控制速度),这再次起作用,但当在游戏中它点击的速度比它应该/任何其他窗口/应用程序慢得多。
我已经想出了添加一个延迟使用
Thread.Sleep(200)
修复了游戏中自动点击器的速度,但它扰乱了键绑定,导致自动点击器总是点击,即使没有按住/按下LButton。
还有什么我可以用作延迟,或者我可以对代码做什么,使其正确工作吗?
在过去的几天里,我一直在尝试许多不同的变化,并在网上搜索,试图让它工作,但没有成功。
这是我的项目中所有与自动点击有关的代码,我添加了一些注释来尝试解释哪一部分正在做什么/定时器被设置为速度
Imports System.Threading
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short
Private Declare Sub mouse_event Lib "user32" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal cbuttons As Integer, ByVal dy As Integer, ByVal dwExtraInfo As Integer)
Private Const mouseclickup = 4
Private Const mouseclickdown = 2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Timer1 isnt doing the clicking, Timer1 is just listening for LButton
'clicks which is why I have it always on aswell as a low interval.
Timer1.Start()
Timer1.Interval = 1
'LButton is the timer that will do the clicking.
LButton.Interval = 100
End Sub
Private Sub LButton_Tick(sender As Object, e As EventArgs) Handles LButton.Tick
If GetAsyncKeyState(Keys.LButton) Then
mouse_event(mouseclickup, 0, 0, 0, 0)
'Without Thread.Sleep(200) the code works as it's suppose to, clicks
'when LButton is held, stops clicking when LButton is let go,
'although without Thread.Sleep(200) it will not work in all games,
'but with it, it will continuously click even when LButton isn't held.
Thread.Sleep(200)
mouse_event(mouseclickdown, 0, 0, 0, 0)
Else
LButton.Stop()
End If
End Sub
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
'This is what will listen for the left clicks and also stop the left
'LButton timer if LButton is not held
If GetAsyncKeyState(Keys.LButton) Then
LButton.Start()
Else
LButton.Stop()
End If
End Sub
End Class
引用 MSDN文档, GetAsyncKeyState()
决定:
调用函数时键是上键还是下键,以及该键是否在之前调用GetAsyncKeyState后被按下。
因此,当您通过If GetAsyncKeyState(Keys.LButton) Then
检查函数时,它将返回非零至少三次,因此执行的代码比您想要的多(这就是您在添加Thread.Sleep(200)
时所经历的)。
要检查键是否按下,您必须检查是否设置了最高有效位,对于Short
是十六进制的0x8000
和十进制的32768
。
检查位标志是通过检查(<number> And <bit>) = <bit>
来完成的,其中And
是位And
运算符。
这将导致您的代码看起来像这样:
Const KeyDownBit As Integer = &H8000
Private Sub LButton_Tick(sender As Object, e As EventArgs) Handles LButton.Tick
If (GetAsyncKeyState(Keys.LButton) And KeyDownBit) = KeyDownBit Then
mouse_event(mouseclickup, 0, 0, 0, 0)
Thread.Sleep(200)
mouse_event(mouseclickdown, 0, 0, 0, 0)
Else
LButton.Stop()
End If
End Sub
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
If (GetAsyncKeyState(Keys.LButton) And KeyDownBit) = KeyDownBit Then
LButton.Start()
Else
LButton.Stop()
End If
End Sub
我不确定是否你的第二个定时器(Timer1
)实际上是需要在这种情况下