Autohotkey loop not working - autohotkey

My loop in my autohotkey script is only running through once. Can anyone tell me why? Thanks
Loop, 8
{
WinActivate, NDTr
ControlClick, Button3 ;Select Batch, enter info, start collecting data
WinWait, Batch Readings
ControlClick, Edit1
Send {BS}+{BS}+{BS}+{BS}+{BS}+{BS}
Send 1
ControlClick, Edit2
Send {BS}+{BS}+{BS}+{BS}+{BS}+{BS}
Send 15
if A_Index = 4
{
Sleep, 20000
}
else if A_Index = 7
{
Sleep, 20000
}
else if A_Index = 1
{
Sleep, 3000
}
else
{
Sleep, 15000
}
ControlClick, Button1
Sleep, 15000
}

WinWait looks like a likely culprit like anthv123 said. Double check your window's title and make sure it fits the TitleMatchMode that you're expecting.
Common debugging practices include adding different ToolTips in places along the problem code. For example tooltips above and below the WinWait line with texts "before" and "after" would tell you if it's pausing indefinitely at that part (if it never says "after").
Sleeping for 3-20 seconds isn't going to help your patience either.

Try using this to diagnose the issue. If "Batch Readings" takes longer than 5 seconds, you get an error letting you know and the loop continues
WinWait, Batch Readings,,5
if (errorLevel = 1)
Msgbox % "Batch Readings timed out"

Related

It maybe seem unnecessary, but I want to reopen a spezific program after it is closed by ahk

This is how far i got:
#IfWinActive ahk_exe zotero.exe
f7::
WinGetTitle, Title, A
WinClose, A
Sleep 1000
IfWinNotExist(zotero)
{
Run, zotero.exe , , Min
}
Return
I am unsure about the if statement in an if statement. How can I make it work?
I would suggest to use:
Loop {
Sleep, 1000 ; just 1 second delay between any check
process, Exist, "Zotero.exe"
if ErrorLevel
Run, zotero.exe , , Min
}

Multiple loops with timers

So I'm new to AutoHotkey and I'm having some issues with the Multiloop timer thing, it works fine as first but on the second loop forward the times don't match up with what I want.
So basically I want the loops to run for 5min the loopTwo should be the first one out after 7s and then 2 seconds later I want the loopOne to be called in loopOne I have a 1.2s delay between presses, the first time it works correctly but then the times starts to shift and everything joins in a mess
F1::
If (loopOne = True)
{
SetTimer loopTwo, Off
TwoSwitch := False
SetTimer loopOne, Off
OneSwitch := False
} else {
TheTwoTime := 0
SetTimer loopTwo, 7000 ;run every 7s
TwoSwitch := True
TheOneTime := 0
SetTimer loopOne, 9000 ;run every 9s
OneSwitch := True
}
return
loopOne:
Send, 1
Sleep, 1200
Send, 1
TheOneTime ++
If TheOneTime >= 300 ;run for 5 minutes
{
SetTimer loopOne, Off
OneSwitch := False
}
return
loopTwo:
Send, 2
Sleep, 2000
TheTwoTime ++
If TheOneTime >= 300 ;run for 5 minutes
{
SetTimer loopTwo, Off
TwoSwitch := False
}
return
I think this is what you were trying to do. I don't see any need for two timers, assuming I understood your thing correctly.
Also ditched the legacy labels and switched over to SendInput due it to being the preferred faster and more reliable send mode.
Should be a pretty straight forward script apart from toggling with toggle:=!toggle. If you can't understand it, you can see an old answer of mine that has a bit about it here.
Also note the usage of a negative period in a timer, it's a very useful thing.
F1::
if (toggle:=!toggle)
{
SetTimer, MyCoolLoop, 7000 ;7sec period
SetTimer, StopLooping, -300000 ;negative period, run ONCE after 5mins
}
else
SetTimer, MyCoolLoop, Off
return
MyCoolLoop()
{
;number 2 gets sent (every 7secs)
;2secs after this, number 1 gets sent
;1.2secs after this, number 1 gets sent again
;3.8secs after this, we start from the beginning
SendInput, 2
Sleep, 2000
SendInput, 1
Sleep, 1200
SendInput, 1
}
StopLooping()
{
SetTimer, MyCoolLoop, Off
}

Auto Hot Key - can't interrupt a loop

Here is an example auto hot key script:
^j::
WinActivate, MyWindow
WinWaitActive, MyWindow
Loop
{
If GetKeyState("Shift", "P")
Break
Click, 44, 55
Sleep, 1000
Click, 144, 155
Sleep, 1000
}
return
Everything works fine but I can't interrupt the loop by pressig "Shift". What is wrong ?
You have to hold the Shift key for more than 2 seconds pressed, because of the sleep times.
Or try something like this:
^j::
Loop
{
If !WinActive("MyWindow")
{
WinActivate, MyWindow
WinWaitActive, MyWindow
}
Click, 44, 55
Sleep_1000()
Click, 144, 155
Sleep_1000()
}
return
Sleep_1000(){
Loop 10
{
Sleep, 100
If GetKeyState("Shift", "P")
exit ; terminate the hotkey's thread
}
}
Using a loop inside a hotkey definition is bad practice.
AHK doesn't provide true multithreading, so long running loops are generally a really bad idea.
Using a timer fixes this, and usage of a timer is anyway always what you want for something like this.
And it'll be much more simple as well.
So, with Ctrl+j we activate the desired window and create the timer and tell it to run our function TimerCallback (which we will shortly create) every 2secs:
^j::
WinActivate, MyWindow
WinWaitActive, MyWindow ;shouldn't be needed, but if you find it helpful, fair enough
TimerCallback() ;run the function once, since the timer is going to
;run it for the first time only after 2secs
SetTimer, TimerCallback, 2000
return
And then we make shift be a hotkey for turning off the timer. And we for sure want to use the ~ modifier to not consume the key when the hotkey is fired:
~Shift::SetTimer, koira, Off
And now lets also define our function TimerCallback:
TimerCallback()
{
Click, 44, 55
Sleep, 1000
Click, 144, 155
}
So here's again the script in full if something was somehow left unclear:
^j::
WinActivate, MyWindow
WinWaitActive, MyWindow ;shouldn't be needed, but if you find it helpful, fair enough
TimerCallback() ;run the function once, since the timer is going to
;run it for the first time only after 2secs
SetTimer, TimerCallback, 2000
return
~Shift::SetTimer, TimerCallback, Off
TimerCallback()
{
Click, 44, 55
Sleep, 1000
Click, 144, 155
}

while loop not executing correctly when toggled on/off

Goals:
Run ahk script so that a window stays active. When the user clicks off the window it immediately becomes active again.
This is so an overlay (considered its own window) can be used in a game and that if the overlay is clicked on by accident the game window will become the active window again.
I would also like this to be able to be turned on and off during game play so that the user can alt+tab if necessary.
Problems:
I'm testing my code implementation, so far i have it set up to make a blank notepad file become the active window and stay active.
The problem is the toggle (ctrl+alt+J). I can toggle the code it off just fine but when i toggle it on the window doesn't become active again.
Code:
stop := 0
; 0 = off, 1 = on
while (stop = 0)
{
IfWinNotActive, Untitled - Notepad
{
WinActivate, Untitled - Notepad
}
}
return
^!j::
stop := !stop
if (stop = 0){
MsgBox, stop is off.
}
else{
MsgBox, stop is on.
}
return
The reason it doesn't work after you toggle it off is that While only runs until is evaluates to false. Even if what it would evaluate later becomes true again, it won't restart.
Here's what you can do to make your current code work:
stop := 0
; 0 = off, 1 = on
labelWinCheck: ; label for GoSub to restart while-loop
while (stop = 0)
{
IfWinNotActive, Untitled - Notepad
{
WinActivate, Untitled - Notepad
}
Sleep , 250 ; added sleep (250 ms) so CPU isn't running full blast
}
return
^!j::
stop := !stop
if (stop = 0){
MsgBox, stop is off.
} else {
MsgBox, stop is on.
}
GoSub , labelWinCheck ; restarts while-loop
return
There are a couple of different ways that I would look at to achieve your goal.
Easy: use SetTimer instead of While.
stop := 0
SetTimer , labelWinCheck , 250 ; Repeats every 250 ms
labelWinCheck:
If !WinActive( "Untitled - Notepad" )
WinActivate , Untitled - Notepad
Return
^!j::
SetTimer , labelWinCheck , % ( stop := !stop ) ? "off" : "on"
MsgBox , % "stop is " . ( stop ? "on" : "off" )
Return
Advanced: us OnMessage() to monitor WinActivate events. I don't have a working example of this as that would take a bit of research for me, but here is a link for a solution I made to monitor keyboard events, Log multiple Keys with AutoHotkey. The links at the bottom may especially prove useful.

Temporarily Pause Autofire loop on holding button

I wrote a script that sends autofire left clicks and can be triggered on and off. The script works. However, the problem is that holding the right mouse button does not work properly anymore because the left click keeps getting sent. So I want to change the script that it gets temporarily paused while I hold down the right mouse button.
How would I go about doing this? Here is my current code:
#MaxThreadsPerHotkey 3
#z::
#MaxThreadsPerHotkey 1
if keep_winz_running = y
{
keep_winz_running = n
return
}
; Otherwise:
keep_winz_running = y
Loop
{
GetKeyState, rbut, Rbutton
If rbut, = U
{
Loop,
{
MouseClick, left
Sleep, 50 ;This means the script will wait 1.5 secs
if keep_winz_running = n ; The user signaled the loop to stop.
break ; break out of the loop
}
Timers are the best!
sendToggle := false
#z::
if(!sendToggle) {
sendToggle := true
SetTimer, SendClick, 100
} else {
sendToggle := false
SetTimer, SendClick, Off
}
return
#If sendToggle
RButton::
SetTimer, SendClick, Off
KeyWait, RButton
SetTimer, SendClick, 100
return
SendClick:
Click
return
I find the send interval of 50 ms awfully fast, especially since you won't be able to actually reach 50 ms without reducing SetBatchLines and SetKeyDelay. If it really needs to be that fast, consider changing them.