Start loop again with a key press Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest

Start loop again with a key press

10 Sep 2018, 00:53

Here's a script that checks if color is white. If it is, it start Loop anew. If it's not, it waits 10 seconds before starting Loop again. Please help me make it start Loop anew with a key press so it won't wait 10 seconds in the second situation.

Code: Select all

Loop {
Sleep 20
      Clipboard = hello
Send {Shift down}{Insert}{Shift Up}
	  Sleep 160
      Wait2()
   }
 

   Wait2()
{
	CoordMode, Pixel, Screen
	CoordMode, Mouse, Screen
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
	sleep 10000
		break
	}
}

  ralt::
  pause
  Return
Rindis
Posts: 213
Joined: 23 Dec 2013, 13:58
Location: Norway
Contact:

Re: Start loop again with a key press

13 Sep 2018, 12:13

Could the banana fix it?
Not really sure what you want to happen....
(code untested)

Code: Select all

; see https://autohotkey.com/boards/viewtopic.php?t=11952

#maxThreadsPerHotkey, 2
setKeyDelay, 50, 50
setMouseDelay, 50
banana:=0
$f1::
	; banana:=!banana .... This assigns banana to the value of NOT (!) banana. so lets
	; say banana starts out FALSE (0). you then turn banana to NOT FALSE. which is
	; TRUE (1). so now banana is set to TRUE. and then lets say you toggle it again.
	; you set banana to NOT TRUE, which is FALSE. banana is now set to FALSE. 
	; .... 1 is true, 0 is false. ! is NOT.
	banana:=!banana
	


Loop {
Sleep 20
      Clipboard = hello
Send {Shift down}{Insert}{Shift Up}
	  Sleep 160
      Wait2()
   }
 

   Wait2()
{
	CoordMode, Pixel, Screen
	CoordMode, Mouse, Screen
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
	Else
	{
	while (banana=1)
		{
		sleep 10000
		break
		}
		break
	}
	}
}

  ralt::
  pause
  Return
Guest

Re: Start loop again with a key press

14 Sep 2018, 23:09

This didn't work. Thank for trying to help.
I have a code that sends "hello" and searches for white color at (20, 285) every 160 milliseconds.
1. If it finds it, it keeps doing it.
2. If it doesn't find it, it waits 10 seconds and then keeps doing it.

I want to break this 10 seconds sleep at my will with a key so the code goes back to doing the loop.
Rohwedder
Posts: 7552
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Start loop again with a key press

15 Sep 2018, 01:21

Hallo,
to break this 10 seconds with key Escape
replace sleep 10000 by:

Code: Select all

End := A_TickCount + 10000
While A_TickCount < End
	Sleep, 50
and add at the end of your script:

Code: Select all

*Esc::End=
Guest

Re: Start loop again with a key press

15 Sep 2018, 09:20

Rohwedder wrote:Hallo,
to break this 10 seconds with key Escape
replace sleep 10000 by:

Code: Select all

End := A_TickCount + 10000
While A_TickCount < End
	Sleep, 50
and add at the end of your script:

Code: Select all

*Esc::End=
It didn't work.
Rindis
Posts: 213
Joined: 23 Dec 2013, 13:58
Location: Norway
Contact:

Re: Start loop again with a key press

16 Sep 2018, 05:56

try this

F2 will start the program

F1 will break the waiting

Code: Select all

; see https://autohotkey.com/boards/viewtopic.php?t=11952
#SingleInstance, Force
#maxThreadsPerHotkey, 2
setKeyDelay, 50, 50
setMouseDelay, 50


$f1::
	; banana:=!banana .... This assigns banana to the value of NOT (!) banana. so lets
	; say banana starts out FALSE (0). you then turn banana to NOT FALSE. which is
	; TRUE (1). so now banana is set to TRUE. and then lets say you toggle it again.
	; you set banana to NOT TRUE, which is FALSE. banana is now set to FALSE. 
	; .... 1 is true, 0 is false. ! is NOT.
	banana:=!banana
return

F2::
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

Loop 
{
	banana:=0
	Sleep 20
	Clipboard = hello
	Send {Shift down}{Insert}{Shift Up}
	Sleep 160

	Loop
		{
			banana:=0
			Sleep 20
			PixelGetColor, col, 20, 285 
		 If (col = 16777215)
			break
		
		while (banana=0)
			{
				
				sleep 100
				if (A_Index = 100)
				break
			}
			
		}
		  
		  
		  
   }
return




  ralt::
  pause
  Return



esc::
ExitApp
Guest

Re: Start loop again with a key press  Topic is solved

17 Sep 2018, 01:02

Rindis wrote:try this

F2 will start the program

F1 will break the waiting

Code: Select all

; see https://autohotkey.com/boards/viewtopic.php?t=11952
#SingleInstance, Force
#maxThreadsPerHotkey, 2
setKeyDelay, 50, 50
setMouseDelay, 50


$f1::
	; banana:=!banana .... This assigns banana to the value of NOT (!) banana. so lets
	; say banana starts out FALSE (0). you then turn banana to NOT FALSE. which is
	; TRUE (1). so now banana is set to TRUE. and then lets say you toggle it again.
	; you set banana to NOT TRUE, which is FALSE. banana is now set to FALSE. 
	; .... 1 is true, 0 is false. ! is NOT.
	banana:=!banana
return

F2::
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

Loop 
{
	banana:=0
	Sleep 20
	Clipboard = hello
	Send {Shift down}{Insert}{Shift Up}
	Sleep 160

	Loop
		{
			banana:=0
			Sleep 20
			PixelGetColor, col, 20, 285 
		 If (col = 16777215)
			break
		
		while (banana=0)
			{
				
				sleep 100
				if (A_Index = 100)
				break
			}
			
		}
		  
		  
		  
   }
return




  ralt::
  pause
  Return



esc::
ExitApp
This doesn't work. It doesn't even have 10 sec break in the code :(
Rindis
Posts: 213
Joined: 23 Dec 2013, 13:58
Location: Norway
Contact:

Re: Start loop again with a key press

17 Sep 2018, 04:29

This part hast the 10 sec break:
while (banana=0)
{

sleep 100
if (A_Index = 100)
break
}

100 * 100 = 10 000 ....

how does it not work? what happens if you run it
Guest

Re: Start loop again with a key press

17 Sep 2018, 05:59

Rindis wrote:This part hast the 10 sec break:
while (banana=0)
{

sleep 100
if (A_Index = 100)
break
}

100 * 100 = 10 000 ....

how does it not work? what happens if you run it
If pixel's color is not white it doesn't do the loop every 10 seconds. It doesn't write hello every 10 seconds.
Guest

Re: Start loop again with a key press

17 Sep 2018, 10:13

I tried to insert the 'banana' part to the original code instead of Sleep 10000 but it didn't do anything:

Code: Select all

$f1::
	; banana:=!banana .... This assigns banana to the value of NOT (!) banana. so lets
	; say banana starts out FALSE (0). you then turn banana to NOT FALSE. which is
	; TRUE (1). so now banana is set to TRUE. and then lets say you toggle it again.
	; you set banana to NOT TRUE, which is FALSE. banana is now set to FALSE. 
	; .... 1 is true, 0 is false. ! is NOT.
	banana:=!banana
return

Loop {
Sleep 20
      Clipboard = hello
Send {Shift down}{Insert}{Shift Up}
	  Sleep 160
      Wait2()
   }
 

   Wait2()
{
	CoordMode, Pixel, Screen
	CoordMode, Mouse, Screen
	Loop
	{
		Sleep 20
		PixelGetColor, col, 20, 285 
	 If col = 16777215 
		break
		while (banana=0)
			{
				
				sleep 100
				if (A_Index = 100)
				break
			}
	}
}

  ralt::
  pause
  Return
Rindis
Posts: 213
Joined: 23 Dec 2013, 13:58
Location: Norway
Contact:

Re: Start loop again with a key press

17 Sep 2018, 12:01

Sorry then I can't help you
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Start loop again with a key press

21 Sep 2018, 09:10

Code: Select all

F1::
Timeout:
	SetTimer CheckColor, 160
	Gosub CheckColor
Return

CheckColor:
	PixelGetColor color, 20, 285, RGB
	if (color == 0xFFFFFF)
	{
		Clipboard := "hello"
		Send {Shift Down}{Insert}{Shift Up}
	}
	else
	{
		SetTimer, , Off
		SetTimer Timeout, -10000
	}
Return
Guest

Re: Start loop again with a key press

21 Sep 2018, 18:49

swagfag wrote:

Code: Select all

F1::
Timeout:
	SetTimer CheckColor, 160
	Gosub CheckColor
Return

CheckColor:
	PixelGetColor color, 20, 285, RGB
	if (color == 0xFFFFFF)
	{
		Clipboard := "hello"
		Send {Shift Down}{Insert}{Shift Up}
	}
	else
	{
		SetTimer, , Off
		SetTimer Timeout, -10000
	}
Return
It looks like it would work but it doesn't do anything.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Start loop again with a key press

27 Sep 2018, 11:35

Code: Select all

F1::
	dbg("<HOTKEY> pressed")
	SetTimer CheckColor, 10
	Gosub CheckColor
Return

Timeout:
	dbg("<TIMEOUT> triggered")
	SetTimer CheckColor, 10
	Gosub CheckColor
Return

CheckColor:
	PixelGetColor color, 20, 285, RGB
	dbg("CheckColor...", 10, 25, 2)
	dbg(Format("Found - 0x{:X}", color), 10, 25, 2)
	Tooltip % "^ - Pixel Location", 10, 295, 3

	if (color == 0xFFFFFF)
	{
		dbg("Sending...", 10, 50)
		dbg("...Hello", 10, 50)
		; Clipboard := "hello"
		; Send {Shift Down}{Insert}{Shift Up}
	}
	else
	{
		dbg("CheckColor <OFF>", 10, 50)
		SetTimer, , Off
		dbg("Priming <TIMEOUT>", 10, 50)
		SetTimer Timeout, -5000
	}
Return

dbg(text, x := 10, y := 0, num := 1) {
	static DELAY := 350
	ToolTip % Format("[{}]: {}", A_TickCount, text), x, y, num
	Sleep DELAY
}
demo
it would appear to me the script is behaving exactly as requested, ie search loop can be restarted with a hotkey on demand and will automatically restart itself after a timeout period if no color was found
if it isnt working, the problem might have to do with the coordinates, color or sending mode

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 113 guests