Page 1 of 1

Only first #IfWinActive works

Posted: 25 Jun 2022, 01:03
by lawnmowerobot
The script below is pretty simple and it looks like it should work in a straightforward fashion. This issue is that it works for the first case (ie Notepad) but not for Calulator and I really cant see why?? Any help greatly appreciated as driving me to distraction.... Thanks LMR

Code: Select all

!4::

#IfWinActive, ahk_exe notepad.exe
MsgBox, Notepad is active
return
#IfWinActive

#IfWinActive, ahk_exe calculator.exe
MsgBox, Calculator is active
return
#IfWinActive

return

[Mod edit: Added [code][/code] tags]

Re: Only first #IfWinActive works

Posted: 25 Jun 2022, 01:19
by boiler
Read the documentation on directives again. They go before the hotkey name itself, not pieces of code after it. The last one before the hotkey is the one that applies. You can’t have two directives apply within the same hotkey. You can repeat the hotkey with a different directive, like this:

Code: Select all

#IfWinActive, ahk_exe notepad.exe

!4::
MsgBox, Notepad is active
return

#IfWinActive, ahk_exe calculator.exe

!4::
MsgBox, Calculator is active
return

Which can be reduced to this because single line hotkey routines can be on the same line without a return:

Code: Select all

#IfWinActive, ahk_exe notepad.exe

!4::MsgBox, Notepad is active

#IfWinActive, ahk_exe calculator.exe

!4::MsgBox, Calculator is active

Note that there is no reason to put a bare #IfWinActive unless you have hotkeys below it for which you don’t want the prior directive to apply, which you don’t have here.

Re: Only first #IfWinActive works

Posted: 25 Jun 2022, 20:03
by lawnmowerobot
Thanks very much for that - works well now (had to change calculator.exe to ApplicationFrameHost.exe as indicated by WinSpy)
And yes will read up on the Directives