Find color closest to cursor and click Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Find color closest to cursor and click

Post by Hellbent » 15 Apr 2024, 00:52

mamo691 wrote:
12 Apr 2024, 20:57
So basically can you make it work more accurately with much smaller targets, and also make a option to remove mask because my stupid head cant find the 3-4 lines of code that you mentioned here :
- If you don't need a mask you can comment out the 3-4 lines of code that handle displaying the block windows.

And also help me out with the keybinds please?

I think that the accuracy problem that you were having may have been due to an unpolished feature that I just duct taped in so that I could test with black as the target color. I have removed it so hopefully you won't have an issue with targets near the edge of the screen.

I have also removed the mask windows and a bunch of the other unused code, without the masking windows you won't be able to find multiple targets unless the act of finding them and interacting with them causes them to disappear on their own. From your gif it looks like they don't disappear so you will likely either have to use the mask or use @Chunjee's method if you want to find multiple targets.

As for the key stuff, I'm not really into writing sending key combos for other people or "hotkey" stuff in general, fortunately there are many people on the forum that do help with sending keys and other hotkey related things.

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
CoordMode, ToolTip, Screen
;********************
colorList 		:= [ "0xFF0000" ]
;********************
variation 		:= 10
range 			:= 300
rate 			:= 20
currentFinds	:= 0
maxFinds		:= 1
;********************
return
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&;
*ESC::ExitApp
;********************	
$g:: ;{ start search
	dist := 1
	currentFinds:= 0
	
	Gui, t:New, +AlwaysOnTop -DPIScale -Caption +Border +LastFound +ToolWindow +E0x20
	WinSet, TransColor, f0f0f0
	
	While( GetKeyState( "g" , "p" ) ){
		
		MouseGetPos, cx, cy 
		
		if( cx != lx || cy != ly ){
			lx := cx 
			ly := cy 
			dist := 1
		}
		
		sx := cx - dist
		ex := cx + dist
		sy := cy - dist
		ey := cy + dist
		
		Gui, t:Show, % "x" sx - 1 " y" sy - 1 " w" ex - sx + 2 " h" ey - sy + 2 " NA"
		
		for k , v in colorList	{
			
			PixelSearch, tx, ty, sx, sy, ex, ey, % colorList[ k ], % variation, RGB Fast
			
			if( !ErrorLevel ){
				
				++currentFinds
				;********************
				Action_Function_1( tx , ty ) ;<<<<<-----  Call the function that contains the actions to perform if the color is found. Passing the found x and found y values.
				;********************
				
				if( currentFinds = maxFinds && maxFinds )
					break 2
				
				dist := 1
					
			}
		}
		
		if( ( dist += rate ) >= ( range + rate ) )
			dist := 1
			
	}
	Gui, t:Destroy
	KeyWait, g
	return
;}	
;********************
;********************
;********************
;When the color is found it runs this code.
Action_Function_1( x , y ){ ;<--- You can "pass" as many arguments as you need to. In this example I am just passing the x and y values for where the color was found.
	
	;~ MouseMove, x, y 							;example of moving the cursor.
	;~ Sleep, 500			
	;********************
	;This is the action you asked for.
	send, { Space, Down } 					;hold the space key down
	sleep, 30								;wait a fraction of a second
	Send, % "{ Click , " x " , " y " }"		;send a click
	sleep, 30								;wait a fraction of a second
	send, { Space, Up }						;release the space key.
	sleep, 30								;wait a fraction of a second
	;********************
	
	return 1
	
}
;********************

mamo691
Posts: 41
Joined: 07 Jan 2024, 12:40

Re: Find color closest to cursor and click

Post by mamo691 » 15 Apr 2024, 10:24

the code totally stopped workin for me idk @hellbent

mamo691
Posts: 41
Joined: 07 Jan 2024, 12:40

Re: Find color closest to cursor and click

Post by mamo691 » 15 Apr 2024, 10:25

it spams grey line squares and thats it, im trying it with the same game and i didnt make any changes but change the color

mamo691
Posts: 41
Joined: 07 Jan 2024, 12:40

Re: Find color closest to cursor and click

Post by mamo691 » 15 Apr 2024, 10:27

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
CoordMode, ToolTip, Screen
;********************
colorList 		:= [ "0x101AFD" ]
;********************
variation 		:= 10
range 			:= 300
rate 			:= 20
currentFinds	:= 0
maxFinds		:= 1
;********************
return
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&;
*ESC::ExitApp
;********************	
$g:: ;{ start search
	dist := 1
	currentFinds:= 0
	
	Gui, t:New, +AlwaysOnTop -DPIScale -Caption +Border +LastFound +ToolWindow +E0x20
	WinSet, TransColor, f0f0f0
	
	While( GetKeyState( "g" , "p" ) ){
		
		MouseGetPos, cx, cy 
		
		if( cx != lx || cy != ly ){
			lx := cx 
			ly := cy 
			dist := 1
		}
		
		sx := cx - dist
		ex := cx + dist
		sy := cy - dist
		ey := cy + dist
		
		Gui, t:Show, % "x" sx - 1 " y" sy - 1 " w" ex - sx + 2 " h" ey - sy + 2 " NA"
		
		for k , v in colorList	{
			
			PixelSearch, tx, ty, sx, sy, ex, ey, % colorList[ k ], % variation, RGB Fast
			
			if( !ErrorLevel ){
				
				++currentFinds
				;********************
				Action_Function_1( tx , ty ) ;<<<<<-----  Call the function that contains the actions to perform if the color is found. Passing the found x and found y values.
				;********************
				
				if( currentFinds = maxFinds && maxFinds )
					break 2
				
				dist := 1
					
			}
		}
		
		if( ( dist += rate ) >= ( range + rate ) )
			dist := 1
			
	}
	Gui, t:Destroy
	KeyWait, g
	return
;}	
;********************
;********************
;********************
;When the color is found it runs this code.
Action_Function_1( x , y ){ ;<--- You can "pass" as many arguments as you need to. In this example I am just passing the x and y values for where the color was found.
	
	;~ MouseMove, x, y 							;example of moving the cursor.
	;~ Sleep, 500			
	;********************
	;This is the action you asked for.
	send, { Space, Down } 					;hold the space key down
	sleep, 30								;wait a fraction of a second
	Send, % "{ Click , " x " , " y " }"		;send a click
	sleep, 30								;wait a fraction of a second
	send, { Space, Up }						;release the space key.
	sleep, 30								;wait a fraction of a second
	;********************
	
	return 1
	
}
;********************

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Find color closest to cursor and click

Post by Hellbent » 15 Apr 2024, 11:05

mamo691 wrote:
15 Apr 2024, 10:25
it spams grey line squares and thats it, im trying it with the same game and i didnt make any changes but change the color
It looks like you are using a BGR format rather than an RGB one.

Code: Select all

;********************
colorList 		:= [ "0x101AFD" ]
;********************
^ That color in RGB would be blue. Either change the code to use BGR or get the color again in RGB.
It should be "0xFD1A10" in RGB.

Post Reply

Return to “Ask for Help (v1)”