Image search + sound

Ask gaming related questions (AHK v1.1 and older)
ivar85
Posts: 5
Joined: 11 Feb 2020, 12:21

Image search + sound

03 Aug 2021, 20:12

Hi guys!

Could you help me with a small script that will search for a image and play a sound if it's found?


Start button (any button)
Search for a image (could be two or three images)
If found
Play sound file
Loop sound
When image disappear then it stops playing that sound.

Hope someone can help.
Thanks!
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Image search + sound

03 Aug 2021, 21:08

Code: Select all

image = ....png
sound = ....wav
F3::
WinGetPos,,, width, height, A
Loop {
 Sleep, 200
 ImageSearch, x,, 0, 0, width, height, %image%
 SoundPlay, % ErrorLevel ? "" : sound, Wait
} Until (x = "")
Return
ivar85
Posts: 5
Joined: 11 Feb 2020, 12:21

Re: Image search + sound

04 Aug 2021, 04:04

Thank you !
Does AHK work that way I can just have any image in the AHK folder that ends in with a .png and this scripts search for it ?

Right now this script cant find any test image I make.
Could you help me a little more here ?

Code: Select all

image = ....png
sound = ....wav
F3::
WinGetPos,,, width, height, A
Loop {
 Sleep, 200
 ImageSearch, x,, 0, 0, width, height, %image%
 SoundPlay, % ErrorLevel ? "" : sound, Wait
} Until (x = "")
Return
[Mod edit: [code][/code] tags added.]

This is how my folder looks right now:
image.png
(27.82 KiB) Downloaded 108 times
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Image search + sound

04 Aug 2021, 05:45

You can loop through various images, but start with just one, so that you can get the script working. Change the first two lines: provide a full path to an image & sound file. The script searches with coordinates relative to the active window (can be changed), so ensure that the target window is active before you trigger the hotkey.
ivar85
Posts: 5
Joined: 11 Feb 2020, 12:21

Re: Image search + sound

04 Aug 2021, 14:25

That works thank you.

I get it working when I am browsing in chrome... It finds the image and makes the sound.

But I am trying to do it from a computer game and it seems that it´s not finding any pictures there.
Do you know why that could be ?
ivar85
Posts: 5
Joined: 11 Feb 2020, 12:21

Re: Image search + sound

04 Aug 2021, 14:52

Should I add *50 in it if the shades are not exactly the same ?

Where would I add it ?

edit***
This actually works !
So I have another question.
When the program finds the image... It start´s playing the sound.
So far so good.
Then when the picture disappears it´s like the script does not reload or something ?
I have to manually press F3 again to turn it on.
Is it possible to keep going without my input to refresh it ?
And also.. Would it be possible to search the whole screen even if the window is not active ?
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Image search + sound

04 Aug 2021, 18:51

You can search the screen if you change the second coordinate to A_ScreenWidth, A_ScreenHeight and change the CoordMode to screen. You can enclose any routine inside a Loop to execute it repeatedly.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 64 guests