Only first #IfWinActive works

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lawnmowerobot
Posts: 9
Joined: 21 Oct 2020, 04:04

Only first #IfWinActive works

Post by lawnmowerobot » 25 Jun 2022, 01:03

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]

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Only first #IfWinActive works

Post by boiler » 25 Jun 2022, 01:19

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.

lawnmowerobot
Posts: 9
Joined: 21 Oct 2020, 04:04

Re: Only first #IfWinActive works

Post by lawnmowerobot » 25 Jun 2022, 20:03

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

Post Reply

Return to “Ask for Help (v1)”