Autohotkey - Pausing Script and when unpaused show 'RUNNING' on tooltip - autohotkey

I would like my code to show 'paused' upon script being paused and show 'running' for only a short interval after the script is unpaused.
However, it did not work as expected.
My code below (commented the problematic issue):
NumpadEnter:: ; script is paused on said key and reactivated on said key
Suspend
ToolTip, PAUSED, 200, 250
Pause ,, 1
; ; ToolTip, RUNNING, 200, 250 ; Code segment when un-commented does not work as it should
; ; Sleep 500 ;
SetTimer, RemoveToolTip, 1
return
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return
Thank you so much!!

Try this:
NumpadEnter::
Suspend
Pause ,,1
if A_IsPaused {
ToolTip, PAUSED, 200, 250
} else {
ToolTip, RUNNING, 200, 250
SetTimer RemoveToolTip, 1000
}
return
RemoveToolTip:
ToolTip
SetTimer, RemoveToolTip, Off
return

Related

A continuous while loop with actions toggled on/off by a hotkey, separate from the main body. How do I approach this?

I need to have
while (true) {
if (toggle) { ;(toggle is simply set on/off with a hotkey)
...stuff...
Sleep, 250
}
}
running independently separate from the main "thread", which in this case is stuck in a RunWait
prev_volume := 20
SoundGet, prev_volume
if (prev_volume <= 32) {
SoundSet, 26
}
RunWait, some game
SoundSet prev_volume
ExitApp
Not sure how to approach this
Any help is appreciated, thanks in advance.
So to use the command SetTimer in this instance:
SetTimer, ToggleThingy, On ; defaults to 250 ms
toggle := false
prev_volume := 20
SoundGet, prev_volume
if (prev_volume <= 32) {
SoundSet, 26
}
RunWait, some game
SoundSet prev_volume
ExitApp
{hotkey here}::
toggle := !toggle
Return
ToggleThingy:
if (toggle) { ;(toggle is simply set on/off with a hotkey)
...stuff...
; Sleep, 250 ; not needed because timer already runs every 250 ms
}
}
Return ; needed to end the subroutine.

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
}

How do I use WinWait on multiple labels without blocking the others?

I have SetTimers pointing to multiple labels but they stop working when I have a WinWait in one of the labels.
SetTimer, MyLoop, -5
SetTimer, MyLabel, -1000
Return
MyLoop:
SoundBeep, 700
SetTimer, MyLoop, -5
Return
MyLabel:
SoundBeep, 300
WinWait, Notepad
SetTimer, MyLabel, -1000
Return
MyLoop stops working as soon as MyLabel reaches the WinWait section. How do I make MyLoop continue on while MyLabel is waiting for the WinWait on its own?
Please refer to the docs: Threads, SetTimer, WinWait
i := 0
settimer, afterTenMsg, 1000 ; Start our timer!
GoSub, WaitForNotepad
ExitApp
WaitForNotepad:
WinWait, Untitled - Notepad ; This Thread is interupted by the Timer.
MsgBox % "Yeah NotePad Opened!" ; Close this will return to line 3
return
afterTenMsg:
i++
if (i == 10) {
settimer, afterTenMsg, off
run, notepad.exe
settimer, openMsgBox, 1000 ; Interupt MsgBox thread after 1 sec
}
return ; Returns to WinWait
openMsgBox:
settimer, openMsgBox, off
MsgBox % "We interupted a MsgBox!" ; close this will return to line 8
return
Hope this helps.

Can someone help me an AutoHotKey script?

I've read the help and guide for it but I just can't figure it out. All I want to do is create a hotkey that when pressed will move my mouse until the hotkey is pressed again. Can anyone tell me how to do this? It should be really simple but apparently I'm missing something.
This is pretty much the most annoying HotKey ever, but here you go (hotkey is Ctrl+Alt+C);
#MaxThreadsPerHotkey 3
^!c::
#MaxThreadsPerHotkey 1
if DoMouseMove
{
DoMouseMove := false
return
}
DoMouseMove := true
Loop
{
Sleep 100
Random, randX, 1, 1028
Random, randY, 1, 800
MouseMove, randX, randY, 25
Sleep, 100
if not DoMouseMove
break
}
DoMouseMove := false
return
I will throw in my solution.
pause::
If (Toggle := !Toggle) ; Toggle the value True/False
{
ToolTip Mouse Mover,A_ScreenWidth/2,0 ; Show that Mouse Mover is active
SetTimer MoveMouse, 1000 ; 1000 ms = 1 sec. Every minute (60000 ms) is probably enough.
}
Else
{
Tooltip ; Turn Mouse Mover alert window off
SetTimer MoveMouse, Off ; Turn the timer off
}
return
MoveMouse:
MouseMove, 1, 0, 1, R ;Move the mouse one pixel to the right
Sleep, 50 ; Wait 50 ms. Not realy required, but makes the move visible
MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
return

Autohotkey loop not working

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"