左键单击GetAsyncKeyState卡住

时间:2017-01-18 00:46:08

标签: vb.net

所以我正在编写一个有CheckBox的项目,如果选中了CheckBox,它会启动Timer并且Timer获得左键单击状态。出于某种原因,当我点击左键时,asynckeystate选择它,然后它开始Timer 1,由于某种原因,当我点击它时,它有时会卡住,即使我已经放弃了左边单击。

有人可以帮忙吗?

If CheckBox2.Checked = True Then
    Me.KeyPreview = True
    Timer3.Enabled = True
Else
    Me.KeyPreview = False
End If

End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
    hotkey = GetAsyncKeyState(Keys.LButton)
    If hotkey = True Then
        Timer1.Start()
    Else
        Timer1.Stop()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我测试了你的代码,它似乎工作正常,唯一可能的原因是计时器的间隔太高。如果您希望仅在按下或释放鼠标按钮时触发事件,我稍微修改了您的函数并添加了一个布尔值来检查鼠标按钮是否已经关闭或释放。

Private Sub Timer3_Tick(sender As Object, e As EventArgs)
    Static LeftMouseButtonDown As Boolean = False
    Dim hotkey = GetAsyncKeyState(Keys.LButton)
    If CBool(hotkey) = True Then
        ' Timer1.Start()
        If Not LeftMouseButtonDown Then
            LeftMouseButtonDown = True
            Console.WriteLine("Left mouse button was pressed.")
        End If
    Else
        ' Timer1.Stop()
        If LeftMouseButtonDown Then
            LeftMouseButtonDown = False
            Console.WriteLine("Left mouse button was released.")
        End If
    End If
End Sub

我希望这有帮助!
〜尼克