Page 1 of 1

clicking a moving image only after the distance greater than n pixels

Posted: 12 Mar 2018, 11:11
by AHKtreasureland
Hello, I'm new to AHK which i'm beating myself up for not knowing earlier. darn

basically I have an image and it is moving up and down. X axis is fixed, the movement is only alongside Y axis. 2 dimensional (no Z).

what i want to do is I'd like to have my mouse cursor click on it every 10 seconds, but only if the image had moved more than 100 pixels (My mouse better not click it when this thing had moved 99 pixels over 10 seconds: the subtract value of the Y-coordinates should be greater than one hundred).

i spent days, literally days to no avail.
a sample script would be so much appreciated.
any pointers as well.

well, thank you so much for your time and hope you have a great day.

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 08:25
by Nwb
Example script. This checks if the image is 100pixels away from the previous location of the image 10seconds ago if that is what you meant. If it's a static coordinate you want to be 100pixels away from then you will have to substitute the value of x1 and y1 with those coordinates.

Now also remember that X and Y coords retrieved from imagesearch are the top left pixel of the image. So if you want to click the image, you will have to use mouseclick rather than click and according to the picture's size make it so that the click falls on the image.

Code: Select all

F3:: ExitApp
F2:: SetTimer, click, Off
F1::
SetTimer, click, 10000
ImageSearch, x1, y1, 0, 0, A_ScreenWidth, A_ScreenHeight, Location\ImageFile.png
return

click:
ImageSearch, x2, y2, 0, 0, A_ScreenWidth, A_ScreenHeight, Location\ImageFile.png
If (y1-y2 > 100 or y2-y1 > 100) {
Click, %x2%, %y2%
y1 := y2
x1 := x2 ; don't think x1 := x2 is necessary but anyway 
}
else
{
; whatever to do if the image is not 100pixels away from previous location. For example if you wanted it to wait for it to go 100pixels away.
}
return

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 10:49
by AHKtreasureland
umhh.. appreciate your help but it doesn't work.

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 10:57
by Nwb
AHKtreasureland wrote:umhh.. appreciate your help but it doesn't work.
What isn't working? I haven't tested it either so I wouldn't know.

First try running this while the image is on screen and tell me what msgbox you get. Don't forget to change "Location" with the file location and Imagefile.png with the image file name and type.

Code: Select all

F1::
ImageSearch, x2, y2, 0, 0, A_ScreenWidth, A_ScreenHeight, Location\ImageFile.png
If ErrorLevel = 0
Msgbox Found
If ErrorLevel = 1
Msgbox Not found
Else
Msgbox Error
return

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 11:18
by AHKtreasureland
"save file" window pops up. It's a sign that ImageSearch is working fine, I guess..?

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 11:28
by Nwb
AHKtreasureland wrote:"save file" window pops up. It's a sign that ImageSearch is working fine, I guess..?
Huh? Save file? When you click F1? And this is an ahk window?


First take a screenshot of your screen and narrow down the image you want to search for, and save it. Open file explorer and copy the location of the image and replace the word "Location" in the script by pasting the location from file explorer. And replace Imagefile.png with your Imagename.filetype.

Run that script I had done an error fixed it. And click F1. Tell me what messagebox you see when you click F1.

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 12:02
by AHKtreasureland
ok, i kept getting "not found", and when i added this line, then i got "found".
CoordMode, Pixel, Screen

with this line added, i tried your script again ..but i get nothing, nada.. i so much appreciate you helping me like this by the way.

Re: clicking a moving image only after the distance greater than n pixels

Posted: 13 Mar 2018, 22:21
by Nwb
AHKtreasureland wrote:ok, i kept getting "not found", and when i added this line, then i got "found".
CoordMode, Pixel, Screen

with this line added, i tried your script again ..but i get nothing, nada.. i so much appreciate you helping me like this by the way.
Okay so we know imagesearch works. However I don't understand why adding Coordmode, Pixel, Screen should affect you finding an image or not. Because it only interprets the output coordinates. So I suggest you try it more. Because maybe your image changes from time to time or something that you found it once but didn't another time. From my understanding your image is completely static.

Can you tell me how big the image itself is?

What isn't working? The clicking part? Open menu by double left clicking the ahk icon in your system tray when the script is running and describe to me what is not happening. Is it clicking the wrong place? Then you have to adjust that by using mouseclick and adding to x and y according to your image dimensions/2.