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.
Related
I am currently having this code which will press Z every 1.5 seconds after activated by pressing B
toggle := 0
return
b::
toggle := !toggle
if (toggle = 1)
SetTimer, Pressz, 1500
else
SetTimer, Pressz, Off
return
Pressz:
SendInput, z
v::SetTimer, Pressz, 1500
But then I am not sure how to change the SetTimer into random time between 0 to 1500
Please help thanks.
Utilizing the 'Random' function that 0x464e mentioned, and single fire SetTimer routines, I created this
toggle := 0
return
b::
toggle := !toggle
;MsgBox %toggle%
if (toggle){
gosub Routine
}
return
Routine:
if(toggle){
Random, var, -1500, 0
gosub Pressz
SetTimer, Routine, %var%
}
return
Pressz:
SendInput, z
return
v::
toggle=1
gosub Routine
return
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?
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.
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
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
So I am making my script
toggl:=false
while(1){
if(toggl){
Send,E
}
Sleep, 150
}
!r::
toggl:=!toggl
return
!y::ExitApp,101
Problem is that while the loop is running, I can not cancel it because it blocks !y, so I had to restart computer. So any help with this would be nice.
Set #MaxThreads 2 to allow each hotkey to run twice, OR:
Use SetTimer to allow the hotkey to end and continue your loop in a "Pseudo-Thread".
toggle := 0
F12::
toggle := !toggle
if (toggle){
SetTimer, DoLoop, -100
}
return
DoLoop:
Loop {
if (!toggle){
break
}
; [Do your stuff here]
}
return
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
I am trying to detect Ctrl+V. Then, make Xbutton1act as Enter for a few seconds, but I can't get it working.
Transform, CtrlV, Chr, 3
Input, OutputVar, L1 M
XButton1::
if OutputVar = CtrlV
{
SetTimer, SendEnter, 0
Sleep, 2000
SetTimer, SendEnter, Off
}
else
{
Send ^t
}
Return
SendEnter:
Send {Enter}
Return
~^v::lastPaste := A_TickCount ;stores counter when ctrl+v is pressed
Xbutton1::
If A_TickCount - lastPaste < 2000 ;checks if 2 seconds gone after ctrl+v was clicked
{
Send, {Enter} ;sends enter
Return
}
else
{
Send, ^t ;sends ctrl+t
Return
}