0

我正在创建一个“自动点击器”,用于在按下按钮时反复左击鼠标的视频游戏中。我想让热键设置为全局工作,而无需自动点击器窗口成为焦点。出于某种原因,当前只有用于停止点击的热键起作用(F2)。F1 键应该开始单击,但目前启动它的唯一方法是手动按下窗体上的“开始”按钮。尝试将代码部署到 .NET 框架的 4.0 版时,该代码似乎会引发错误,但将其设置为 3.5 可以解决此问题。我在下面包含了我的整个代码,因为我无法弄清楚为什么 F1 热键无法正常工作,尽管 F2 热键工作完美。谢谢你的时间。这段代码的很大一部分是我通过查看其他人的工作示例得出的,我只是在一定程度上理解了它。我希望我的代码格式正确,因为我是这个网站的新手,我并不完全了解如何正确粘贴它。提前感谢您的帮助,我真的很感激。

Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vkey As Long) As Integer
Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwflags As Long, ByVal dx As Long, ByVal cbuttons As Long, ByVal dy As Long, ByVal dwExtraInfo As Long)
Dim hotkey1 As Boolean
Dim hotkey2 As Boolean
Private Const mouseclickup = 4
Private Const mouseclickdown = 2
Dim Test As Integer
Dim Interval As Integer
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Click.Start()
End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    Click.Stop()
End Sub

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    Test = Test + 1
    Counter.Text = Test
End Sub

Private Sub Click_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click.Tick
    Interval = CInt(timeTextbox.Text)
    Click.Interval = Interval
    mouse_event(mouseclickdown, 0, 0, 0, 0)
    mouse_event(mouseclickup, 0, 0, 0, 0)
    hotkey1 = GetAsyncKeyState(Keys.F1)
    If hotkey1 = True Then
        btnStart.PerformClick()
    End If
    hotkey2 = GetAsyncKeyState(Keys.F2)
    If hotkey2 = True Then
        btnStop.PerformClick()
    End If
End Sub

Private Sub timeTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeTextbox.TextChanged
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    KeyPreview = True
End Sub

结束类

4

1 回答 1

0

您的声明适用于 VB6。VB.Net 需要更改数据类型,如下所示。

此外,您需要两个独立的计时器。一种用于检测按键,它必须以高速率运行。另一个用于进行实际点击,其速率由您的 TextBox 值决定。

Public Class Form1

    Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
    Private Const MOUSEEVENTF_LEFTUP As Integer = &H4

    Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As System.Windows.Forms.Keys) As Short

    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
        ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
        ByVal dwExtraInfo As Integer)

    Private WithEvents KeyTimer As New Timer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        KeyTimer.Interval = 50
        KeyTimer.Start()

        Click.Enabled = False
    End Sub

    Private Sub KeyTimer_Tick(sender As Object, e As System.EventArgs) Handles KeyTimer.Tick
        If IsKeyDown(Keys.F1) Then
            Click.Start()
        ElseIf IsKeyDown(Keys.F2) Then
            Click.Stop()
        End If
    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Static Test As Integer
        Test = Test + 1
        Counter.Text = Test
    End Sub

    Private Sub timeTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeTextbox.TextChanged
        Dim Interval As Integer
        If Integer.TryParse(timeTextbox.Text, Interval) Then
            Click.Interval = Interval
        End If
    End Sub

    Private Sub Click_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click.Tick
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    End Sub

    Private Function IsKeyDown(ByVal key As System.Windows.Forms.Keys) As Boolean
        Return ((GetAsyncKeyState(key) And &H8000) = &H8000)
    End Function

End Class
于 2013-11-11T08:10:08.633 回答