How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

07 Aug 2018, 12:03

Script1 has a hotkey for Alt+e...

Code: Select all

!e::MsgBox I don't want this messagebox to happen when app1 is active.
While app1 is active, sometimes I press Alt+e for a hotkey that is specific to app1. When that happens, I don't want Script1's hotkey to trigger. The best solution to this would be to use IfWinActive/IfWinNotActive...

Code: Select all

#IfWinNotActive app1
!e::MsgBox I don't want this messagebox to happen when app1 is running.
#IfWinActive
However there are many similar hotkeys in script1, some of which have their own specific #IfWinActive or similar conditions. If I were to utilize #IfWinNotActive like above, I would need to add it all over script1, and I don't want to for other reasons as well, so assume for this request that is not possible.

Is there any method by which script2 could intercept Alt+e so that script1 doesn't trigger it's hotkey for it? Assume both script1 and script2 are constantly running.

One potential method so far would be to have a timer in script2 to check if app1 is active and if it is, use PostMessage to suspend hotkeys (https://www.autohotkey.com/docs/FAQ.htm#close) for script1, and unsuspend when app1 no longer active. I don't love that but it could potentially work, however that PostMessage method is a toggle, rather than a binary switch to suspend or unsuspend, so I see potential for script2 to sometimes accidentally toggle script1's hotkey suspension back on or off when I don't want.

Any other ideas?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

07 Aug 2018, 13:24

u can use a plain #If along with WinActive() to combine multiple condition into one single directive:

Code: Select all

#If !WinActive("app1") && WinActive("app2")
!e::MsgBox something
#If
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

07 Aug 2018, 13:27

swagfag wrote:u can use a plain #If along with WinActive() to combine multiple condition into one single directive:

Code: Select all

#If !WinActive("app1") && WinActive("app2")
!e::MsgBox something
#If
I appreciate the idea, but that looks like it would still require heavily modifying script1, which I want to try to avoid here if possible.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

07 Aug 2018, 13:50

first.ahk

Code: Select all

q::MsgBox % "from first"
second.ahk

Code: Select all

#If WinActive("ahk_exe notepad.exe")
q::return
#If
try if it works for your app
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

07 Aug 2018, 15:07

- Are you aware that hotkeys are positional. So, perhaps you can use fall through. If you have an '#IfWinActive' hotkey, and no earlier hotkey was triggered, that hotkey is triggered.

Code: Select all

#IfWinActive, A
q::MsgBox, % "A"
#IfWinActive, B
q::MsgBox, % "B"
#IfWinActive, C
q::MsgBox, % "C"
#IfWinActive
q::MsgBox, % "other"
- Another idea is if script 1 receives a hotkey, script 1 does something. If script 2 receives a hotkey, it checks if script 1 is open, if script 1 is open, script 2 sends a message and does a 'return', if script 1 is not open, script 2 does something. (That would involve: DetectHiddenWindows, if WinExist, PostMessage and OnMessage.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

07 Aug 2018, 19:51

swagfag wrote:first.ahk

Code: Select all

q::MsgBox % "from first"
second.ahk

Code: Select all

#If WinActive("ahk_exe notepad.exe")
q::return
#If
try if it works for your app
This is the solution I would recommend also.

When two scripts have the same hotkey the last one run has priority.

In this case the second script is checked first. If notepad is active it triggers. If notepad is not active then this script does not capture the key press and the next script with the same hotkey catches it.

You could layer it as much as you want. You just need to be sure the scripts are always started in the right order. If you need to reload one you will have to reload them all to get the order right again.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

09 Aug 2018, 12:27

FanaticGuru wrote:
swagfag wrote:first.ahk

Code: Select all

q::MsgBox % "from first"
second.ahk

Code: Select all

#If WinActive("ahk_exe notepad.exe")
q::return
#If
try if it works for your app
This is the solution I would recommend also.

When two scripts have the same hotkey the last one run has priority.

In this case the second script is checked first. If notepad is active it triggers. If notepad is not active then this script does not capture the key press and the next script with the same hotkey catches it.

You could layer it as much as you want. You just need to be sure the scripts are always started in the right order. If you need to reload one you will have to reload them all to get the order right again.

FG
Hmm, could that also be set to just Send that hotkey to the window instead of return? i.e. for second.ahk rather than "q::return" it would be "q::Send q" so that button still gets sent to active window (and therefore doesn't get triggered by first.ahk as a hotkey)...
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to use script2 to block script1's hotkeys when a specific window is active WITHOUT using IfWinActive

09 Aug 2018, 13:27

Code: Select all

; first.ahk
$q::MsgBox % "from first"


; second.ahk
#If WinActive("ahk_exe notepad.exe")
*q::Send {Blind}q
#If

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3 and 243 guests