Sign in to follow this  
Followers 0
SomeBody22

How to click in a sequence of pixels

9 posts in this topic

Hi, I need a way to click sequence of pixels.

I want to give just a click where you find the following colors:

Example: Blue>> Yellow>> White

post-67262-0-81663500-1328174093_thumb.p

This is my code, but is not working properly, it is clicking somewhere else:

MsgBox(0, "", "Initializing", 5)
$ColorLeft = 0x003366FF ;Blue
$ColorCenter = 0x00FFFF00 ;Yellow
$ColorRight = 0x00FFFFFF ;White
$ColorExists = 0
$LeftPosition = 1
$TopPosition = 1
Do
    $First = PixelSearch($LeftPosition, $TopPosition, @DesktopWidth, @DesktopHeight, $ColorLeft)
    If IsArray($First) Then
        $LeftPosition = $First[0]
        $TopPosition = $First[1]
        $Second = PixelSearch($LeftPosition, $TopPosition, @DesktopWidth, @DesktopHeight, $ColorCenter)
        $LeftPosition = $Second[0]
        If IsArray($Second) Then
            $Third = PixelSearch($LeftPosition, $TopPosition, @DesktopWidth, @DesktopHeight, $ColorRight)
            If IsArray($Second) Then
                $Left = PixelGetColor($Second[0] - 1, $Second[1])
                $Center = PixelGetColor($Third[0] - 1, $Third[1])
                If $Left = $ColorLeft And $Center = $ColorCenter Then
                    MouseClick("left",$First[0], $First[1])
                    MsgBox(0, "", "Done")
                    $ColorExists = 1
                Else
                    ;$LeftPosition = $First[0]+1
                    $TopPosition = $First[1] + 1
                EndIf
            EndIf
        EndIf
    EndIf
Until $ColorExists <> 0

Share this post


Link to post
Share on other sites



You're searching your entire desktop area, is it possible one of those colors might exist somewhere else on your desktop or whatever is on the screen at the time? Limit the search area to where you think the thing you're looking for might be and you'll probably find less false clicks.


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

Share this post


Link to post
Share on other sites

Yes, I'm looking at the entire desktop. Because the idea is to identify exactly where the sequence of colors pixels to click it.

Share this post


Link to post
Share on other sites

That search pattern isn't going to find what you're looking for unless you limit the area to be searched. I can understand the first search looking through the entire screen, but you are only limiting the second/third search patterns by using the top and left of the found pixel before it. You're still searching the rest of the screen for the next pixel if the first pixel found isn't in the image you showed, it might find the next 2 colors anywhere on the screen.

If you've ever played the game Battleship, think of your search area as looking for the PT boat, you hit the first pixel, you should look for the second pixel within a very small area around the first one, not the whole screen, same as with the third pixel, search in a small area around the second pixel.If you don't find it in that area, rerun the first search pattern again and try again.

1 person likes this

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

Share this post


Link to post
Share on other sites

Well, thanks for the explanation, but it is really what I'm trying to do, he first looks around the desktop, if not find a sequence of pixels searched, he traces the beginning of search where the first pixel was found, but do not know where I am missing, since the research is not running in the correct area to redo the search ...

Below is a second code that I tried to make it work properly, but without success:

MsgBox(0, "", "Initializing", 5)
$ColorLeft = 0x003366FF ;Blue
$ColorCenter = 0x00FFFF00 ;Yellow
$ColorRight = 0x00FFFFFF ;White
$LeftPosition = 1
$ColorExists = 0
Do
$First = PixelSearch($LeftPosition, 1, @DesktopWidth, @DesktopHeight, $ColorLeft)
If IsArray($First) Then
  $Center = PixelGetColor($First[0] + 1, $First[1])
  $Right = PixelGetColor($First[0] + 2, $First[1])
  If $Center = $ColorCenter And $Right = $ColorRight Then
   $ColorExists = 1
   MouseClick("left", $First[0], $First[1])
   MsgBox(0, "", "Done")
  Else
   $LeftPosition = $First[0] + 1
   ToolTip("Searching... [" & $LeftPosition & "]", 1, 1)
  EndIf
EndIf
Until $ColorExists <> 0

Share this post


Link to post
Share on other sites

MsgBox(0, "", "Initializing", 5)
$ColorLeft = 0x0000ff ;Blue
$ColorCenter = 0xFFFF00 ;Yellow
$ColorRight = 0xFFFFFF ;White
$ColorExists = 0
$LeftPosition = 1
$TopPosition = 1
$RightPosition = @DesktopWidth
$BottomPosition = @DesktopHeight
Do
    $First = PixelSearch($LeftPosition, $TopPosition, $RightPosition, $BottomPosition, $ColorLeft)
    If IsArray($First) Then ; first pixel found, look for the second pixel
        MsgBox(64, "", "Found the first pixel")
        $LeftPosition = $First[0]
        $TopPosition = $First[1]
        $RightPosition = $LeftPosition + 100 ; search within 100 pixels to the right and below the first pixel
        $BottomPosition = $TopPosition + 100 ;
        $Second = PixelSearch($LeftPosition, $TopPosition, $RightPosition, $BottomPosition, $ColorCenter)
        If IsArray($Second) Then ; second pixel found, look for the third pixel
            MsgBox(64, "", "Found the second pixel")
            $LeftPosition = $Second[0]
            $TopPosition = $Second[1]
            $RightPosition = $LeftPosition + 100 ; search within 100 pixels to the right and below the second pixel
            $BottomPosition = $TopPosition + 100
            $Third = PixelSearch($LeftPosition, $TopPosition, $RightPosition, $BottomPosition, $ColorRight)
            If IsArray($Third) Then
                MsgBox(64, "", "Found the third pixel")
                $Left = PixelGetColor($Second[0] - 1, $Second[1])
                $Center = PixelGetColor($Third[0] - 1, $Third[1])
                If $Left = $ColorLeft And $Center = $ColorCenter Then
                    MouseClick("left", $First[0], $First[1])
                    MsgBox(0, "", "Done")
                    $ColorExists = 1
                Else ; third pixel not found in that area, reset the search area using the last checked spot
                    $LeftPosition = $RightPosition
                    $TopPosition = $BottomPosition
                    $BottomPosition = @DesktopHeight
                    $RightPosition = @DesktopWidth
                EndIf
            EndIf
        Else ; second pixel not found in that area, reset the search area using the last checked spot
            $LeftPosition = $RightPosition
            $TopPosition = $BottomPosition
            $BottomPosition = @DesktopHeight
            $RightPosition = @DesktopWidth
        EndIf
    Else
        $TopPosition = @DesktopHeight ; bail out, the first pixel isn't on the screen at all, no point looking any more
    EndIf
Until $TopPosition >= @DesktopHeight - 50
MsgBox("", "", "Exiting")

Something like this might work for you. It looks within a rectangle 100 pixels wide by 100 pixels tall for the next pixel color. If it doesn't find it in that area, it resets the search area and looks again.


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

Share this post


Link to post
Share on other sites

Thanks but still does not work. :)

Share this post


Link to post
Share on other sites

Check to make sure that the colors being searched for are the correct colors, and try a shade variation if the color is sometimes different by a little bit.


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

Share this post


Link to post
Share on other sites

I made some adjustments to the initial code and successfully I got what wanted.

Thanks for the support BrewManNH!

Share this post


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
Sign in to follow this  
Followers 0