#IfWinActive vs If WinActive()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

#IfWinActive vs If WinActive()

18 Oct 2018, 08:54

#IfWinActive ahk_class only processes lines after it if they match the app.
If WinActive("ahk_exe ") only processes lines after it if they match the app.
Am I duplicating effort here?

I am unclear on the best way to approach this, could someone explain best practice for this to me please?

Code: Select all

Explorer_JumpTo(expl_path)
{
    #IfWinActive ahk_class CabinetWClass   ; Only process lines below here if CabinetWClass is in focus
    if WinActive("ahk_exe explorer.exe")
    {
        Send, !d   ; move to the address bar
        Sleep, 100
        Send, %expl_path%
        Sleep, 100
        Send, {Enter}
    }
    #IfWinActive                           ; End context sensitivity for CabinetWClass
    return
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: #IfWinActive vs If WinActive()

18 Oct 2018, 08:57

#ifwinactive is a directive affecting hotkeys and hotstrings. IDK why you're using that here

u can specify multiple window attributes in if WinActive("....") for greater precision
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: #IfWinActive vs If WinActive()

18 Oct 2018, 09:11

Thanks swagfag, I'm using it because I'm clueless and trying to learn the language for the last 2 weeks! :-)

That's great, thanks for the clarification, can you give me an example of when you would use #ifwinactive. Not sure what you mean by a directive affecting hotkeys and hotstrings, do you mean something like this, and then would that mean that this would force everything inside the directive to only be visible inside those apps?

Code: Select all

#ifwinactive ahk_class CabinetWClass   ; Only process lines below here if CabinetWClass is in focus
^!a:: function1(stuff)
^!b:: function2(stuff)
^!c:: function3(stuff)
#ifwinactive  ; End context sensitivity for CabinetWClass
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: #IfWinActive vs If WinActive()

19 Oct 2018, 04:46

Bit confused on this.
I create a function that can only run inside explorer. Fine.
I create a hotkey that will only run if explorer is active/focused (#IfWinActive ahk_class CabinetWClass).
In practice this does not work.
To test it, I load the below, then go to Chrome and do Ctrl-Shift-t (which will reload a previously closed tab). Nothing happens.
I unload this .ahk script and Ctrl-Shift-t again works in Chrome.
If I put a ~ in front of the ^+t, then Ctril-Shift-t works in Chrome again, but that makes no sense due to the #IfWinActive ahk_class CabinetWClass which should mean that these lines are only interpreted while Explorer is active.

Could someone explain to me why this is not working as I would expect please?

Code: Select all

Explorer_JumpTo(expl_path)
{
    if WinActive("ahk_exe explorer.exe")
    {
        Send, !d   ; move to the address bar
        Sleep, 100
        expl_path := RegExReplace(expl_path, "!.", "{!}{.}")
        Send, %expl_path%
        Sleep, 100
        Send, {Enter}
    }
    return
}

#IfWinActive ahk_class CabinetWClass   ; Only process lines below here if CabinetWClass is in focus
^+t::Explorer_JumpTo("\\HP1\Drive-D\0\!.Complete")
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: #IfWinActive vs If WinActive()

19 Oct 2018, 05:09

cant reproduce it with the script uve provided. it works as one would expect it to
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: #IfWinActive vs If WinActive()

19 Oct 2018, 08:06

Ctrl-Shit-T stopped working for me in Chrome.
Do you think that my description above is correct and that I should not need the ~ for this?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Decar, doodles333, Google [Bot], mikeyww, Tupper and 223 guests