IfWinActive vs WinActive()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

IfWinActive vs WinActive()

18 Oct 2018, 22:02

Hello,

I noticed that the docs for IfWinActive include an alert stating:
Deprecated: This command is not recommended for use in new scripts. Use the WinActive() function instead.

The syntax shown for WinActive() begins with:
UniqueID :=
Question 1: What does that mean and must it be included?


The syntax also indicates that WinTitle should be included (as it is for IfWinActive), but the following example is given:

Code: Select all

if WinActive("ahk_class Notepad") or WinActive("ahk_class" . ClassName)  ; "ahk_class" need not have a space after it.
    WinClose  ; Uses the last found window.
I've always preceded IfWinActive, with the command SetTitleMatchMode, 2 and then input some part of the WinTitle. For example:

Code: Select all

SetTitleMatchMode, 2
IfWinActive, Excel
	{...}
Return
Question 2: Has that simple form changed and must the class now be included if using WinActive()?

Question 3: What is the simplest/shortest way to retain a hotkey's native function as the Else action if (and only if) an If criteria is not satisfied?

For example:

Code: Select all

Space::
SetTitleMatchMode, 2
IfWinActive, Excel
	{...}
Else
	Send {Space}
Return
Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: IfWinActive vs WinActive()

19 Oct 2018, 04:01

UniqueID := is optional. That is if you want the HWND in the variable

You can simply use WinActive() as, If WinActive("Untitled - Notepad") the same as the old way of, IfWinActive, Untitled - Notepad.

The older command IfWinActive is likely deprecated to gradually prepare people for AutoHotkey V2, where the syntax is done the newer way of WinActive(). You can use the Window title or class name, same as the old way. In the below example, either way will work.

Note- ";" is used to comment out code. By removing the ";" that command line becomes active.

Code: Select all

Loop
{
;	If WinActive("Untitled - Notepad")
	If WinActive("ahk_class Notepad") 
	{
	WinClose, Untitled - Notepad
	break
	}
}
MsgBox, Notepad window was closed!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: IfWinActive vs WinActive()

19 Oct 2018, 05:12

q3: a context sensitive hotkey

Code: Select all

#If WinActive("ahk_exe notepad.exe")
q::MsgBox notepad is active

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Fredrik F, maxkill, ShatterCoder and 403 guests