Problem with #IfWinActive condition.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Problem with #IfWinActive condition.

23 Mar 2016, 05:02

Hello. Novice user here in need of some help.

First of all thank you for this wonderful piece of software, it's a true lifesaver.

I have created a few simple scripts to use mouse clicks as triggers for keypresses for specific apps. But I have encountered a problem. Despite the #IfWinActive condition, the script is still triggered even if the mouse is outside the window while it remains active.
here is an example:

Code: Select all

#IfWinActive ahk_class IrfanView

MButton::
Send {Enter}
return

Enter::
Send {t down}{t up}
return

LButton::
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
Send {t down}{t up}
return

#IfWinActive ahk_class IrfanViewThumbnails

Enter::
Send {t down}{t up}
return

#ifWinActive	; everything beneath it will work on all windows
It's a script for IrfanView, when it's active and I click the mouse it triggers specific events. no problem with that. But if I move the mouse outside the active window and try to click the taskbar icons, nothing happens because it still triggers the script events. the only way to bypass it is to lose focus of the IrfanView window with alt-tab. so any idea how I can fix this? I need the condition to trigger the events of my script, while the window is active and the mouse is inside the window but not outside it.

any help will be appreciated.
User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: Problem with #IfWinActive condition.

23 Mar 2016, 05:49

The way I've dealt with this in the past is to use MouseGetPos. Whenever you press a hotkey, have it run a function which closely follows the example from the MouseGetPos page to check if the mouse is over Irfanview or not, then have it send the appropriate keystroke(s). I tend to post on my work computer which doesn't have AHK, so here's a brief (potentially bug-laden) example:

Code: Select all

#IfWinActive ahk_class IrfanView

MButton::
MouseGetPos, , , id, control
WinGetClass, temp, ahk_id %id%
;if temp == IrfanView
if temp == "IrfanView"
	Send {Enter}
else
	Send {MButton}
return
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Problem with #IfWinActive condition.

23 Mar 2016, 13:39

Flarebrass is correct in his approach, however, there is a more effective way of doing this.

Code: Select all

#if winActive("ahk_class IrfanView") && mouseOverWin("ahk_class IrfanView")
MButton::
Send {Enter}
return

Enter::
Send {t down}{t up}
return

LButton::
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
Send {t down}{t up}
return

#if winActive("ahk_class IrfanViewThumbnails") && mouseOverWin("ahk_class IrfanViewThumbnails")

Enter::
Send {t down}{t up}
return
#if

mouseOverWin(winName){
    mouseGetPos,,,ahwnd
    winGet,whwnd,id,% winName
    return ahwnd=whwnd?1:0
}
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Re: Problem with #IfWinActive condition.

25 Mar 2016, 05:54

Thank you very much for your help, I finally solved this irritating problem.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Problem with #IfWinActive condition.

25 Mar 2016, 19:24

Flarebrass wrote:if temp == "IrfanView"
Unless the window class is literally = "IrfanView" including a single equal sign and quote marks, this is never going to work. You mean if (temp == "IrfanView") or if temp = IrfanView (but the latter is case-insensitive).

I would also caution against the use of temp as a variable name in scripts that lack #NoEnv. If the variable happens to be empty (e.g. because WinGetClass fails), you will get the value of the TEMP environment variable. Of course, in this case the result is the same (the if evaluates to false), but in other cases it can get very confusing.

Code: Select all

temp := ""  ; Empty the temp variable.
if (temp != "")
    MsgBox temp variable not empty?!
GeneBene

Re: Problem with #IfWinActive condition.

17 Dec 2017, 03:07

Masonjar13 wrote:Flarebrass is correct in his approach, however, there is a more effective way of doing this.

Code: Select all

#if winActive("ahk_class IrfanView") && mouseOverWin("ahk_class IrfanView")
MButton::
Send {Enter}
return

Enter::
Send {t down}{t up}
return

LButton::
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
Send {t down}{t up}
return

#if winActive("ahk_class IrfanViewThumbnails") && mouseOverWin("ahk_class IrfanViewThumbnails")

Enter::
Send {t down}{t up}
return
#if

mouseOverWin(winName){
    mouseGetPos,,,ahwnd
    winGet,whwnd,id,% winName
    return ahwnd=whwnd?1:0
}
helpful indeed! What if I want a regular condition

#if winActive("ahk_class IrfanView") && (someVar = 21)

in y tests regardless what I do it returns as true and continues to execute the script

I also tries AND instead of &&

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, inseption86, jaka1, LuckyJoe, Rohwedder and 327 guests