Keypress when image is found

Ask gaming related questions (AHK v1.1 and older)
daffy82
Posts: 14
Joined: 03 Aug 2016, 00:42

Keypress when image is found

04 Aug 2016, 00:25

Im trying to make a script that will press '5' when image is found on a specific location on the screen.

Actually I am thinking of making 30 images for the script to look for. When my health is on 30 or below the script should press '5'

The pictures would look like this; http://imgur.com/a/AnNUJ (this is at 67% health - i just found it on google but I wanted to show you how the images would look like). In this example lets call the image '67.png'

Im very novice and cant code much... This is what ive came up with:

Code: Select all

image_name67=67.png
image_name30=30.png
image_name29=29.png

ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %image_name67%
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %image_name30%
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %image_name29%

if ErrorLevel = 2
tooltip Could not conduct the search.
else if ErrorLevel = 1
tooltip Image could not be found on the screen.
else
{
press '5'

}
return
Can someone help me with the code :wtf:
User avatar
SnowFlake
Posts: 368
Joined: 28 Apr 2015, 05:41
Contact:

Re: Keypress when image is found

04 Aug 2016, 06:14

Hey

Try this, Press X to search for the images also make sure that the mages are in the same place/folder as the script or chnage the path to the pictures.
like this:
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, "C:/games/67.png"

Code: Select all

IF NOT A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}

CoordMode, Pixel, Client
#SingleInstance Force

 X::
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 67.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
tooltip 67.png Image could not be found on the screen.
if ErrorLevel = 2
tooltip Could not conduct the search.
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 30.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
tooltip 30.png Image could not be found on the screen.
if ErrorLevel = 2
tooltip Could not conduct the search.
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 29.png
 If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
tooltip 29.png  Image could not be found on the screen.
if ErrorLevel = 2
tooltip Could not conduct the search.
return
:yawn:
daffy82
Posts: 14
Joined: 03 Aug 2016, 00:42

Re: Keypress when image is found

04 Aug 2016, 08:46

SnowFlake wrote:Hey

Try this, Press X to search for the images also make sure that the mages are in the same place/folder as the script or chnage the path to the pictures.
like this:
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, "C:/games/67.png"

Code: Select all

IF NOT A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}

CoordMode, Pixel, Client
#SingleInstance Force

 X::
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 67.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
tooltip 67.png Image could not be found on the screen.
if ErrorLevel = 2
tooltip Could not conduct the search.
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 30.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
tooltip 30.png Image could not be found on the screen.
if ErrorLevel = 2
tooltip Could not conduct the search.
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, 29.png
 If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
tooltip 29.png  Image could not be found on the screen.
if ErrorLevel = 2
tooltip Could not conduct the search.
return
Thank you!

I will test this. If succesful I will begin cutting all 30 images and extend the script to look for them all.

One question:
My plan was to have the script run in the background all the time. So I woulnt have to press 'X'. Basically the script should monitor a little ractangle on the screen - lets say Xpos: 129 Ypos: 960 and Xpos 220 Ypos 990 (these are not the exact coordinates but for this example it will do.)
Please have a look for a better understanding: http://imgur.com/a/dz28V (bottom left corner - the black rectangle)

Edit: Perhaps it would be fine if the script monitors the whole screen? I dunno whats best. But how do I get it running infinitely without any interaction from me?

Best regards
p3trus
Posts: 137
Joined: 02 Aug 2016, 03:20

Re: Keypress when image is found

04 Aug 2016, 09:51

take a look at https://autohotkey.com/boards/viewtopic ... 18&t=21016 - in this case (monospaced digits), a custom "OCR" function which checks a few pixels per digit is far more efficient (faster in detection, and way less work than making 30 screenshoots, cropping them, ..)
User avatar
SnowFlake
Posts: 368
Joined: 28 Apr 2015, 05:41
Contact:

Re: Keypress when image is found

04 Aug 2016, 09:54

It is faster to search with in a area then search the whole screen also you might also be able to use pixelsearch or memory reading to get the health you are looking for.

Here are a few more examples:

Code: Select all

IF NOT A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}

toggle= 0
toggle2= 0
image = 0
image2 = 0
#MaxThreadsPerHotkey 2
CoordMode, Pixel, Client
#SingleInstance Force
 
Z:: ; Loop imagesearch all images from 1 to 1000.
SoundBeep
Toggle := !Toggle
While Toggle{
image++

IF (image = 1000)
{
MsgBox all images have been searched. reset images
image = 0
break
}	

ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %image%.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
ToolTip %image%.png Image could not be found on the screen.
if ErrorLevel = 2
MsgBox  Error %image%.png
}
return

 X:: ; Loop specific images using a function.
SoundBeep
Toggle2 := !Toggle2
While Toggle2{
Search_HP("30.png")
sleep 100
Search_HP("29.png")
Sleep, 100
Search_HP("67.png")
Sleep, 100
}
return

C:: ; Everytime you press C a new picture will be searched.
SoundBeep
image2++	
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
ToolTip %image2%.png Image could not be found on the screen.
if ErrorLevel = 2
MsgBox  Error %image2%.png
return

Search_HP(image){
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %image%.png
If ErrorLevel = 0
send, {5}
if ErrorLevel = 1
ToolTip %image%.png Image could not be found on the screen.
if ErrorLevel = 2
MsgBox  Error %image%.png
}
return
:yawn:

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: mexican scientist and 71 guests