0
votes

I've only just started with AutoHotKey, and I'm looking to make a script that will click once per second 10 times, then hold right mouse button for 3 seconds, before resetting. I intend it to active on alt+c, and break if I press the left mouse button.

The script I came up with it

LButton::
BreakLoop = 1
return
!c::
Loop
{
if (BreakLoop = 1)
break
;

Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
Return
}

However, this is not working. Is there a way to fix this, or have I taken the completely wrong approach for this script?

3

3 Answers

1
votes

You did make a Mistake in the code, On the Bottom You did have the Return Command into the Loop that is not possible. (This Return Command Will be needed for !c:: and it must outsite the loop command)

The Code must be Like:

~LButton::
BreakLoop = 1
return

!c::
Loop
{
if (BreakLoop = 1)
break

Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
}
Return

Tip: if you change Lbutton:: into ~Lbutton:: then The Default LeftButton is also active.

0
votes

I was actually able to find a way to compact it significantly (and break the loop faster) by nesting a loop within a loop

!s::
BreakLoop = 1
return


!c::
BreakLoop = 0

Loop
{

Loop 10
{
if (BreakLoop = 1)
break
;
Click
Sleep, 900
}

Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
}
if (BreakLoop = 1)
Break
;

Return
0
votes

A better method is to use SetTimer, this allows you to break out of the loop at any point in your sequence of actions.

Try:

!c::setTimer, doAction, 1000
!s::SetTimer, doAction, Off 

doAction:
    i += (i <= 14 ? 1 : -13)
    if (i == 14)
        send, {RButton Up}
    else if (i == 11 )
        Send, {RButton Down}
    else if (i <= 10) 
        click   
return