Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Loop until key is pressed?


  • Please log in to reply
11 replies to this topic
Oriknama
  • Guests
  • Last active:
  • Joined: --
Baisically, I have a script set to loop and click at different locations every few seconds, which looks like this:
#x::
Loop 100,
{
LeftClick,  530,  378
Sleep, 100
Send, Text
Sleep, 100
LeftClick,  472, 415
Sleep, 100
LeftClick,  520,  552
Sleep, 100
LeftClick,  470,  580
Sleep, 100
}

It does indeed need to loop roughly this fast to get the desired effect. However, this poses a problem when I want to stop the macro. Is there any way to make the macro stop looping when I press the macro hotkey again, or is there a universal pause all macros hotkey?

Thanks

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
There are at least two approaches. The first is to make a new hotkey that pauses the entire script. For example, this makes Ctrl-Alt-p do that:

^!p::pause ; toggle the pause state off and on

The other approach is to use a variable to make your #x hotkey multi-purpose, so that pressing it again will turn off the looping activity. Here's how to do it:

; Allow multiple threads so that this hotkey can "turn itself off":
#MaxThreadsPerHotkey 2
#x:: 
#MaxThreadsPerHotkey 1
if keep_x_running = y
{
     keep_x_running = n  ; Signal the other thread to stop.
     return
}
keep_x_running = y
Loop 100, 
{
    if keep_x_running = n  ; Another thread signaled us to stop
        return
    LeftClick,  530,  378 
    Sleep, 100 
    Send, Text 
    Sleep, 100 
    LeftClick,  472, 415 
    Sleep, 100 
    LeftClick,  520,  552 
    Sleep, 100 
    LeftClick,  470,  580 
    Sleep, 100 
}


In the above, you can repeat the following section more often (e.g. after every sleep) if you need the stopping to be more instantaneous:

if keep_x_running = n
return

young707
  • Members
  • 4 posts
  • Last active: May 31 2004 04:33 PM
  • Joined: 29 May 2004
I have 2 loops of clicking the mouse.

How can I cancel and stop any of the looping hotkeys?
Thank you.


^!z::
Loop, 50
{
Sleep, 3800
MouseClick, left, , , 1
}
return


^!m::
Loop, 400
{
Sleep, 4000
MouseClick, left, , , 1
}
return

beardboy
  • Members
  • 443 posts
  • Last active: May 13 2014 01:22 PM
  • Joined: 02 Mar 2004
^!z::
Loop, 50
{
  Sleep, 3800
  MouseClick, left, , , 1
  if zloop = 1
  {
    zloop = 0
    break
  }
}
return


^!m::
Loop, 400
{
  Sleep, 4000
  MouseClick, left, , , 1
  if mloop = 1
  {
    mloop = 0
    break
  }
}
return

^!s::
zloop = 1 ; Tell the ^!z Loop to stop
mloop = 1 ; Tell the ^!m Loop to stop
return
thanks,
beardboy

jamestr
  • Members
  • 98 posts
  • Last active: Jul 03 2008 12:30 AM
  • Joined: 05 Apr 2004
ctrl j stops the loop


^!m:: 
Loop, 40 
{ 
	if stopit = 1
		{
		stopit = 0
		break
		}
Sleep, 1000 
MsgBox, 0, ,  looping here, .6
} 
return

^j::
	MsgBox, 0, ,stopit !, .6
	stopit = 1
return


young707
  • Members
  • 4 posts
  • Last active: May 31 2004 04:33 PM
  • Joined: 29 May 2004
Thank you for your advise.

How do I stop and cancel in the middle of this repeating 100 keystroke?

^!a::
SetKeyDelay, 5000
send, {a 100}
return

Thank you.

jamestr
  • Members
  • 98 posts
  • Last active: Jul 03 2008 12:30 AM
  • Joined: 05 Apr 2004
try this

^!m::
stopit = 0
Loop, 3 
{
	if stopit = 1 
		exit
	MsgBox, 0, ,starting loop , .6
	loop, 20
		{
		sleep, 444
		if stopit = 1
			{
			MsgBox, 0, ,stopit !, .6
			break
			}
		send, %a_index% {enter}	
		}
Sleep, 1544 
} 
return

!j::
	
	stopit = 1
return



jamestr
  • Members
  • 98 posts
  • Last active: Jul 03 2008 12:30 AM
  • Joined: 05 Apr 2004
you can also toggle a timer on or off, as shown below.

warning: run the script in notepad window, the script sends {enter} keys.

run notepad.exe
Rshift & k:: 
settimer, loopit, 5000

loopit: 
MsgBox, 0, ,starting loop , .6
	loop, 30
	{
	sleep, 20
	if stopit = 1
		{
		stopit = 0
		MsgBox, 0, ,stopit !, .6
		Break
		}
	send, %a_index% {enter}	
	}
Sleep,  44 
return

rshift & l::
	stopit = 1
	settimer, loopit, off
return


jamestr
  • Members
  • 98 posts
  • Last active: Jul 03 2008 12:30 AM
  • Joined: 05 Apr 2004
this uses gosub to loop without initial delay.

you can also toggle a timer on or off, as shown below.

warning:
run the script in notepad window, the script sends {enter} keys.



run notepad.exe
!r::reload


Rshift & k::
	settimer, loopit, 5000
	gosub,  loopit
return

rshift & l::
	stopit = 1
	settimer, loopit, off
return

loopit: 
MsgBox, 0, ,starting loop , .6
	loop, 30
	{
	sleep, 20
	if stopit = 1
		{
		stopit = 0
		MsgBox, 0, ,stopit !, .6
		Break
		}
	send, %a_index% {enter}	
	}
Sleep,  44 
return


young707
  • Members
  • 4 posts
  • Last active: May 31 2004 04:33 PM
  • Joined: 29 May 2004
Thank you.

I see we cannot stop this repeat send command.
Sounds like this command should be able to do emergency stop or cancel.
me think!

send, {a 100}

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

I see we cannot stop this repeat send command.
send, {a 100}

To make a Send interruptible, as in jamestr's examples, enclose the command in a loop instead:
Loop, 100
{
Send, a
}

Then any other hotkey that does an ExitApp, or that notifies the loop to stop via global variables such as in jamestr's examples, will terminate the loop.

young707
  • Members
  • 4 posts
  • Last active: May 31 2004 04:33 PM
  • Joined: 29 May 2004
I see.
But good design, the repeat "send" should be able to stop or cancel, instead of the loop requirements.
Me think.