Vega
Vega

Reputation: 2929

Start & stop a script a looped action with a button

The script should to the following:

Press 'C': Start/Stop script

Left Mouse Button: Start/Stop the loop

Inside the loop: While holding down the Left Mouse Button, the left mouse button is repeated till I lift my finger of it.

Alternative the mouse is moved X pixel down after each click and my script is very slow. It goes click .. click .. click instead of ClickClickClick :(.

I changed it to this now, but the Left Mouse Button is always activated even when not holding it down, I can't stop/start the script with C too.

HotKey, ~$*LButton, myLButtonAction ; Activate the hotkey by default
return

~c::    ; configure a Hotkey: c will enable / disable your LButton actions
HotKey, ~$*LButton, toggle  ; ON / OFF
return

myLButtonAction:    ;cnote: this is NOT a hotkey, it's a label
Loop
{
    Click
    Sleep 7,516  ;your loop actions (see question code)
}
return  ; don't forget your returns at the end of a label / hotkey

Upvotes: 0

Views: 2001

Answers (1)

phil294
phil294

Reputation: 10822

Looks like you need to use the Hotkey command.

x := (A_ScreenWidth // 2)
y := (A_ScreenHeight // 2)
HotKey, ~$*LButton, myLButtonAction ; Activate the hotkey by default
setMouseDelay, 0
setKeyDelay, 0
return

~c::    ; configure a Hotkey: c will enable / disable your LButton actions
HotKey, ~$*LButton, toggle  ; ON / OFF
return

myLButtonAction:    ; note: this is NOT a hotkey, it's a label

Loop                 ;loop the script until broken
{ ;loop start
GetKeyState, var, LButton, P ;Get the state of Lbutton
If var = U                            ;has it been released?
    Break       ;its been released so break the loop
;Send {LButton}  ;It hasnt been released so send another Click
Click %x%, %y%
Sleep 100 ;time between presses, after sleep return to the top of the loop
} ;loop end

return  ; don't forget your returns at the end of a label / hotkey

my script is very slow. It goes click .. click .. click instead of ClickClickClick :(

Include setMouseDelay, 0 into your auto-execution section. I already did this in the code example above.

Upvotes: 2

Related Questions