FindClick() - ImageSearch, Clicking, & More [Newest Version]

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

31 Mar 2019, 19:02

Hi Oblituarius, happy to help. I just tried using the directional option (e.g. "bottom") and was dismayed to find it didn't seem to be working. There's a bunch of options in here that I don't really use myself, and unfortunately sometimes when I tweak something sometimes it screws up something elsewhere. I'm guessing that's what happened here. I'll take a look and let you know once I figure it out.
gverrilla1
Posts: 6
Joined: 26 Mar 2019, 23:31

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

02 Apr 2019, 13:52

Hey guys, I don't know why this isnt working, can you please help?

Code: Select all

^!m::  ; Control+Alt+M hotkey.
Loop
{
FindClick("F:\tidle\worker.png", "n2 e o20")


Sleep, 60000
}
I'll run the script, open the target-window, and press ctrl+alt+m.
There are 6 instances of this image on my screen, but it's only finding 2 of them.
Second problem is that after finding them, the click doesnt work. I have used "dx" on options to see if it was clicking the right place, and it was. But the clicking doesnt work.
What can I do? Tried n3 aswell. And Sleep20 and Sleep50.
Dx doesn't shoot any error btw.
geniishii
Posts: 1
Joined: 04 Jun 2019, 09:08

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

04 Jun 2019, 09:17

Hi! Amazing work you've done here.

Im struggling a bit with what im trying to do. So i have a small image 22x18. Im trying to find how many times it appears ( i can expect 0-6 appearances in the screen). However the only way to see the image is to be hovering over the item's tooltip and since the mouse moves while im using the FindClick (), then it'll never detect it. Is there any way to use the function and allow the mouse to stay static while the function searches over the image?

; Moving mouse to tooltip
MouseMove, 10, 25, 0, R
;Initializing Id var
IdCount = 0
;Searching for img in tool tip
IdCount := FindClick(A_ScriptDir . "\dmgtop1.png", "Count o20")
;Results
MsgBox, %IdCount%
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

05 Jun 2019, 19:35

Yeah, you can use the "n" option to not click on anything. The mouse will not move either in that case.
carno
Posts: 266
Joined: 20 Jun 2014, 16:48

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

09 Jun 2019, 15:41

This script is in the top 5 of the best AHK has ever offered the community. :bravo:
godofOOF
Posts: 27
Joined: 22 Dec 2018, 06:03

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

21 Jun 2019, 15:50

This script seems to not work for me
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

26 Jun 2019, 21:25

Thanks for the kind words carno! :)

Could you provide me with more details, godofOOF?
arazu
Posts: 5
Joined: 05 Sep 2019, 05:12

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

05 Sep 2019, 05:21

o/

**found a fix

Code: Select all

FindClick("img/1.png", "dx o65 r" )

r := "" targetwindow "" ,                r_user := "" targetwindow ""
thanks :bravo:

the findclick is great and powerful
i got a small issue with the "r" option

im trying to push r "" "" %targetwindow% "" ""

it wont let me use any % on the r title only direct name of window works.

can i bypass it?

"global targetwindow := "No window Picked" ;Window name"

"connected to edit box
Gui, Add, Edit, x100 y5 w100 h18 +0x200 vtargetwindow gsubmit_all, %targetwindow%"

"F2::
MouseGetPos,,,guideUnderCursor
WinGetTitle, Title, ahk_id %guideUnderCursor%
;Msgbox, %Title%
GuiControl,,targetwindow, %Title%
Gosub,submit_all
Return"

i want to be able to put a %% window title so user can keep pick window with simple f2


atm my target tester look like this

Code: Select all

#Include Core/FindClick.ahk
global targetwindow := "Mirror2" ;Window name


WinGetPos, X, Y, Width, Height, %targetwindow% 
X1 := X
Y1 := Y
W := Width
H := Height
X_end := (X1 + W) 
Y_end :=  (Y1 + H)
Loop
	
{
;MsgBox, %targetwindow%
;FindClick()
FindClick("img/1.png", "dx o65 r"  "Mirror2"  )
sleep, 5000
}

F8::ExitApp
Sargerasrex
Posts: 15
Joined: 27 Jan 2019, 12:50

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

28 Oct 2019, 09:36

Hey thank you for your amazing work.
Im trying to compile an autohotkey script using your code, but it doesnt appear to work. Probably due to my incompetence :D.

I'm right now going into "https raw.githubusercontent.com /berban/FindClick/master/FindClick.ahk" Broken Link for safety ) copy pasting the code into a new autohotkey script, and then compiling that script. However on clicking it only turns on the script but doesnt show me the ui i'm expecting to see.

My end goal is to create a script that will detect a certain predefined image on the top right area of my screen and then send a keystroke that I indicate in the script. Lets say there are three images :sick: , :oops: , and :crazy: . Now, when the program 'sees' :sick: appear i want it to send "q", when it sees :oops: i want it to send "w". ETC.

Here's what knowledge i currently possess
I think i will have to save the predetermined image into a folder for my script to reference. But i dont know the script involved.
I will have to create a send script, which i absolutely know how to make. I just need the IF portion of the syntax.

If <script sees :oops: >
Send, {w}
Sleep 100
Return

Something like that.
Hope you can help me! Thank you and god bless.
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

02 Nov 2019, 21:00

Sargerasrex wrote:
28 Oct 2019, 09:36
... My end goal is to create a script that will detect a certain predefined image on the top right area of my screen and then send a keystroke that I indicate in the script. ...
You don't need to compile anything.

1. Find the Lib folder on your computer, which is inside the Autohotkey folder where Autohotkey.exe resides.
2. Place the file FindClick.ahk in the Lib folder. Now any script you run will be able to use the function from FindClick. (Alternatively, you could copy-paste the complete contents of FindClick.ahk into your new script that will be using it.)
3. Create a new script anywhere you like, containing just FindClick() (or run that line in some other script: it doesn't matter).
4. Run the script. Now you will get the FindClick interface, and you will be able to pick an area on the screen and save it as an image from the interface.
5. Make sure to check "Copy code to clipboard"; and add n0 to the options ("click 0 times on the image"), otherwise the mouse will click on the image whenever it is found.
6. After clicking Save, you will get code on your clipboard, like FindClick("D:\Programs\AutoHotkey\Graphics\NewFile", "n0"). You can use that code in a script to have the script look for the image. You can use it like this:

Code: Select all

Image1Found := FindClick("D:\Programs\AutoHotkey\Graphics\NewFile", "n0")
If (Image1Found)
{
	SoundBeep, 300, 100
}
When you execute this code, it will search for the image, and, if the image is found, it will store the coördinates of its location on the screen in the variable Image1Found. If the image is not found, the variable will be made empty.
Then the If checks whether there is anything inside the variable; if so, it will produce a sound beep.

You may want to "watch" for an image to appear. You could use the Watch option in FindClick, but I don't know how that works. It may be safer to use the normal method, a timer that executes the code above, every e.g. 1000 milliseconds (lower this period to make the script respond faster):

Code: Select all

SetTimer, SearchForImages, 1000

SearchForImages(){
	static SearchIsBusy
	If ( SearchIsBusy = True)
	{
		Return
	}
	SearchIsBusy := True
	
	Image1Found := FindClick("D:\Programs\AutoHotkey\Graphics\Image1", "n0")
	If (Image1Found)
	{
		SoundBeep, 300, 100
	}
	
	Image2Found := FindClick("D:\Programs\AutoHotkey\Graphics\Image2", "n0")
	If (Image2Found)
	{
		SoundBeep, 600, 100
	}
	
	SearchIsBusy := False
}
I've also added a check SearchIsBusy, to make sure the search isn't triggered again while it is still busy; this could happen if you set the period of the timer to a very low number of milliseconds.

Note: of course the code above won't work on your computer, until you have generated your own FindClick(... and put it in there.

Good luck! Do let me know if you still can't get it to work.

P.S. Chapeau for including your end goal in your question; many people forget this, but it makes it much easier to help.

P.P.S. You used Send {w}, but normal letters don't need to be included in braces with Send. Though I don't think it hurts either.
Oblituarius
Posts: 13
Joined: 31 Mar 2019, 16:57

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

03 Nov 2019, 18:10

berban wrote:
31 Mar 2019, 19:02
Hi Oblituarius, happy to help. I just tried using the directional option (e.g. "bottom") and was dismayed to find it didn't seem to be working. There's a bunch of options in here that I don't really use myself, and unfortunately sometimes when I tweak something sometimes it screws up something elsewhere. I'm guessing that's what happened here. I'll take a look and let you know once I figure it out.
Sounds good to me! I'll keep an eye on the forums for any updates if they end up appearing.
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

04 Nov 2019, 21:23

Thanks for answering the question Cerberus, you're the man!! I haven't been able to get around to any AHK-related stuff lately :cry: But you explained it better than I'd be able to, to be honest.

And Oblituarius, yeah my bad again that's on my to-do list but I can't give you a real ETA unfortunately. But if you need that feature you can try following the example on page 19 of the documentation, "Choosing Among Multiple Results". You can easily modify to click on the last result instead of the 4th from top.

Code: Select all

; The below code stores the coordinate pairs in %Results%
; "n" tells FindClick to not click on the images because you just want to know where they are
; "fy,x" tells FindClick to format the results with the y-coordinate first, then a comma, and then the x-coordinate. This will allow sorting by the y-coordinate
Results := FindClick("image", "e n fy,x")
Sort, Results, N R                  ; Will sort the results based on the y-coordinate. N is numerical sort. R is reverse so the last item appears first
Loop, Parse, Results, `n            ; Parse the list one coordinate pair at a time
{                                   ; Stop at the 1st item (ie the last, cause reverse sort was used)
	StringSplit, Coords, A_LoopField, `,              ; Split by comma
	MsgBox, The coordinates are (%Coords2%,%Coords1%) ; Reverse the order so it becomes x,y again
	Break
}
sorry about all the dated pseudoarrays haha
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

05 Nov 2019, 20:52

@berban I have been using FindClick for ages; it is really one of the best functions on the forum. The diagnostic mode, the offset, all the options, everything.

I may want to create a version of it where you can choose for the search area to be positioned relative to a corner of a window, so that you can e.g. easily pick an button that is always in a certain area that is a number of pixels off the bottom of the window and a number of pixels from the right side—without having to find and type any coördinates by hand. But that is probably a long-term project.
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

06 Nov 2019, 07:32

Thanks for the kind words!!
Cerberus wrote:
05 Nov 2019, 20:52
I may want to create a version of it where you can choose for the search area to be positioned relative to a corner of a window, so that you can e.g. easily pick an button that is always in a certain area that is a number of pixels off the bottom of the window and a number of pixels from the right side...
Maybe I'm misunderstanding but I think that's already available. For the a option (search area), if you use negative numbers it'll be interpreted as an offset from that edge of the original search area (either the screen, or the window if r is used.) For instance, if you use a-400,-300,-200,-100 r then it will give you the search area shown here:

Image

If you wanted to go all the way to the edge of the window/screen, just omit the width and/or height, e.g. a-400,-300

I know for sure this works because I use this all the time :)
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

06 Nov 2019, 19:54

@berban Ah, yes, I didn't explain it properly. What I'd like to do is make it so that you can click a button "pick area", then you draw a rectangle on the screen, then you click buttons to pick between offset from the left (default) or right, and from the top (default) or bottom. I'd make it so that those buttons just add the coördinates and possible minuses to the a option. I have used the offset in the past, and I was very happy with it.
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

09 Nov 2019, 07:35

Oh I see what you mean. Yeah I have a separate hotkey to get coordinates on a window or screen. I use that for findclick but also for other ahk-related stuff
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

09 Nov 2019, 10:20

Yeah, I have a hotkey that tells me this (not all of it is still working, but at least the offsets should still work):

Code: Select all

526 = Last Right Click x, 
1189 = Last Right Click y, 
 = Master Volume, 
0xF4F4F4 = COlour, 
 = px2, 
 = ClickplaceX, 
 = ClickplaceY, 
 = GuiWidth, 
1096 = WindowWidth, 
1814 = WindowHeight, 
570 = click offset from right edge, 
625 = click offset from bottom, 
-8 = upper left window corner x, 
114 = upper left window corner y.
I just want to make the area and offset shown as a rectangle and integrate it into FindClick, someday. There is always so much to code!

Another thing that could be interesting is handling multiple monitors.
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

09 Nov 2019, 10:30

P.S. I think I have found a small bug in the code. On line 1299, it says this:

Code: Select all

CopyToClipboard = %False%                ; True to have this checkbox cleared by default
But, in fact, when this is set to False, the check box is empty, at least for me. So I have changed that line to this:

Code: Select all

CopyToClipboard = %True%                 ; True to have this option checked by default
I'm also curious as to in what scenario someone would not want to save the code to clipboard: how could one use FindClick without getting the code?
User avatar
berban
Posts: 99
Joined: 14 Apr 2014, 03:20

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

09 Nov 2019, 19:52

Oh yeah that’s supposed to be a user setting section of the code. You put true/false depending on if you want the box to be checked by default, or not.

Personally I don’t usually use that feature much, I mostly use it to create small images only which I then save to my default dir for use in whatever script I’m writing. If you’re using it most of the time, then I can definitely see why a feature to get relative coords right from the image creator GUI would be helpful, so you could have the final snippet of code all set to go without any extra steps.
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: FindClick() - ImageSearch, Clicking, & More [Newest Version]

10 Nov 2019, 17:47

I've changed "cleared" into "checked" in the comment, because I believe it did the opposite of what it said. When set to True, the box is checked (not cleared) by default.

Hmm so you're saying you don't actually use FindClick's main function much, as you only use it to save small images, not actually use the code FindClick produced? That is surprising to hear from its creator!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: wilkster and 34 guests