ImageSearch in a loop Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

ImageSearch in a loop

17 Aug 2017, 15:13

Hi,
Im trying to make a script that keep clicking on images. All these clicks are in a loop. The problem is that i end up with infinite clicks everywhere i move the mouse (desktop/taskbaer/etc)
The code is:

Code: Select all

F4::
SetDefaultMouseSpeed 30
CoordMode Pixel
loop {
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%,*20 c:\1.bmp
	If (ErrorLevel == 0){
		Click, %foundX%, %foundY%
		ErrorLevel=5
	}
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%,*20 c:\2.bmp
	If (ErrorLevel == 0){
		Click, %foundX%, %foundY%
		ErrorLevel=5
	}
	else
	{
		ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%,*50 2_1.bmp
		If (ErrorLevel == 0){
			Click, % foundX-40, %foundY%,down
			MouseMove, % foundX-40, % foundY-50, 80  <--mousedrag had some issues probably from continuous clicks so ive tryed this method
			Click left up
			ErrorLevel=5
		}
	}
	Sleep, 2000
	ErrorLevel=5
}
return

F3::
Exit
return
I have no clue what am i doing wrong.
I even try to change errorlevel value thinking that it will prevent it from entering all those ifs. but no luck
this script is for an android game on nox emulator. i know nox has its own macros but i does not communicate between 2 or more instances of nox so i need to create my own macro.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: ImageSearch in a loop

17 Aug 2017, 22:54

I honestly don't know if you are allowed to give your own value to ErrorLevel, but I suppose so.

Your code at a glance looks correct otherwise. Have you tried not using the tolerance factor of *20 on those images? I wonder if you're too generous and it's getting a lot of false positives.

And you shouldn't be getting clicks wherever your mouse is moved to, the clicks should be moving your mouse to where the image is found. Is the expected image found there?

Oooooh, your Mouse coordinates may not match the Pixel coordinates. So that makes it click not where the images were found. Per CoordMode documentation:
If this command is not used, all commands except those documented otherwise (e.g. WinMove and InputBox) use coordinates that are relative to the active window.
Which means Pixel and Mouse CoordModes start off as Window (Relative). However:
If Param2 is omitted, it defaults to Screen.
You omitted Param2 in CoordMode Pixel, so that changed it to Screen mode. So now the coordinates are out of sync.

So it may be you have two separate problems going on. You may be too liberal with your ImageSearch variation of *20 which can cause false positives, and then your coordinates were not synchronized between Pixel and Mouse.
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

Re: ImageSearch in a loop

18 Aug 2017, 08:22

I've tried to add SCREEN option for CoordMode and also remove all tolerances for imagesearch. I ran the script on paint brush with one of the image that im searching for.
I still get non-stop clicks right after the image is recognized.
Also sometimes i think the mouse coordinates (when it actually start to move) are random.
If i'm correct the mouse should always move to the correct position relative to the image it founds. But it seems it gets random coordinates.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: ImageSearch in a loop

18 Aug 2017, 13:26

You should be getting a click (or two) every 2 seconds so long as any of the searched images are on screen.

Did you put CoordMode, Mouse, Screen and CoordMode, Pixel, Screen in your script? You're not getting back random coordinates, but they may appear random because your two coordmodes aren't synchronized and/or you are getting false positives (but if you removed the tolerance, that's far less likely).
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

Re: ImageSearch in a loop

18 Aug 2017, 17:38

Ok it seems that i missed a comma separator. Now with CoordMode, Pixel,Screen it seems that non-stop click is gone.
But as i said before i get a weird behavior of my mouse:
it founds the image 2. Mouse moves to image 2 position. i think it sorts of click it but not really. After that mouse moves away from the image.
I've added a few tooltip to figure out if image 3 is somehow mistaken with icon or desktop or something. But surprise: only image 2 is found. And on that if i have only a click.
So how come the mouse moves 2 times (1st time on the image and 2nd time outside probably 200-300 px to the right)? and it seems that its not quite clicking on the android emultator.
the emulator seems to become front app but the button on the emulator is not pressed although it needs only 1 click even when nox window is not focused.
Well i'm happy that at least i get rid of non-stop clicks :)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: ImageSearch in a loop

18 Aug 2017, 17:59

The mouse moving away from the image makes me think it is finding image 2_1.

Code: Select all

	Click, % foundX-40, %foundY%,down
			MouseMove, % foundX-40, % foundY-50, 80  <--mousedrag had some issues probably from continuous clicks so ive tryed this method
			Click left up
			ErrorLevel=5
However, this would move the mouse only up 50 pixels...

I don't see anywhere in your code to make the mouse move 200-300 px to the right, unless it is matching two images at a time (Image 1 and 2 or 1 and 2_1).

Edit: I just noticed that you have 2_1.bmp not c:\2_1.bmp -- is that causing any issues for you? You use c:\1.bmp and c:\2.bmp otherwise.

Do you have any other concurrently running code?

Tough to say why the emulator isn't responding to the artificial input like it does to human input. You can try doing two clicks instead of 1, with the first being used to focus the window. Or you can try WinActivate; it is possible to use MouseGetPos to decide which window the mouse is over which can then be passed as a variable into WinActivate to activate the correct emulator window.
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

Re: ImageSearch in a loop

19 Aug 2017, 04:47

I think that nox player is messing ahk somehow because even those tooltips apears a 200-300 px away
I've played a little with the 3rd image and the tooltips for entering and exit that if statement keeps moving left - right. and the coordinates for these tooltips are just plain numbers. no variable used or anything. so they should stay in the same position no matter what.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: ImageSearch in a loop

19 Aug 2017, 08:41

Tooltip actually is subject to its own CoordMode, so that could be a problem in interpreting results.

But maybe there is some interference. Perhaps someone with Nox Player can comment if they experience any issues. I'm at a loss otherwise. It might be useful to see exactly your updated code to make sure there hasn't been any unaccounted for changes that might be an issue.
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

Re: ImageSearch in a loop

19 Aug 2017, 11:23

well my code right looks like this

Code: Select all

F4::
SetDefaultMouseSpeed 30
CoordMode, Pixel,Screen
loop {
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%,*20 c:\1.bmp
	If (ErrorLevel == 0){
		Tooltip Entering PLAY, 100, 100
		Click, %foundX%, %foundY%
		ToolTip, PLAY, 100, 150
	}
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%,*100  c:\2.bmp
	If (ErrorLevel == 0){
		ToolTip, %foundX%  %foundY%, 100, 300
		Click, %foundX%, %foundY%
		Click, %foundX%, %foundY%   ;<==trying to force the click but no luck
		ToolTip, %foundX%  %foundY%, 100, 150
		;break
	}
	else
	{
		ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%,*100 c:\3.bmp ;<--in case you wonder why *100. well while scrolling the image tend to look a bit different. probably dpi to pixel conversion from nox/android/whatever
		If (ErrorLevel == 0){
			BlockInput, On
			BlockInput, MouseMove
			ToolTip click3 enter, 100,150 ;<---these tooltips moves left <-> right on the screen. im guessing that it moves the same distance as the mouse moves from its designated position
			
			;Click, % foundX-40, %foundY%,down
			;MouseMove, % foundX-40, % foundY-50, 80 
			;Click left up
			
			;one more try with mouseclickdrag. still no luck
			MouseMove, % foundX-40, %foundY%, 80 
			MouseClickDrag, left, % foundX-40, %foundY%, % foundX-100, %foundY%, 50
			BlockInput, MouseMoveOff
			BlockInput, Off
			ToolTip, click3 exit, 100, 180 ;<--see above. the y seems to be ok. this tooltip is just a bit lower than the previous one even if tooltips move. just x axis is moving  left<->right
		}
	}
	Sleep, 2000
}
return

F3::
ExitApp
return
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

Re: ImageSearch in a loop

19 Aug 2017, 12:28

well problem solved.
Sikuli much simpler and actually it works. lol
i kind of hate python but oh well, it works. i can manage python :)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: ImageSearch in a loop  Topic is solved

19 Aug 2017, 13:12

I Still didn't see CoordMode, Mouse, Screen in your original updated code. You need at least two instances of the CoordMode; one for Mouse and one for Pixel. And I wouldn't put it past the Tooltip shifting left and right because of a different active window, because you didn't change the default Tooltip for CoordMode mode, so it acts like CoordMode, Tooltip, Window. At the very least, your tooltips should've been showing where the clicks would be because they are both in Window mode because you didn't use CoordMode on them. And you can't click through a Tooltip in my testing, so, ironically, your Tooltip prevented the clicks from having any useful effect.
loganjahk
Posts: 8
Joined: 31 Oct 2015, 20:21

Re: ImageSearch in a loop

19 Aug 2017, 15:43

good point. I've added that but probably i did one to many undo while playing with the script.
As for the tooltip i didn't add it at all.
Everything seems to stay together now. but the image recognition is extremely bad. even with *100 is still can't find the image. i'll try to rise the number but i might end up with wrong positions.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 172 guests