Find color on screen ... Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Find color on screen ...

Post by AHKJeff » 02 Oct 2022, 16:51

I would like to create a hotkey that finds a specific color on the screen and right-clicks
anywhere in a box that contains that color.

This is what I have so far:

Code: Select all

F9::
PixelSearch, Px, Py, 1730, 397, 1906, 955, 0xFFFFCC, 3, Fast
if ErrorLevel
    MsgBox, That color was not found in the specified region.
else
    MsgBox, A color within 3 shades of variation was found at X%Px% Y%Py%.

return
That is supposed to be hex for an RGB value of (255,255,204).
I heard that AHK wants BGR instead of RGB, so I also tried using 0xCCFFFF.

In either case when I run the code with the correct color showing on the screen in the area designated,
I get the message: "That color was not found in the specified region."

So something isn't right with my test code.
I can't proceed to my actual code until this test works.
Any ideas what is wrong with my code?

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Find color on screen ...

Post by Xtra » 02 Oct 2022, 18:20

Set CoordMode to screen when using screen coordinates.

Code: Select all

CoordMode, Pixel, Screen

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Find color on screen ...

Post by boiler » 02 Oct 2022, 18:37

Also, you can leave it in RGB format 0xFFFFCC if you change the last PixelSearch parameter to include the RGB mode: Fast RGB

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Find color on screen ...

Post by AHKJeff » 02 Oct 2022, 19:18

Thanks folks.
Not sure which of the two (or both) suggestions did the trick, but it's working now! :)
When I did the second suggestion alone it didn't work.
But when I added the first it worked.

So now I will try to slowly build the net result I am looking for.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Find color on screen ...

Post by AHKJeff » 02 Oct 2022, 20:48

OK now I can definitely use some expert help. :)
There is/are one or two lines of code I need to add before
MouseClick, right.

The code is correctly finding the color when it is on-screen.
A MsgBox will even name the exact pixel location where the color is.
I need to tell AHK to move the mouse to that exact position.
Then the rest of the code should give the desired effect.

Code: Select all

F9::

CoordMode, Pixel, Screen

PixelSearch, Px, Py, 1730, 397, 1906, 955, 0xFFFFCC, 3, Fast RGB
if ErrorLevel
    MsgBox, That color was not found in the specified region.
else
    MouseClick, right 
    Send (Down 11}
    Send, {Enter}

return

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Find color on screen ...

Post by sofista » 02 Oct 2022, 20:59

Pretty straightforward. Try:

Code: Select all

MouseMove, px, py

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Find color on screen ...

Post by boiler » 02 Oct 2022, 21:34

Or just add the coordinates to your MouseClick command since you already have it:

Code: Select all

MouseClick, Right, Px, Py

AHKJeff wrote:
02 Oct 2022, 20:48
OK now I can definitely use some expert help. :)
I would suggest to you that you are not at all in need expert help to find such straightforward commands in the documentation. What could be easier than to look through the handful of commands that start with “Mouse” and not even have to read the details of each to find which move the mouse and/or click at a specified location?

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Find color on screen ...

Post by Xtra » 02 Oct 2022, 21:44

Coordinates are relative to the active window unless CoordMode was used to change that. If omitted, the cursor's current position is used.
You will need to set coordmode for mouse too.

Code: Select all

CoordMode, Mouse, Screen

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Find color on screen ...

Post by AHKJeff » 02 Oct 2022, 23:06

OK thanks folks.
I've incorporated the last two suggested lines.

I'm getting some weird unexpected behavior with my code.

When I open the pop-up window I need to open, manually, and hit the key sequence that
needs to be delivered, I get the desired result.

But when I run my AHK code (below) that does the same thing, I get different results.

Code: Select all

F9::

CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

PixelSearch, Px, Py, 1730, 397, 1906, 955, 0xFFFFCC, 3, Fast RGB
if ErrorLevel
    MsgBox, That color was not found in the specified region.
else

    MouseClick, Right, Px, Py
    Sleep 1200
    Send, o
    Sleep 1200
    Send, o
    Sleep 1200
    Send, (Up}
    Sleep 1200
    Send, (Up}
    Sleep 1200
    Send, {Enter}

return
I entered obscenely long sleep times to see what the code was up to.
Instead of sending o,o and then the two 'ups' and then 'enter'
AHK is sending o,o and then sending two {downs} followed by three {ups} and then {enter} !!!

Or AHK might not be sending that code, BUT my app is interpreting what's being sent as
what I've indicated above. :(

The right mouse click at the top of the code opens a pop-up menu that has 21 choices.
I want to highlight a choice near the middle of the list and then hit {Enter}.
I discovered a way to get close to the choice I want: If you hit the hot-key o (Set color)
and then once again, it brings the cursor to just two positions below where it needs to be.
But now when I want to tell the pop-up to select {up} two times, it does the crazy moves I mentioned above.

Any ideas I could try?

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Find color on screen ...  Topic is solved

Post by boiler » 02 Oct 2022, 23:14

First thing to fix is that indenting lines of code doesn’t group them like you have done under the else. You need to define a block using { }, or else only the line immediately under the else is conditional. The rest get executed after the if/else no matter what.

Regarding your other issue, look closely at your code. You are not sending {Up}, you are sending (Up}, which is totally different and would be expected to have wildly different results. Instead of sending the Up key, you are sending the characters (, U, p, and } in sequence. In your earlier post, you are sending (Down 11} instead of {Down 11}.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Find color on screen ...

Post by AHKJeff » 02 Oct 2022, 23:34

boiler wrote:
02 Oct 2022, 23:14
First thing to fix is that indenting lines of code doesn’t group them like you have done under the else. You need to define a block using { }, or else only the line immediately under the else is conditional. The rest get executed after the if/else no matter what.

Regarding your other issue, look closely at your code. You are not sending {Up}, you are sending (Up}, which is totally different and would be expected to have wildly different results. Instead of sending the Up key, you are sending the characters (, U, p, and } in sequence. In your earlier post, you are sending (Down 11} instead of {Down 11}.
BINGO!
You've got 'em. :)

I made the two adjustments to my code and it works now! (At least in slow motion).
I will gradually speeds things up by adjusting the Sleeps.

Thanks very much!

I know what happened with the sloppy coding (Up} instead of {Up}.
I did have it right, but then as I was making various adjustments, deleting
and adding lines, a '(' slipped in there and I just copied it.

Post Reply

Return to “Ask for Help (v1)”