Search an area for a specific color and move mouse to that color

217 Views Asked by At

I am trying to set the X1, Y1, X2, Y2 variables of the PixelSearch to the X and Y coordinates of where the mouse is when 1 is initially pressed. I want to also add to the X2, Y2 so that it creates a rectangle of search area when looking for the designated color. (So X1 Y1 would be the top right point of the search area/rectangle and X2 Y2 would be the bottom right point.) E.g.illustration of rectangle search area around where pointer was when 1 was clicked

#SingleInstance, Force
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

1::
    MouseGetPos, mX, mY
    X1 = mX
    Y1 = mY
    X2 = mX + 100
    Y2 = mY + 100
    
    PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, 0xFAFF00, 1, Fast

    if (ErrorLevel = 0) {
        MouseMove, OutputVarX, OutputVarY, 1
    }
    else {
        MsgBox, error
        Sleep, 500
    }
return

2::Reload
3::ExitApp
1

There are 1 best solutions below

1
nickg On
#SingleInstance, Force
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

Loop
{
    1::
        CoordMode, Pixel, Screen
        CoordMode, Mouse, Screen

        Loop
        {
            PixelSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 0xFAFF00, 1, Fast
            if (ErrorLevel = 0)
            {
                MouseClick, left, FoundX, FoundY, 1, 0
                Sleep, 5000
            }
            else
            {
                Sleep, 100
            }
        }
    return

    2::Reload
    3::ExitApp
}