Having two Windows excluded in a IfWinNotActive in AutoHotKey - 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

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

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

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

Related

WinActivate on specific criteria (include / exclude windows)

I'm stumped on how to best re-write my hotkey. What I want to do is simple. I have a script that switches to File Explorer IF it's currently open Else it will open a new file explorer window.
Here's the catch - It's hard to "identify" if the Explorer window is open. I used to just do this by using WinActivate, 00123456 because that's the name of the folder (eight-digit random number). But the problem with that is that these are support ticket numbers. And that number will also be within the title of my current Chrome session and sometimes other windows too (Firefox, Snagit, notepad, etc). So it will inadvertently switch to those other programs. So that led me to exclude in this way:
Ticket_Out := clipboard
xclude:= Ticket_Out . " | Salesforce - Google Chrome"
WinActivate, % Ticket_Out, , % xclude
That's handy except for it only excludes 1 window at a time. How could I implement something that excludes a second application? OR --- another thought, how could I just say: look for Explorer.exe with window title "00123456" ?
You can use ahk_exe to filter by the process name too:
WinActivate, ahk_exe explorer.exe %Ticket_Out%
Another way would be to look for the window class - since depending on the Windows version Explorer can use either CabinetWClass or ExploreWClass, you can use a group, as shown in the docs:
; Define the group: Windows Explorer windows
GroupAdd, Explorer, ahk_class ExploreWClass ; Unused on Vista and later
GroupAdd, Explorer, ahk_class CabinetWClass
; Activate any window matching the above criteria and the title
WinActivate, ahk_group Explorer %Ticket_Out%
Note on CherryDT's answer the two WinActivate examples are missing a comma between the WinTitle, and WinText parameters...
Should be....
WinActivate, ahk_exe explorer.exe, %Ticket_Out%
Second example (when using GroupAdd)
WinActivate, ahk_group Explorer, %Ticket_Out%
Caught me out for awhile.
(I would comment on the answer, but as a new user don't have 50 reputation)

What is ahk_class? How can I use it for window matching?

The AutoHotkey Beginner Tutorial includes a sample script to demonstrate window specific hotkeys and hotstrings.
#IfWinActive ahk_class Notepad
::msg::You typed msg in Notepad
#IfWinActive
#IfWinActive untitled - Paint
::msg::You typed msg in MSPaint!
#IfWinActive
The second example is fairly self-explanatory: Check for a window named "untitled - Paint". It's the first example's use of ahk_class that has me confused.
I haven't been able to find any explanation of the variable in the AHK documentation. According to an AHK forum post, ahk_class is the name of a window as given by Windows Spy, the post didn't go into any more detail.
Would there be any difference between using ahk_class Notepad and Untitled - Notepad? Would the second example work if replaced with #IfWinActive ahk_class Paint?
What is ahk_class and how can I use it for window matching?
From https://autohotkey.com/docs/misc/WinTitle.htm#ahk_class
A window class is a set of attributes that the system uses as a template to create a window. In other words, the class name of the window identifies what type of window it is.
In other words you can use it to identify windows of the same type, if you open notepad the title will be Untitled - Notepad if you save it to temp.txt the title will become temp - Notepad. ahk_class on the other hand will always remain Notepad.
The second example will work if you replace it with #IfWinActive ahk_class MSPaintApp because that's the class of mspaint.
Usually you find ahk_class using Window Spy and then use it in your scripts. If you don't have Window Spy you can use the following hotkey:
#1::WinGetClass, Clipboard, A ; Will copy the ahk_class of the Active Window to clipboard
After you found it you can use it in any place you can use window title for example instead of writing WinActivate, Untitled - Notepad you can write WinActivate, ahk_class Notepad.
Check this article. Ahk_class is the class that is given to you when you use the WindowSpy tool. This tool should be in the same folder as your AutoHotkey executable.

autohotkey partial window title match (without ahk class)

I would like to add partial window title matching to #IfWinActive.
For example to match any notepad window, regardless wether the title is "Untitled - Notepad" or "MyFile - Notepad"
This should be very simple. My attemtps:
SetTitleMatchMode, Regex
SetTitleMatchMode, Slow
#IfWinActive *.Notepad
+n::b
#IfWinActive
second attempt matchmode 2 should match anywhere
SetTitleMatchMode, 2
#IfWinActive Notepad
+n::b
#IfWinActive
A test with using the full title Untitled - Notepad confirms that the code +n::b does substute Shift-n for Shift-b
NOTE: ahk_class is not an option as the class is different on every machine for the target programme TeXnicCenter
As Robert Ilbrink said, my second example actually works, and the problem was that I did not place SetTitleMatchMode at the very top of my script.
The default example script for ^!n was in effect and interfered. Not exactly sure why it does that, but placing SetTitleMatchMode at the top solved my problem.

Combining two sequences of keystrokes using autohotkey

I would like to send a sequence of keystrokes using just one key to accomplish certain task in sublime text 2 using autohotkey. To set a mark in sublime text 2 the key sequence is Ctrl+K followed by Ctrl+Space. I tried
#IfWinActive, ahk_class PX_WINDOW_CLASS
Numpad0::Send ^k sleep 5, Send ^+Space
#IfWinActive
which will activate the first part of the sequence but has the side effect of also typing send sleep 5, Send and trying to save the file which I am not trying to do.
If I remove the (sleep 5, Send ^+Space) I then have to push Ctrl+Space to finish off the sequence in order to set the mark. What do I need to add after the Ctrl+K to accomplish the ability to set a mark in sublime text with autohotkey?
When Send is used, it will try to send the rest of the line. That would be why the rest is being typed. However, I tried splitting them up, and it still did not function correctly.
The following works for me, give it a try:
#IfWinActive, ahk_class PX_WINDOW_CLASS
Numpad0::Send, {CTRLDOWN}k{CTRLUP}{CTRLDOWN}{SPACE}{CTRLUP}
#IfWinActive
Inside #IfWinActive, you can use multiple lines this way.
#IfWinActive, ahk_class PX_WINDOW_CLASS
Numpad0::
Send, ^k ; Send Ctrl k
Sleep, 5 ; Wait 5 ms (probably too short, I would use 400 ms)
Send, ^+{Space} ; Send Ctrl Shift Space
Return
#IfWinActive
Added Curly brackets around Enter!

Mimic Sublime Text 2 multi select (multi cursor) in Notepad++ using autohotkey

In sublime text 2 you can multi select(cursor) lines by holding ctrl+alt+arrow key (up/down). In notepad++ you can accomplish the same thing by holding ctrl and clicking the additional lines(areas) you would like to edit. I figured I could use a autohotkey script to accomplish the same functionality. So I tried
#IfWinActive, ahk_class Notepad++
^!Down::^Click
but each time that I try to load the script I get
Error at Line 2
Line Text: ^Click
Error: This line does not contain a recognized action
the program will exit
then I tried
#IfWinActive, ahk_class Notepad++
^!Down::send ^Click
but that shows up as ETXClick in notepad++
What more do I need to get this script to work only for Notepad++
EDIT: It appears that autohotkey cannot mimic this behavior in Notepad++
IT
You can use Alt + Shift + Arrow keys.
Change to:
#IfWinActive, ahk_class Notepad++
^!Down::Send, ^{Click}
#IfWinActive