Image key press help

Get help with using AutoHotkey and its commands and hotkeys
metherliince
Posts: 2
Joined: 16 Feb 2019, 23:34

Image key press help

18 Feb 2019, 11:08

I have this script which involves ahk finding the image file, and then pressing a key. Currently, once it finds the key, it will press the key, but wont stop after the image is gone. I know this might have something to do with Errrorlevel 1 (when the image is not found) but i have no clue how to make something to stop the key from sending without stopping the whole script. It's supposed to be automatic, so i don't want it to just reload, or restart.

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance Force
CoordMode, Mouse, Window
CoordMode, Pixel, Window
Menu, Tray, Icon, Shell32.dll, 95 , 1


;--------------------------------------------------
;Change the following variables to suit your needs:
;--------------------------------------------------


;Top left corner of game relative to the active window
X1 := 685
Y1 := 692


;Bottom right corner of game relative to the active window
X2 := 1169
Y2 := 926


;For imageFile, specify the path to the image, do not use "Snipping Tool" as this adds compression
;Use PrintScreen and paste the image into mspaint.exe, then crop and save to the desktop as myButton.png
;The smaller the image, the faster the search


imageFile := A_Desktop "\A.png"


;Set the Window Title to activate the window when search starts (optional, leave blank if not)
windowTitle =


;Set the search interval in milisecnds
searchInterval := 1000

offset := 10


;F1 to Toggle
F1::
If !FileExist(imageFile)
{
MsgBox, 0, ERROR, Could not locate image file at %imageFile%`n`nShutting down Script
ExitApp
}

SetTimer, imgSearch, % (Toggle := !Toggle) ? searchInterval : "Off"
Menu, Tray, Icon, Shell32.dll, % Toggle ? 94 : 95 , 1
SplashImage,, w100 x0 y0 b fs12, % Toggle ? "Running" : "Stopped"
Return


imgSearch:

If (windowTitle)
WinActivate, % windowTitle


ImageSearch, foundX, foundY, % X1, % Y1, % X2, % Y2, % imageFile


If !ErrorLevel

{

If State=70
State=Off
else
State=70
SetTimer SendKey, %State%
Return

SendKey:
Send a
Return



}


Return

I've been doing this for like a day and a half so if anyone can do something which involves with "if ErrorLevel 1" and something that can stop the key spam.
Also I've been using autohotkey for a day and half.
trust_me
Posts: 85
Joined: 29 Jul 2017, 10:46

Re: Image key press help

19 Feb 2019, 06:51

Hope that i restored to original code because i changed it a bit for testing with a picture in a window.

Code: Select all

#NoEnv
SendMode Input 
SetWorkingDir %A_ScriptDir%
#SingleInstance Force
CoordMode, Mouse, Window
CoordMode, Pixel, Window
Menu, Tray, Icon, Shell32.dll, 95 , 1



;Gui, add,picture,,haystack.png
;Gui, show ,,test

;wingetpos,xx,yy,ww,hh,test

;--------------------------------------------------
;Change the following variables to suit your needs:
;--------------------------------------------------


;Top left corner of game relative to the active window
X1 := 685
Y1 := 692


;Bottom right corner of game relative to the active window
X2 := 1169
Y2 := 926



;For imageFile, specify the path to the image, do not use "Snipping Tool" as this adds compression
;Use PrintScreen and paste the image into mspaint.exe, then crop and save to the desktop as myButton.png
;The smaller the image, the faster the search


imageFile := A_Desktop "\A.png"


;Set the Window Title to activate the window when search starts (optional, leave blank if not)
windowTitle = test


;Set the search interval in milisecnds
searchInterval := 1000

offset := 10
status:=70

;F1 to Toggle
F1::

If !FileExist(imageFile)
{
MsgBox, 0, ERROR, Could not locate image file at %imageFile%`n`nShutting down Script
ExitApp
}

SetTimer, imgSearch, % (Toggle := !Toggle) ? searchInterval : "Off"
Menu, Tray, Icon, Shell32.dll, % Toggle ? 94 : 95 , 1
SplashImage,, w100 x0 y0 b fs12, % Toggle ? "Running" : "Stopped"

Return


imgSearch:

If (windowTitle)
WinActivate, % windowTitle


ImageSearch, foundX, foundY, % X1, % Y1, % X2, % Y2, % imageFile
If !ErrorLevel
{
  SetTimer SendKey, % status
}
else
{
  SetTimer SendKey, off
}
Return


SendKey:
if !toggle
{
SetTimer, sendkey,off 
return
}
send a
Return


Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Dlxgp7, DooMWhite, Google [Bot], JaydeNpie, keanpteamos, LM_, Raymond888, RubbeH, slupka, Smile_, TAC109, yhk and 53 guests