Jesse730
Jesse730

Reputation: 13

Using Persistent to continuously run script every ___ ms in AutoHotKey

I am very new so I apologize for this question being basic, but I have been trying tips on the forum but cannot seem to get them to run.

I'm trying to get the following basic script to run continuously on a timer so every 15 seconds it will automatically run the function.

The base function is

*1::
Click 287, 536 
Click 228, 405
return 

I attempted to run it with the delay and continue running and only stop when exiting app (only way I know by making the following changes:

Esc::ExitApp

#Persistent
*1::
Click 287, 536 
Click 228, 405
Sleep 15 ;  seconds
return

What am I missing? I of course would love the solution but if it could be explained so I can write scripts in the future I'd appreciate it. Thanks :)

Upvotes: 0

Views: 3590

Answers (4)

Ian Campos
Ian Campos

Reputation: 178

Adding #Persistent does not do what you want; it just makes it so that the script doesn't close until it gets to an ExitApp. Also, Sleep 15 ; seconds won't do what you want either; what you wrote would tell the script to wait just 15 milliseconds, and everything from the semicolon to the end of the line is ignored because it is considered a comment. So what you need to do is put the code for your hotkey into a loop. Then, multiply that 15 in your sleep statement by 1000. Afterwards, this is what your code should look like:

Esc::ExitApp

#Persistent
*1::
    Loop
        {
         Click 287, 536 
         Click 228, 405
         Sleep 15000
        }
    return

If you need any more help, then you should really read the AutoHotkey documentation.

Upvotes: 0

LogicDaemon
LogicDaemon

Reputation: 570

So, you want the script to start clicking that coordinates each 15 seconds after you've pressed any key combination with "1" involved, right?

Then the code by the @immo will do this for you.

About #Persistent: If a script has no hotkeys or hotstrings, script process will terminate on Exit or return from auto-execute section. This directive prevents this.

But as your script has got hotkey (*1::), it already won't terminate on Exit or return (only current thread will). And in that case, directive does nothing.

Also, though this is only question of style, I offer another script, which does almost the same thing, except it will also pause clicking if you press 1:

toggleTimer=1
*1::
    If toggleTimer
        SetTimer ClickThem, 15000
    else
        SetTimer ClickThem, Off
    toggleTimer:=1-toggleTimer
return

Esc:: ExitApp

ClickThem:
    Click 287, 536 
    Click 228, 405
return

Note, that if you'll remove hotkeys from the script (*1 and Esc), you will need the #Persistent directive, or it will exit before the timer will kick in for first time.

And, for the last, what you're doing wrong in your script:

Currently, you ask ahk to click that coordinates, wait 15 milliseconds (that's why we're writing 15000), and return, which means stop running current thread, the one which processed your 1 keypress.

But you need to repeat clicking, so you have options. First is a loop, like immo offered. Another is timer.

Also, I hope you know, that ; means comment for other readers, and AutoHotkey ignores them, so you've wrote "seconds" for us, not for autohotkey.

Upvotes: 1

L d Bonnie
L d Bonnie

Reputation: 101

Click is preformed in the active window. If you want to click a coordinate in a different window it has to be activated.

Look at ControolClick, ControlSend and the GontrolGet series of commands. They provide better control of windows.

Upvotes: 0

immo
immo

Reputation: 11

*1::
loop {
    Click 287, 536 
    Click 228, 405
    Sleep 15000
}
return

Esc::ExitApp

also see loop command for details

Upvotes: 1

Related Questions