Dude
Dude

Reputation: 39

Having two Windows excluded in a IfWinNotActive in AutoHotKey

I am mapping Alt+F4 to ESC so that I can close windows just by pressing escape. However there are two windows that I need to actually use ESC in. So, when either of these two windows are active I want to be able to press ESC without closing the window. What is the easiest way to accomplish this? I have my script working when I just am excluding one active window but I need to work when either of the two windows are active.

Here is my attempted code:

GroupAdd, ESC, Untitled - Notepad
GroupAdd, ESC, 

#IfWinNotActive, ahk_group, ESC
Escape::!F4
Return

This is the code that works properly with just one window:

;#IfWinNotActive, Untitled - Notepad
;Escape::!F4
;Return

UPDATE: Should this work?

SetTitleMatchMode, 2
SetTitleMatchMode, 2
#IfWinNotActive, Untitled - Notepad
#IfWinNotActive, Document 1 - Microsoft Word
    Escape::!F4
Return

Upvotes: 2

Views: 3168

Answers (3)

JinSnow
JinSnow

Reputation: 1663

This worked for me:

1: write this at the top of your script (before the first hotkey):

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetTitleMatchMode, Regex 

2: Then you can use the not operator !:

if !(WinActive("ahk_class Notepad") or WinActive("ahk_class Calculator")) {
...
}

Credit

Upvotes: 1

bgmCoder
bgmCoder

Reputation: 6371

Try using SetTitleMatchMode.

http://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm

2: A window's title can contain WinTitle anywhere inside it to be a match.

Then you can do this (it is case-sensitive by default):

settitlematchmode, 2
#IfWinNotActive, Untitled - 

Try this:

First, you can't remap the same key twice in the same script.

This command affects the behavior of all windowing commands, e.g. IfWinExist and WinActivate.

Second, you stack lines like this:

#IfWinNotActive, Untitled - Notepad
#IfWinNotActive, Document 1 - Microsoft Word

you are saying, if win1 is not active, THEN if win2 is not active.

Try this, instead:

settitlematchmode, 2
app1 := winexist("other app")
app2 := winexist("Untitled - Notepad")

if(app1 || app2)    ;the || means OR.  you can use && for AND.
    Escape::!F4   ;you can only map a particular key one time per script

or this, which is more along the lines of your approach:

settitlematchmode, 2
GroupAdd, ESC, Untitled - Notepad
GroupAdd, ESC, My other window
IfWinNotActive, ahk_group ESC
    Escape::!F4
Return

Upvotes: 3

Elliot DeNolf
Elliot DeNolf

Reputation: 2999

You have an extra comma in your #IfWinNotActive line after ahk_group

Try the following:

GroupAdd, ESC, Untitled - Notepad
;  Add more windows to the group if necessary

#IfWinNotActive, ahk_group ESC
    Escape::!F4
Return

Upvotes: 6

Related Questions