Image find and ctrl click loop

Ask gaming related questions
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Image find and ctrl click loop

Post by timebones » 24 Jan 2021, 23:37

trying to create a script that will find an image on screen and control click it till there are no more or it hits 44. having troubles having it find the image match on screen
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 25 Jan 2021, 05:31

Use ImageSearch. Feel free to post your script to get feedback about it.
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 25 Jan 2021, 16:48

Code: Select all

f16::
coordMode,pixel

^1::
ImageSearch,ix,iy,0,0,2383,1137,G:\Mega\Autohotkey\Images\22800.png
msgbox % "Errorlevel: " . errorlevel . "`nix: " . ix . "`niy: " . iy
MouseClick, left,  ix+20, iy+20
return
[Mod edit: [code][/code] tags added.]

I have this. It will find the picture as long as it is in an windows photo window. I tried it within a program the same exact picture and it wont detect it. Along with this is will not click on the image.
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 25 Jan 2021, 16:53

Also thanks for any help!
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 25 Jan 2021, 16:57

By default, coordinates are relative to the active window-- whichever window is active when you trigger the routine. If your image is visible, it's likely out of the range that you specified. To modify the reference point for the coordinates, use CoordMode.
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 12:27

So I went through and made sure my coordinates were gather when the program window was active. Changed the cords in the script to reflect the proper ones and I am still getting error code one.

Code: Select all

f16::

^1::
ImageSearch,ix,iy,1685,102,2539,1248,G:\Mega\Autohotkey\Images\22800.png
msgbox % "Errorlevel: " . errorlevel . "`nix: " . ix . "`niy: " . iy
MouseClick, left,  ix+20, iy+20
return
[Mod edit: [code][/code] tags added.]


I cant spot what is wrong :(
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 12:38

Code: Select all

^!a::
  Loop
{
	WinActivate, EST.exe
	CoordMode Pixel
	CoordMode Mouse ;
	ImageSearch, FoundX, FoundY, 1690,345,2545,1339, *25 G:\Mega\Autohotkey\Images\22800.png
	sleep, 100
	err := Errorlevel 
	If (err = 0)
	{
      ;MsgBox Image Found
      ;Sleep, 500
      ;Click %FoundX%, %FoundY% 
		
		MouseMove, %fx%, %fy% 
		Sleep 500 
		click 
		sleep,500 
		break
	}
	Sleep, 500
}
return 

Found this on the forums and tried it but to no avail. Could it be a problem with the program and AHK cant match the image?
Last edited by gregster on 26 Jan 2021, 12:49, edited 1 time in total.
Reason: [code] tags added. Please use them yourself. Thank you!
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 26 Jan 2021, 13:04

Since the script does not work, pare it down: start with a static image that you want to find. Display it on your screen, say, with an image editor. Remove the hue variation option. Remove loop, mouse move, and click. You just want to get image search working. Run Window Spy. Activate your window. Check coordinates. Next, see if the script identifies your image.

This process usually narrows down the problem. In almost all cases, one of the following problems is occurring: file does not exist, images are not exactly the same, or coordinate reference point is misstated.

Code: Select all

file = G:\Mega\Autohotkey\Images\22800.png
If !FileExist(file) {
 MsgBox, 48, Error, File not found.`n`n%file%
 Return
}
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %file%
MsgBox, 0, Result, %file%`n`nx = %x%`ny = %y%
If it does not work, you can use a small portion of a new screenshot as your test file.
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 13:40

Okay so it can find the image anywhere on the screen but the moment I go back into the program it cannot find it. I made the screen shot even smaller to see if it can identify a smaller section with not luck. So it has to be that the images are not exactly the same even though I am clipping it from the program itself into a image file.
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 26 Jan 2021, 13:52

Which coordinates were identified as found? Are they within your search range? If so, then yes, perhaps the image is changing.
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 13:53

I changed the cords to the whole screen and yes it found it and would output a message box with the x and y when not in the program
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 26 Jan 2021, 14:10

OK, so if you are using CoordMode, Pixel, the search should also succeed with your program window active, as long as you are searching the entire screen, the image is visible, and the file matches the image.
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 15:02

managed to get it to work by giving a pixel variation *10

Do you know if I can make it repeat a certain amount of time or until no more matches are found?
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 15:14

Is there a way to control click as well. This causes the control to become constantly pressed.

Code: Select all

f15::ExitApp
f16::


Loop,44{
	coordMode,pixel
	
	^1::
	
	ImageSearch,ix,iy,0,0,2383,1137,*10 G:\Mega\Autohotkey\Images\28000.png
	
	
	Send, {Control down}
	sleep,100
	MouseClick,Left, ix+20, iy+20
	sleep,100
	Send, {Control up}
}
return
[Mod edit: [code][/code] tags added.]
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 26 Jan 2021, 15:45

One idea is below.

Code: Select all

F3::
CoordMode, Pixel
CoordMode, Mouse
Loop, 44 {
 ImageSearch, ix, iy, 0, 0, 2383, 1137, *10 G:\Mega\Autohotkey\Images\28000.png
 If !ErrorLevel {
  MouseMove, ix + 20, iy + 20
  Send ^{LButton}
 } Else Sleep, 300
} Until (ix > "")
Return
timebones
Posts: 15
Joined: 12 Jun 2020, 19:41

Re: Image find and ctrl click loop

Post by timebones » 26 Jan 2021, 18:16

Will try when I get home! Can I use the error level 1 to have the script then search for a 2nd picture?
User avatar
mikeyww
Posts: 5109
Joined: 09 Sep 2014, 18:38

Re: Image find and ctrl click loop

Post by mikeyww » 26 Jan 2021, 19:22

Yes, sure.
Post Reply

Return to “Gaming”