Script that "monitors" a coordinate for certain pixel color

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AfcaEric
Posts: 8
Joined: 29 Sep 2018, 06:29

Script that "monitors" a coordinate for certain pixel color

15 Oct 2018, 06:18

Hi all,

I'm looking for a pretty simple script. The only catch is: it needs to perform multiple things at the same time.
What i'm looking for:
- I have 2 hotkeys that perform 2 very simple Click actions
- While i'm using those 2 hotkeys, i would like a toggle (or constant action) that monitors a X, Y coordinate for color Z, and if that color is found perform some more actions.

I did found the "WaitPixelColor" function, but my Click hotkeys are seriously impacted on performance due to this script, and there is a static timeout which i need to wait for. I need the click actions as fast as possible (with the basic "Click, X, y" function it's fast enough).

Is this even possible?
eelrod
Posts: 65
Joined: 10 Apr 2018, 11:17

Re: Script that "monitors" a coordinate for certain pixel color

15 Oct 2018, 07:29

I haven't used the "WaitPixelColor" function before, so I can't really comment on that; but have you tried using a timer with PixelGetColor? That's probably the route I would take, but I'm sure some of the AHK gurus about have a clever solution.

https://autohotkey.com/docs/commands/SetTimer.htm
https://autohotkey.com/docs/commands/PixelGetColor.htm
AfcaEric
Posts: 8
Joined: 29 Sep 2018, 06:29

Re: Script that "monitors" a coordinate for certain pixel color

16 Oct 2018, 00:48

eelrod wrote:
15 Oct 2018, 07:29
I haven't used the "WaitPixelColor" function before, so I can't really comment on that; but have you tried using a timer with PixelGetColor? That's probably the route I would take, but I'm sure some of the AHK gurus about have a clever solution.

https://autohotkey.com/docs/commands/SetTimer.htm
https://autohotkey.com/docs/commands/PixelGetColor.htm
I was thinking about PixelGetColor as well. I think it should be possible, but the difficult part would be that i want the PixelGetColor function constantly active (or "monitoring"). The 2 Hotkeys i'm using are generating new screens, i want the PixelGetColor function just to be constant active, and if it finds the desired color on X,Y then instantly perform another function.

Edit:
I currently have something like this (copied from other AHK user):

Code: Select all

#Persistent
#NoEnv		; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input	; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%	; Ensures a consistent starting directory.
CoordMode, Click, Pixel, Screen

X := 1627		; X coordinate - CHANGE THIS TO WHAT YOU NEED
Y := 623		; Y coordinate - CHANGE THIS TO WHAT YOU NEED
SetColor := 0x783BF9		; Color at X,Y from above - CHANGE THIS TO WHAT YOU NEED

SetTimer, AutoClick, 100		; Runs 100 times a second to check color
Return

AutoClick: 
Pixelgetcolor, Color, %X%, %Y% 			; Check the color at X,Y
if (Color = SetColor) 					; Compare the color we set to the color we see now
{
	Click, 1673, 744
	sleep 100
	Click, 1046, 666
	return
}
I think it worked once, but now it's not working anymore. I'm 100% sure the set color is on the set coordinates, but it's not performing the click routine. Any ideas?


Edit2:
Ok, i just removed the brackets from the If statement, and that "locked" up my system (couldn't move the mouse since it was resetting everytime). I'm still no pro so i'm messing around with If statements and Coordmode and stuff. I think this is the function i want, now just get it to work.
eelrod
Posts: 65
Joined: 10 Apr 2018, 11:17

Re: Script that "monitors" a coordinate for certain pixel color

16 Oct 2018, 08:03

It looks like your code works, it there are a couple of issues. The first is just a clarification based on one of your comments in the code. Where it says "Runs 100 time a second" next to the timer, it's actually 10 times a second as you have it because the timer is based in milliseconds. The second issue that is preventing the click routine is the sleep 100 in your if-true statement. Since your timer runs every 100 milliseconds and there is a sleep for 100 milliseconds, the timer re-runs before the second click. To fix the issue, try changing your sleep to something lower, like 50; or setting your timer to something higher, like 150.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Joey5, JPMuir, KolaBorat, mebelantikjaya, Thorlian and 170 guests