I'm trying to automatically repeat LButton while it's held down, then stop when it's released, I've ran into a problem where it will continuously repeat itself even while it's not been held down.
Is there any workarounds for this? I need it to also work on other applications which is why I'm using GetAsyncKeyState
.
This is what I have so far:
Imports System.Threading
Public Class Form1
Const KeyDownBit As Integer = &H8000
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 Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If (GetAsyncKeyState(Keys.LButton) And KeyDownBit) = KeyDownBit Then
mouse_event(mouseclickup, 0, 0, 0, 0)
Thread.Sleep(100)
mouse_event(mouseclickdown, 0, 0, 0, 0)
End If
End Sub
With this code, when I left click, the code will continuously keep clicking automatically even when Lbutton is released, but that's not exactly what i want. I want it so when I hold LButton, it will then continuously click, then when LButton is released, it will stop clicking.
I have tried using a BackgroundWorker
, although the same thing happens.
I have also tried having mouse_event(mouseclickdown, 0, 0, 0, 0)
before mouse_event(mouseclickup, 0, 0, 0, 0)
, but then it just turns a single click into a double click each time it's pressed down, then stops.
See Question&Answers more detail:
os