Jump to content

Help with looping a search for a pixel color (with hotkey)


Recommended Posts

I was trying to make an AutoIt script that any pixel with the color 0xB200 in the coordinate range 0,0 to 100, 200 but I also wanted it to repeatedly search for that pixel until I use my hotkey {esc} to stop the search, and {esc} to start it again. Heres my current code, the issue is that it won't do anything when I click {esc}
 

#include <AutoItConstants.au3>
 HotKeySet("{esc}","Loop")

$looping = 0

Func Loop()
   If $looping == 1 Then ; if it is searching stop searching
      $looping = 0
   Else
      Do
         $coord = PixelSearch(0, 0, 100, 200, 0xB200)
         If Not @error Then
            MouseClick($MOUSE_CLICK_LEFT, $coord[0], $coord[1], 2, 1)
            MsgBox(0, " Clicked at X:", $coord[0] & "and Y:" & $coord[1])
         EndIf

      Until $looping <> 0
      $looping = 1
   EndIf
EndFunc

Thanks in advance!

Link to post
Share on other sites
#include <AutoItConstants.au3>
HotKeySet("{esc}", "Loop")

$looping = 0
Loop()

While 1
    ConsoleWrite("Searching" & @LF)
    $coord = PixelSearch(0, 0, 100, 200, 0xB200)
    If Not @error Then
        MouseClick($MOUSE_CLICK_LEFT, $coord[0], $coord[1], 2, 1)
        MsgBox(0, " Clicked at X:", $coord[0] & "and Y:" & $coord[1])
    EndIf
WEnd

Func Loop()
    $looping = Not $looping
    ConsoleWrite("Paused" & @LF)
    While $looping
        Sleep(200)
    WEnd
EndFunc   ;==>Loop

 

Link to post
Share on other sites
9 hours ago, badcoder123 said:
#include <AutoItConstants.au3>
HotKeySet("{esc}", "Loop")

$looping = 0
Loop()

While 1
    ConsoleWrite("Searching" & @LF)
    $coord = PixelSearch(0, 0, 100, 200, 0xB200)
    If Not @error Then
        MouseClick($MOUSE_CLICK_LEFT, $coord[0], $coord[1], 2, 1)
        MsgBox(0, " Clicked at X:", $coord[0] & "and Y:" & $coord[1])
    EndIf
WEnd

Func Loop()
    $looping = Not $looping
    ConsoleWrite("Paused" & @LF)
    While $looping
        Sleep(200)
    WEnd
EndFunc   ;==>Loop

 

Hmmm, First off you cant just use a function like this "Loop()" you need to call it like this Call("Loop") in order for it to run. Second, wouldn't the Loop needs to be run after it was defined? It is only sending "Paused" to console and never sends "Searching" when I try to use this. After some more testing, it seems that it wasn't detecting the ESC key being pressed, I edited it to +1 (Shift+1) and it showed the Paused when it started and the Paused when I clicked it. It then stopped the script with exit code 0 

Link to post
Share on other sites

You don't need to use the Call function to call a function, it's redundant.

Here's a modified script that might work as intended.

#include <AutoItConstants.au3>
HotKeySet("{esc}", "Loop")
HotKeySet("{tab}", "_Exit")

$Paused = 0
;~ Loop()

While 1
     If Not $Paused Then
          ConsoleWrite("Searching" & @LF)
          $coord = PixelSearch(0, 0, 100, 200, 0xB200)
          If Not @error Then
               MouseClick($MOUSE_CLICK_LEFT, $coord[0], $coord[1], 2, 1)
               MsgBox(0, " Clicked at X:", $coord[0] & "and Y:" & $coord[1])
          EndIf
          Sleep(1000) ; <<<< Sleeps for 1 second before searching again
     Else
          ConsoleWrite("Paused" & @CRLF)
          Sleep(1000) ; << Used so it's not spamming the console all the time
     EndIf
WEnd

Func Loop()
     $Paused = Not $Paused
EndFunc   ;==>Loop
Func _Exit()
    Exit
EndFunc

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites
9 hours ago, BrewManNH said:

You don't need to use the Call function to call a function, it's redundant.

Here's a modified script that might work as intended.

#include <AutoItConstants.au3>
HotKeySet("{esc}", "Loop")
HotKeySet("{tab}", "_Exit")

$Paused = 0
;~ Loop()

While 1
     If Not $Paused Then
          ConsoleWrite("Searching" & @LF)
          $coord = PixelSearch(0, 0, 100, 200, 0xB200)
          If Not @error Then
               MouseClick($MOUSE_CLICK_LEFT, $coord[0], $coord[1], 2, 1)
               MsgBox(0, " Clicked at X:", $coord[0] & "and Y:" & $coord[1])
          EndIf
          Sleep(1000) ; <<<< Sleeps for 1 second before searching again
     Else
          ConsoleWrite("Paused" & @CRLF)
          Sleep(1000) ; << Used so it's not spamming the console all the time
     EndIf
WEnd

Func Loop()
     $Paused = Not $Paused
EndFunc   ;==>Loop
Func _Exit()
    Exit
EndFunc

 

Thanks Brew, that worked perfectly, didn't know about the Call not being needed. when i tried to use mine it was erroring so i was confused.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...