使用GetAsyncKeyState未在游戏中注册全局热键 - vb.net

时间:2016-10-17 00:47:00

标签: vb.net while-loop global repeat pressed

我已经制作了一个工具,如果用户决定使用切换键等连续点击该用户,它在普通的窗口中工作正常,例如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

1 个答案:

答案 0 :(得分:0)

引用the MSDN documentationGetAsyncKeyState()确定:

  

在调用函数时键是向上还是向下,以及在上次调用GetAsyncKeyState之后是否按下了键。

因此,当您通过If GetAsyncKeyState(Keys.LButton) Then检查函数时,它将至少返回非零三次,因此执行的代码超出您的需要(这是您添加Thread.Sleep(200)时的体验)

要检查密钥是否按下,您必须检查是否设置了the most significant bitShort为十六进制0x8000和十进制32768。< / p>

检查位标志是通过选中(<number> And <bit>) = <bit>来完成的,其中Andbitwise And operator

这会导致您的代码如下所示:

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)。