Trying to detect an image seems to do nothing at all

Ask gaming related questions (AHK v1.1 and older)
Kaiji
Posts: 6
Joined: 14 Jan 2019, 16:01

Trying to detect an image seems to do nothing at all

14 Jan 2019, 17:51

I'm trying to disable the hotkeys in a script when the chat window of a game is on the screen. At first I ran it in a loop but I already have a loop in the script and found out you can't have more than one so I changed it to check every 100 milliseconds.

Not sure why, but it appears to do absolutely nothing. It doesn't suspend the hotkeys or create a message box. Hoping someone can help me with this.

Code: Select all

SetTimer, Checkchat, 100
Return

Checkchat:
ImageSearch, OutputVarX, OutputVarY, 1702, 940, 1911, 1068, C:/arrow.png
if ErrorLevel = 2
MsgBox, The search didn't work
if ErrorLevel = 1
MsgBox Image could not be found on the screen.
If ErrorLevel = 0
Suspend, on
Return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 18:02

is that the whole script? if so u need to add #Persistent, otherwise the script will exit once the auto-execute section has run. although, it wont be a very useful script
if not, then assuming the SetTimer is initialized correctly, by definition one of three things will happen, those being the two message boxes and Suspend

post ur whole script
Kaiji
Posts: 6
Joined: 14 Jan 2019, 16:01

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 18:09

K, I tried adding #Persistent to the start of the script but the SetTimer command still does nothing.

Here's the whole script (The commented out part is a failed attempt to detect a pixel color before I tried ImageSearch).

Code: Select all

#Persistent

SetTitleMatchMode, 3

GroupAdd, WinGroup, Heroes of Might and Magic III
GroupAdd, WinGroup, Heroes of Might and Magic III: Horn of the Abyss

#IfWinActive ahk_group WinGroup

; MOVE CURSOR TO CENTRE OF SCREEN WHEN ACTIVATING WINDOW
; -------------------------------------------------------------------

Loop
{
WinWaitActive, ahk_group WinGroup
{
MouseMove, 1033, 500
}
WinWaitNotActive, ahk_group WinGroup
}

; DISABLE CHAT WHEN ON CHAT SCREEN
;  -------------------------------------------------------------------

SetTimer, Checkchat, 100
Return

Checkchat:
ImageSearch, OutputVarX, OutputVarY, 1702, 940, 1911, 1068, C:/arrow.png
if ErrorLevel = 2
MsgBox, The program didn't work
if ErrorLevel = 1
MsgBox Icon could not be found on the screen.
If ErrorLevel = 0
;CoordMode, Pixel, Window
;PixelGetColor, thepixel, 1838, 34
;If thepixel = A69648
Suspend, on
Return

; ENABLE AND DISABLE SCRIPT WHEN CHATTING AND SAVING
; -------------------------------------------------------------------

^d::
SendInput, s
Suspend, on
Return

Tab::
SendInput, {Tab}
Suspend, on
Return

Enter::
Suspend, off
Sendinput, {Enter}
Return

NumpadEnter::
Suspend, off
Sendinput, {Enter}
Return

; MISC HOTKEYS
; -------------------------------------------------------------------

XButton1::Escape
MButton::Enter
q::h
e::t
f::e
z::d
`::q
^d::s
^c::l
x::z
X::w
C::i
r::u

; Auto attack in combat
+d::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 603, 974
MouseMove, OrigX, OrigY
Return
}


; 1-8 HERO HOTKEYS
; -------------------------------------------------------------------

1::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 343
MouseMove, OrigX, OrigY
Return
}

2::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 395
MouseMove, OrigX, OrigY
Return
}

3::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 450
MouseMove, OrigX, OrigY
Return
}

4::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 500
MouseMove, OrigX, OrigY
Return
}


5::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 550
MouseMove, OrigX, OrigY
Return
}

6::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 600
MouseMove, OrigX, OrigY
Return
}

7::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 650
MouseMove, OrigX, OrigY
Return
}

8::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 700
MouseMove, OrigX, OrigY
Return
}

; WASD MAP SCROLLING
; -------------------------------------------------------------------

~s & a::
MouseGetPos, OrigX, OrigY
While GetKeyState("s","P") && GetKeyState("a","P") 
{
MouseMove, 0, 1079
}
MouseMove, OrigX, OrigY

~a & s::
MouseGetPos, OrigX, OrigY
While GetKeyState("a","P") && GetKeyState("s","P")
{
MouseMove, 0, 1079
}
MouseMove, OrigX, OrigY

~a & w::
MouseGetPos, OrigX, OrigY
While GetKeyState("a","P") && GetKeyState("w","P") 
{
MouseMove, 0, 0
}
MouseMove, OrigX, OrigY

~w & a::
MouseGetPos, OrigX, OrigY
While GetKeyState("w","P") && GetKeyState("a","P")
{
MouseMove, 0, 0
}
MouseMove, OrigX, OrigY

a::
  While GetKeyState("a","P")
  {
    Send, ^{Left}
    Sleep, 50
  }

~w & d::
MouseGetPos, OrigX, OrigY
While GetKeyState("w","P") && GetKeyState("d","P") 
{
MouseMove, 1919, 0
}
MouseMove, OrigX, OrigY

w::
  While GetKeyState("w","P")
  {
    Send, ^{Up}
    Sleep, 50
  }

~d & w::
MouseGetPos, OrigX, OrigY
While GetKeyState("d","P") && GetKeyState("w","P") 
{
MouseMove, 1919, 0
}
MouseMove, OrigX, OrigY

s::
  While GetKeyState("s","P")
  {
    Send, ^{Down}
    Sleep, 50
  }

~d & s::
MouseGetPos, OrigX, OrigY
While GetKeyState("d","P") && GetKeyState("s","P")
{
MouseMove, 1919, 1079
}
MouseMove, OrigX, OrigY

~s & d::
MouseGetPos, OrigX, OrigY
While GetKeyState("s","P") && GetKeyState("d","P")
{
MouseMove, 1919, 1079
}
MouseMove, OrigX, OrigY

d::
  While GetKeyState("d","P")
  {
    Send, ^{Right}
    Sleep, 50
  }
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 18:14

Code: Select all

Loop
{
...
}

...

SetTimer, Checkchat, 100
sure enough. infinite loop -> code execution never gets to settimer
Kaiji
Posts: 6
Joined: 14 Jan 2019, 16:01

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 18:41

OK, thanks. I moved the Settimer command before the loop, and it makes it run but it doesn't detect the image when I'm on the game's chatscreen. It shows "The program didn't work" on the tooltip so I guess there's something wrong with the image. It's a simple 68x48 .PNG file which I put in the root of my C: drive to minimize the likehihood of this happening.

Updated script:

Code: Select all

#Persistent

SetTitleMatchMode, 3

GroupAdd, WinGroup, Heroes of Might and Magic III
GroupAdd, WinGroup, Heroes of Might and Magic III: Horn of the Abyss

#IfWinActive ahk_group WinGroup

; DISABLE CHAT WHEN ON CHAT SCREEN
;  -------------------------------------------------------------------

SetTimer, Checkchat, 100
Return

Checkchat:
ImageSearch, OutputVarX, OutputVarY, 1702, 940, 1911, 1068, C:/arrow.png *50
if ErrorLevel = 2
Tooltip, The program didn't work
Else if ErrorLevel = 1
Tooltip Icon could not be found on the screen.
Else
;CoordMode, Pixel, Window
;PixelGetColor, thepixel, 1838, 34
;If thepixel = A69648
{
Tooltip, Icon was found on the screen.
Suspend, on
}
Return

; MOVE CURSOR TO CENTRE OF SCREEN WHEN ACTIVATING WINDOW
; -------------------------------------------------------------------

Loop
{
WinWaitActive, ahk_group WinGroup
{
MouseMove, 1033, 500
}
WinWaitNotActive, ahk_group WinGroup
}

; ENABLE AND DISABLE SCRIPT WHEN CHATTING AND SAVING
; -------------------------------------------------------------------

^d::
SendInput, s
Suspend, on
Return

Tab::
SendInput, {Tab}
Suspend, on
Return

Enter::
Suspend, off
Sendinput, {Enter}
Return

NumpadEnter::
Suspend, off
Sendinput, {Enter}
Return

; MISC HOTKEYS
; -------------------------------------------------------------------

XButton1::Escape
MButton::Enter
q::h
e::t
f::e
z::d
`::q
^d::s
^c::l
x::z
X::w
C::i
r::u

; Auto attack in combat
+d::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 603, 974
MouseMove, OrigX, OrigY
Return
}


; 1-8 HERO HOTKEYS
; -------------------------------------------------------------------

1::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 343
MouseMove, OrigX, OrigY
Return
}

2::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 395
MouseMove, OrigX, OrigY
Return
}

3::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 450
MouseMove, OrigX, OrigY
Return
}

4::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 500
MouseMove, OrigX, OrigY
Return
}


5::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 550
MouseMove, OrigX, OrigY
Return
}

6::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 600
MouseMove, OrigX, OrigY
Return
}

7::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 650
MouseMove, OrigX, OrigY
Return
}

8::
{
MouseGetPos, OrigX, OrigY
MouseClick, Left, 1659, 700
MouseMove, OrigX, OrigY
Return
}

; WASD MAP SCROLLING
; -------------------------------------------------------------------

~s & a::
MouseGetPos, OrigX, OrigY
While GetKeyState("s","P") && GetKeyState("a","P") 
{
MouseMove, 0, 1079
}
MouseMove, OrigX, OrigY

~a & s::
MouseGetPos, OrigX, OrigY
While GetKeyState("a","P") && GetKeyState("s","P")
{
MouseMove, 0, 1079
}
MouseMove, OrigX, OrigY

~a & w::
MouseGetPos, OrigX, OrigY
While GetKeyState("a","P") && GetKeyState("w","P") 
{
MouseMove, 0, 0
}
MouseMove, OrigX, OrigY

~w & a::
MouseGetPos, OrigX, OrigY
While GetKeyState("w","P") && GetKeyState("a","P")
{
MouseMove, 0, 0
}
MouseMove, OrigX, OrigY

a::
  While GetKeyState("a","P")
  {
    Send, ^{Left}
    Sleep, 50
  }

~w & d::
MouseGetPos, OrigX, OrigY
While GetKeyState("w","P") && GetKeyState("d","P") 
{
MouseMove, 1919, 0
}
MouseMove, OrigX, OrigY

w::
  While GetKeyState("w","P")
  {
    Send, ^{Up}
    Sleep, 50
  }

~d & w::
MouseGetPos, OrigX, OrigY
While GetKeyState("d","P") && GetKeyState("w","P") 
{
MouseMove, 1919, 0
}
MouseMove, OrigX, OrigY

s::
  While GetKeyState("s","P")
  {
    Send, ^{Down}
    Sleep, 50
  }

~d & s::
MouseGetPos, OrigX, OrigY
While GetKeyState("d","P") && GetKeyState("s","P")
{
MouseMove, 1919, 1079
}
MouseMove, OrigX, OrigY

~s & d::
MouseGetPos, OrigX, OrigY
While GetKeyState("s","P") && GetKeyState("d","P")
{
MouseMove, 1919, 1079
}
MouseMove, OrigX, OrigY

d::
  While GetKeyState("d","P")
  {
    Send, ^{Right}
    Sleep, 50
  }
Kaiji
Posts: 6
Joined: 14 Jan 2019, 16:01

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 19:04

OK, that's a step forward. I changed that line to...

Code: Select all

ImageSearch, OutputVarX, OutputVarY, 1702, 940, 1911, 1068,  *50 C:/arrow.png
... and also tried it without the *50. Now the command works but it doesn't find the image (tooltip says "Icon could not be found"), when it is definitely there on the screen within the area I defined, the first two of the four numbers are the top left corner and the second two are the lower right corner of the rectangle to search within.
Kaiji
Posts: 6
Joined: 14 Jan 2019, 16:01

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 20:38

Yeah, I assumed I could leave it at the default without using Coordmode since it refers to the window I'm in, unless I'm misunderstanding.

I added "CoordMode, Pixel, Window" to the start of the Checkchat timer and used this script to find the color of the pixel I was searching for (I gave up trying to get ImageSearch to work).

Code: Select all

MouseGetPos, mx, my
PixelGetColor, thepixel, mx, my
tooltip, Pixel color at %mx% %my% is %thepixel%
It turned out that Window Spy was returning a different color at the same coordinates. My best guess as to why that might be is that I'm using a borderless window program, but it looks like I'll be able to move forward from here using PixelGetColor.

Thanks a million for your help, swagfag!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Trying to detect an image seems to do nothing at all

14 Jan 2019, 20:59

there is also a CoordMode for Mouse
mast4rwang
Posts: 141
Joined: 19 Jul 2017, 09:59

Re: Trying to detect an image seems to do nothing at all

15 Jan 2019, 04:45

You play heroes 3? I have it too :D
Btw I don't know if it helps but remember to run in normal windowed mode or pixelsearch and imagesearch won't work! It only worked full screen back when windows was decent (win7).

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 38 guests