Need help to stop script when pixel in location is detected

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SRoy
Posts: 2
Joined: 04 Jun 2023, 03:19

Need help to stop script when pixel in location is detected

Post by SRoy » 04 Jun 2023, 03:26

I'm new and I just can't get this script to stop when it detects a pixel in a location. I've used another script to make sure ahk can detect the pixel. Please help thanks. I'm also not sure how to make it into v2 but it's fine.

Code: Select all


CoordMode, Pixel, RGB

Loop
{
    ; Wait for the Y key to be pressed
    $y::
        SetTimer, AutoClicker, % (Toggle := !Toggle) ? "100" : "Off"
        if (!Toggle)
        {
            SendInput {W up}
            Clicking := false
        }
    return

    AutoClicker:
        ; Set the X and Y coordinates of the click location
        ClickX := 1731
        ClickY := 845

        ; Set the X and Y coordinates of the pixel color location to be detected
        PixelX := 1379
        PixelY := 781

        ; Check the pixel color at the specified location
        PixelGetColor, Color, %PixelX%, %PixelY%

        ; Define the target color (replace with your desired color)
        TargetColor := 0xE9DDD4  ; Color #e9ddd4

        ; Check if the detected color matches the target color
        if (Color = TargetColor)
        {
            MsgBox, Target color detected! Script will now exit.
            if (Clicking)
            {
                Clicking := false
                Click %ClickX%, %ClickY%, up  ; Release the mouse button
            }
            SendInput {Space up} ; Release the SPACE BAR
            ExitApp  ; Exit the script
        }

        ; Perform clicking actions if Clicking flag is true
        if (Clicking)
        {
            Click %ClickX%, %ClickY%
            Sleep 10
        }

        ; Click on the specified location when Toggle is true
        if (Toggle)
        {
            Clicking := true
            Click %ClickX%, %ClickY%
        }

        ; Hold down the W key
        SendInput {W down}

        ; Press the spacebar
        Send {Space}

        ; Wait for a short duration (adjust as needed)
        Sleep 10

        ; Release the W key if Toggle is false
        if (!Toggle)
            SendInput {W up}

        ; Wait for a longer duration (adjust as needed)
        Sleep 10
    return
}
The below script is what I used to detect if ahk is actually picking up the color, which it did.

Code: Select all

CoordMode, Pixel, Screen

; Set the X and Y coordinates of the pixel color location to be detected
PixelX := 1379
PixelY := 781

; Define the target color (replace with your desired color)
TargetColor := 0xE9DDD4  ; Color #e9ddd4

Loop
{
    ; Check the pixel color at the specified location
    PixelGetColor, Color, %PixelX%, %PixelY%, RGB

    ; Compare the detected color with the target color
    if (Color = TargetColor)
    {
        MsgBox, Target color detected! The color is being detected correctly.
        Break  ; Exit the loop
    }

    Sleep 1000  ; Adjust the sleep duration as needed
}


User avatar
mikeyww
Posts: 27107
Joined: 09 Sep 2014, 18:38

Re: Need help to stop script when pixel in location is detected

Post by mikeyww » 04 Jun 2023, 05:40

Welcome to this AutoHotkey forum!

I have some suggestions for you.

1. Defining a hotkey inside a loop is not a way to determine when a key is pressed. First, using :: to define a hotkey is not subject to loops, If statements, or any code like that (other than certain directives). Second, you don't need that, because AutoHotkey is an event-driven program. When you define a hotkey, its subroutine will automatically execute when the key is pressed.

2. You wrote a long script and wonder why it doesn't work. One approach could be to start with something very short and simple. An example is below.

viewtopic.php?p=524665#p524665

After it works, you can expand it, add commands that you want to execute inside the loop, etc.

3. AutoHotkey doesn't let you make up your own command parameters. You have to use the ones that are provided. See the syntax for :arrow: CoordMode.

SRoy
Posts: 2
Joined: 04 Jun 2023, 03:19

Re: Need help to stop script when pixel in location is detected

Post by SRoy » 04 Jun 2023, 08:19

mikeyww wrote:
04 Jun 2023, 05:40
Welcome to this AutoHotkey forum!

I have some suggestions for you.

1. Defining a hotkey inside a loop is not a way to determine when a key is pressed. First, using :: to define a hotkey is not subject to loops, If statements, or any code like that (other than certain directives). Second, you don't need that, because AutoHotkey is an event-driven program. When you define a hotkey, its subroutine will automatically execute when the key is pressed.

2. You wrote a long script and wonder why it doesn't work. One approach could be to start with something very short and simple. An example is below.

viewtopic.php?p=524665#p524665

After it works, you can expand it, add commands that you want to execute inside the loop, etc.

3. AutoHotkey doesn't let you make up your own command parameters. You have to use the ones that are provided. See the syntax for :arrow: CoordMode.
Sorry I am new to autohotkey, I used ChatGPT to make this. I'll try to use the script you linked me. Thanks

User avatar
mikeyww
Posts: 27107
Joined: 09 Sep 2014, 18:38

Re: Need help to stop script when pixel in location is detected

Post by mikeyww » 04 Jun 2023, 09:54

OK. ChatGPT often generates garbage or nonsense. Your script provides one more example. ChatGPT output is not permitted in this forum.

Post Reply

Return to “Ask for Help (v1)”