Random Image Search and Click

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Centurion
Posts: 4
Joined: 01 Apr 2021, 23:06

Random Image Search and Click

02 Apr 2021, 00:44

Hi all, I am fairly new to scripting but have learned some basics through trial and error and reading topics on this site and archived posts via google searches and can usually get by alright. However, this one finally has me stumped. So, here goes:

The web page I need to be on generates the same image in random quantities but at a set time interval after being clicked. For instance you open the page and a random quantity like 3 images will pop up. You click one and then a second later, 5 images will pop up. Click one of those and then a second later 2 images will pop up and so on. Always the same exact images but usually in different positions on the page and different quantities.

How this normally works is you click on them with a mouse or tap the screen if you have a phone or tablet which is all well and good. But my problem is the only device I have to use at the moment is an old laptop with a touch pad and it is incredibly difficult to get the same speed and effectiveness as the other options. It is just too clumsy and burdensome to use for something like this.

So what I need to accomplish is a script that I can activate with a keyboard key that searches the page randomly (up, down, left, right, etc in different spots each time) for one image and then clicks it. Ideally it would work so that I strike the letter "B" or "N" or space bar, etc and could hit it again and again picking a different image in a random spot each time by using image search.

I was able to get as far as using image search and it finding the image and clicking on it once, but the location isn't random and I can't get it to repeat by clicking again. It always picks the first image on the screen every time which doesn't work because if the mouse cursor sits on it and the image lands in the same spot again after being clicked, the mouse cursor obstructs the image view and searching it won't detect the image or move on to find the next one. So every search pretty much has to locate the image in a different spot on the page when they move in order to avoid that. I also need to be able to keep repeating it by clicking the same keyboard key again if at all possible.

I hope this isn't too complicated or too long of a post to understand. Any help is very much appreciated on this
User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: Random Image Search and Click

02 Apr 2021, 05:52

If you search the forum for "multiple images" or "all images", you will find examples. You could use Random to set random coordinates to search. An alternative is that you could find all of the images, and then select one of them randomly.

If necessary, you could use MouseGetPos and MouseMove to change and restore the cursor's position.
Centurion
Posts: 4
Joined: 01 Apr 2021, 23:06

Re: Random Image Search and Click

02 Apr 2021, 07:58

Thanks for the reply. I will try those search terms now and see what I can come up with

Also, I wanted to add a pic and try to explain a little better what I'm trying to do. Please excuse the terrible paint upload as it's all I could do on short notice lol

Image

The background board looks similar to the numbered squares and never changes. The red X's in the pic all represent the exact same image and their position changes immediately when one is successfully clicked on. Then they reposition and wait to be clicked on again.

This is what I had initially tried:

Code: Select all

b::
SetDefaultMouseSpeed 0
loop 
{
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, C:\Users\imagefile.PNG
If(ErrorLevel == 0){
Click, %foundX%, %foundY%
}
}
Esc::ExitApp
return
[Mod edit: [code][/code] tags added.]


There are 2 problems with using that script. The first problem is it isn't random and will always choose the image closest to the top and left side. (box 2 in the pic) If that happens several times, the images tend to reposition themselves fast enough that one will wind up in the same spot as the previous click, however now the mouse cursor will be on the image and it won't successfully search using the script. It just stalls out there and stops. I have read other posts about changing the x and y coordinates to randomize it after but I could not figure out how to make it work in practice after hours of unsuccessful attempts. Basically what I need is something that will search out the images in the other boxes and not just the closest one.

The second problem is the speed that it clicks on each image is and has to be pretty fast. So using that script above the only real way to stop it is with Esc because the mouse cursor stays on the page clicking if the script is working properly. So ideally, that is why I would want to make a script that isn't a loop and trigger fires the random search so to speak. For instance if I were to hold or strike the "b" button repeatedly it would search and click. Then when "b" button is not being used, the script would pause I guess. Or maybe needs to be toggled on and off. I am still a very fresh beginner to scripts so not exactly sure the proper terms. Hopefully this helps explain it a bit better.
User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: Random Image Search and Click

02 Apr 2021, 09:41

OK. You could probably find all images and select one at random. Others have already written functions to find all matching images. As I mentioned, if the mouse pointer is an issue, then you can simply move it out of the way.
User avatar
boiler
Posts: 16952
Joined: 21 Dec 2014, 02:44

Re: Random Image Search and Click

02 Apr 2021, 10:05

I had been posting the function below that finds all images, and it seemed to be working for people, although lately I've been questioning whether it misses certain cases. I haven't spent the time to verify and fix it if so yet. But you can try it. Even if it does work as it should, a faster approach would be to use Gdip_ImageSearch, and speed sounds to be an important factor in your case.

Code: Select all

ImageSearchAll(imageFile, x1 := 0, y1 := 0, x2 := "Screen", y2 := "Screen", var := 0)
{
	; found coordinates are returned as a simple array of coordinate pairs
	; each coordinate pair is an associative array with keys "x" and "y"

	x2 := x2 = "Screen" ? A_ScreenWidth : x2
	y2 := y2 = "Screen" ? A_ScreenHeight : y2
	found := []
	y := y1
	loop {
		x := x1
		loop {
			ImageSearch, foundX, foundY, x, y, x2, y2, % "*" var " " imageFile
			if (ErrorLevel = 2)
				return -1
			if !ErrorLevel {
				found.Push({x: foundX, y: foundY})
				x := foundX + 1
				lastFoundY := foundY
			}
		} until ErrorLevel
		Y := lastFoundY + 1
	} until (x = x1) && ErrorLevel
	return found
}
Centurion
Posts: 4
Joined: 01 Apr 2021, 23:06

Re: Random Image Search and Click

02 Apr 2021, 18:10

Thanks again for the quick replies. I will play around with the find all images next and see what I can come up with. Earlier I had searched through some other threads and found what I thought would work by just doing multiple image searches and a simple click or mouse move. I made another script for it and thought it was working for a little while but eventually it stalls out and when the second image search hits, it goes back to search the same image at the start of the page instead of moving onto the second or third image how I need it to.

I'm sure I probably didn't set it up right and that's why. It might not have even worked for the short amount of time I thought it was working, because of the nature of how the images reposition. It's hard to verify without slowing the script down and defeating it's purpose. That makes it even more frustrating because I have to keep testing it again and again until it either fails or works, but I will keep trying.
Centurion
Posts: 4
Joined: 01 Apr 2021, 23:06

Re: Random Image Search and Click

22 Apr 2021, 03:31

Alright, I gave up on this for awhile and have came back to it now. Still without great results. I think this will be best to work it out in steps and piece it together after. This is the first part of what I need and this works fine currently as is:

Code: Select all

b::
SetDefaultMouseSpeed 0

ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, C:\Users\imagefile.PNG
If(ErrorLevel == 0){
MouseMove, %foundX%, %foundY%
}
Esc::ExitApp
return
what I need to do next is start another image search for the same image but using x and y coordinates further down the screen than where the mouse cursor moved to in the first search. I've tried several different ways and can't figure out how to make that work. If I start another image search, it wants to go to the first image on the screen and I need it to move to the 2nd one or 3rd one or 4th one etc after where the mouse cursor is sitting in the first search. Please help because I'm totally lost here
User avatar
boiler
Posts: 16952
Joined: 21 Dec 2014, 02:44

Re: Random Image Search and Click

22 Apr 2021, 07:14

You probably just didn’t access the results of the function I provided correctly. I’ll help you work with the function, but I’m not going to essentially rewrite the function in a step-by-step back-and-forth series of posts. If you want to do that, I’ll describe the next step so you can see how to do it and all the following steps.

You now have to use foundY + 1 as the starting y position of your next search rectangle:

Code: Select all

ImageSearch, foundX2, foundY2, 0, foundY + 1, A_ScreenWidth, A_ScreenHeight, C:\Users\imagefile.PNG
If there is a possibility that the second image could be at the exact same y position on the screen as the first but further to the right, you would want to search for that possibility first by searching with this rectangle:

Code: Select all

ImageSearch, foundX2, foundY2, foundX + 1, foundY, A_ScreenWidth, A_ScreenHeight, C:\Users\imagefile.PNG
This is where the tricky part comes in: In the results of the search to the right but at the same y position as the prior found image, you have to ignore results if foundY2 does not equal foundY, or else they’ll be also found in subsequent searches, so you’ll find the same image more than once and possibly also mess up the order of found images since this one could be lower on the screen than the next found image. Also, if you do find an image at that same y position, then you should keep searching for more such possibilities until none exist before moving on to search at lower y positions.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], OrangeCat and 149 guests