Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

How to interrupt AHK loop


  • Please log in to reply
9 replies to this topic
4lex
  • Guests
  • Last active:
  • Joined: --
Hi all, I've been using AHK to populate fields in one app with data from another. From time to time the web based app falls over and leaves me without control as the script is still simulating mouseclicks and movements etc. I guess I'm being lazy not building in some error handlers, but is there a way I could interrupt the script using maybe a global hotkey combination for stopping AHK scripts? Or any other suggestions?

Thanks all,
Alex

garath
  • Members
  • 398 posts
  • Last active: Feb 05 2011 10:31 PM
  • Joined: 24 Mar 2005
Pause::Pause  ; Assign the toggle-pause function to the "pause" key...
#p::Pause  ; ... or assign it to Win+p or some other hotkey.
Other related commands:
suspend, break, Exitapp, threads
RTM about them

4lex
  • Guests
  • Last active:
  • Joined: --
Thanks very much, thats exactly what I needed.

Mad
  • Guests
  • Last active:
  • Joined: --
the code is

#space::

Loop, 5
{
MouseClick,left, 338, 460
SetMouseDelay, 10
MouseClick,left, 342, 422
SetMouseDelay, 153
MouseClick, left, 522, 546
SetMouseDelay, 169
MouseClick,left, 559, 288
SetMouseDelay, 10
}

if I put the Pause::Pause inside the loop, the whole thing doesn't work, it pauses the thing when I run it and when I unpause it, it does nothing. If I put it before #space:: then it didn't interruot the loop. I'd like to make an infinite loop, but I would need to be able to end it. Thanks in advance.

sosaited
  • Members
  • 278 posts
  • Last active: Oct 06 2009 06:08 PM
  • Joined: 24 Feb 2005
You can use Break and a variable to totally Terminate a Loop when you press a hotkey (or when something else changes the variable):

stop = 0
Loop
{
  If stop = 1
      Break
  ToolTip, %A_Index%
  Sleep, 500
}

^q::
If stop = 0
  {
   stop = 1
   return
  }
If stop = 1
  {
    stop = 0
    return
  }

Another way:

stop = 0
Loop
If stop = 0
  {
   ToolTip, %A_Index%
   Sleep, 500
  }
return

^q::
If stop = 0
  {
   stop = 1
   return
  }
If stop = 1
  {
   stop = 0
   return
  }
return

P.S: The Loop CONTINUES ( A_Index Variable Increases)in this example if you have "Paused" it with this Method.
My small "thanks" to AHK in shape of these dedicated 3d images
Posted Image

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

Another way:

My initial quick change...

stop = 0
Loop
If stop = 0
{
	ToolTip, %A_Index%
	Sleep, 500
}
return

^q::stop:=!stop
Another version (resumes counting where it left off)...

Loop
{
	If stop
		Break
	If pause
		Loop
		{
			If pause!=1
				Break
			Sleep, 319
		}
	ToolTip, %A_Index%
	Sleep, 119
}

^q::stop=1
Pause::pause:=!pause

Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Mad
  • Guests
  • Last active:
  • Joined: --
this is so surreal. Thank you both, now to the surreal part. I add your wonderful codes to my thing, works perfectly. Then I change only the mouse delay, nothing else and ^q doesn't stop the loop anymore. All I did was add 5 to every delay. I mean I deleted 10 and put 15 in it's place and it stopped working, how is this possible?

sosaited
  • Members
  • 278 posts
  • Last active: Oct 06 2009 06:08 PM
  • Joined: 24 Feb 2005
@JSLover
Excellent code you posted :) ... i never played with bit operations..
@Mad
Can you please post your code.
My small "thanks" to AHK in shape of these dedicated 3d images
Posted Image

Marshall666
  • Members
  • 45 posts
  • Last active: May 16 2013 04:03 PM
  • Joined: 09 Mar 2013

You can use Break and a variable to totally Terminate a Loop when you press a hotkey (or when something else changes the variable):
 

Button3:
stop = 0
Loop
{
  If stop = 1
      Break
  ToolTip, %A_Index%
  Sleep, 500

MouseClick, left,  126,  138
Sleep, 1
MouseClick, left,  539,  399
Sleep, 1
MouseClick, left,  594,  367
Sleep, 1
MouseClick, left,  656,  337
Sleep, 1
MouseClick, left,  712,  360
Sleep, 1
MouseClick, left,  769,  380
Sleep, 1
MouseClick, left,  808,  415
Sleep, 1
MouseClick, left,  710,  434
Sleep, 1
MouseClick, left,  630,  488
Sleep, 1
MouseClick, left,  677,  515
Sleep, 1
MouseClick, left,  735,  484
Sleep, 1
MouseClick, left,  775,  459
Sleep, 1
MouseClick, left,  848,  446
Sleep, 1
MouseClick, left,  898,  467
Sleep, 1
MouseClick, left,  882,  549
Sleep, 1
MouseClick, left,  1032,  622
Sleep, 1
MouseClick, left,  981,  644
Sleep, 1
MouseClick, left,  919,  734
Sleep, 1
MouseClick, left,  973,  749
Sleep, 1
MouseClick, left,  1150,  714
Sleep, 1
MouseClick, left,  1102,  740
Sleep, 1
MouseClick, left,  385,  135
sleep, 3000
}
Return



Button4:
stop:=!stop
return


^^All seems to be ok!

If i press "Button3:" the script starts and if i press "Button4:" the Script ends and after pressing start again the script starts again!
 But if i set  the number of Sleep Time(3000) to 960000 the Script wont start after i click "Button4:"

 

Can somone help pls?



jbeitz107
  • Members
  • 3 posts
  • Last active: Feb 08 2015 11:43 PM
  • Joined: 26 Sep 2013

I am trying to figure out the same thing but, it doesn't seem to be working in my script. This is a simple key presser I am designing for a game I am hosting.

Any suggestions would be appreciated. Thanks

 

 

#SingleInstance Force
Gui, Show, x270 y110 h420 w485, Key Presser
Gui, Add, CheckBox, gKey1, 1
Gui, Add, CheckBox, gKey2, 2
Gui, Add, Button, gExitApp, Exit
Gui, Show
Return
 
 
Key1:
IfWinExist ahk_class Notepad
WinActivate
SetTimer, Loop1, 1000
Return
 
 
Key2:
IfWinExist ahk_class Notepad
WinActivate
SetTimer, Loop2, 1000
Return
 
ExitApp:
ExitApp
Return
 
Loop1:
SendInput, 1
Sleep 100
Return
 
Loop2:
SendInput, 2
Sleep 100 
Return