2017-07-02 74 views
-2

所以我把这一切全部放在一个每80毫秒运行一次的定时器中,由于某种原因,当这个函数激活时,它有时候会卡住,并且即使我没有按住左键点击也会继续。我也尝试添加第二个检查(clickdone),但它仍然在做。这里是什么造成的,我认为是延迟,但我需要延迟,所以如果任何人在这里可以通过添加另一个检查或者修复这个问题的东西来帮助我,这将不胜感激!这里是我的代码:不知道是什么原因造成的

Sub MyDelay() 
    Dim randomlul As New Random 
    Dim ezdelay As Integer 
    ezdelay = randomlul.Next(private delay, private delay) 

    Dim iCount As Integer = 1 
    For iCount = 1 To ezdelay 
     iCount = iCount + 1 
    Next 
End Sub 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    If CheckBox2.Checked = True Then 
     hotkey = GetAsyncKeyState(Keys.LButton) 
     If CBool(hotkey) Then 
      If (clickdone = True) Then 
       mouse_event(mouse_downclick, 0, 0, 0, 0) 
       clickdone = False 
       MyDelay() 
       clickdone = True 
       mouse_event(mouse_upclick, 0, 0, 0, 0) 
      End If 
      End If 
     End If 
End Sub 

这是不是被挂该职位的副本,我试过其他的解决方案,但它所以现在搞砸了我的其他功能我必须做出哪些犯规搞砸计数延迟其他功能,但现在的问题是,getasynckey状态不断循环有时

+0

对于延迟的循环是可怕的。看看['Threading.Thread.Sleep'](https://msdn.microsoft.com/en-us/library/d00bd51t(v = vs.110).aspx)而不是 – freefaller

+0

我不确定,但也许是第一个'Sub'应该是'Public Sub'.Maybe – TGamer

+0

尝试调用鼠标点击Me.Buttons – TGamer

回答

0

它卡住的原因是因为GetAsyncKeyState()读取虚拟鼠标和键盘输入流,而不是物理键的状态。因此,代码将继续执行,因为它会注意到输入流中的更多点击。

previous answer of mine所述,您必须将鼠标点击作为窗口消息发送,因为它们不会被GetAsyncKeyState()注意到。

EDIT(2017年7月31日):
我不赞成我MouseInputHelper类和我经常InputHelper类合并它。放心,功能仍然是一样的。

下载InputHelper从我的GitHub库:
https://github.com/Visual-Vincent/InputHelper/releases

您应该能够使用此类似:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    If InputHelper.Keyboard.IsKeyDown(Keys.LButton) Then 
     InputHelper.WindowMessages.SendMouseClick(Windows.Forms.MouseButtons.Left, Cursor.Position, True, True) 

     'Do stuff here... 

     InputHelper.WindowMessages.SendMouseClick(Windows.Forms.MouseButtons.Left, Cursor.Position, False, True) 
    End If 
End Sub 
+0

评论是不适合扩展讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/148803/discussion-on-answer-by-visual-vincent-not-sure-what-is-causing-this)。 –

相关问题