How to make #IfWinActive match THE script's window(s)?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kidbit
Posts: 168
Joined: 02 Oct 2013, 16:05

How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 11:13

Hey, I'm having a trouble making a script with a hotkeys that I'd like to work only within that script's windows.
Seems like #IfWinActive supports WinTitle, but doesn't support variables, otherwise I'd use something like

Code: Select all

#IfWinActive ahk_id %A_ScriptHwnd%
Using IfWinActive() (without #) - doesn't suit me, as well as specifying ahl_class + title in the WinTitle param, which doesn't suit me too.



The solution (thanks to Lexikos and the other pals):

Code: Select all

GroupAdd Self, % "ahk_pid " DllCall("GetCurrentProcessId") ; Create an ahk_group "Self" and make all the current process's windows get into that group.
#IfWinActive ahk_group Self
... ; here goes your code, which would get executed only if any of script's windows is currently active
Alternatively, you may use

Code: Select all

#If WinActive("ahk_pid " DllCall("GetCurrentProcessId"))
... ; here goes your code, which would get executed only if any of script's windows is currently active
but the 1st code is more preferred.
Don't use "ahk_id " WinExist() unless you actually want to restrict it to the first matching window.
Last edited by kidbit on 09 Oct 2013, 18:37, edited 2 times in total.
question := (2b) || !(2b) © Shakespeare.
kidbit
Posts: 168
Joined: 02 Oct 2013, 16:05

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 13:37

Thanks, it should work, but for some reason it doesn't: probably, because my script has GUI and thus it may have multiple hwnds :-/
Even

Code: Select all

ScriptHwnd_A := WinExist("Ahk_PID " DllCall("GetCurrentProcessId"))
#If WinActive("ahk_id " ScriptHwnd_A)
...
doesn't work :-/
question := (2b) || !(2b) © Shakespeare.
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 13:56

directives are evaluated before variables. Lexikos might correct me but i don't think you can use a variable in a directive line
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 15:02

Code: Select all

#If WinActive("ahk_id " WinExist("Ahk_PID " DllCall("GetCurrentProcessId")))
This should work, or actually, the former should work also as I think the expression is evaluated each time the hotkey is pressed (~at run-time). If not, from what I remember, all Autohotkey windows have the same class (thanks to Guest3456) , or, you could use the Hotkey command to create the hotkey once handles to all of windows are known.
kidbit
Posts: 168
Joined: 02 Oct 2013, 16:05

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 15:18

trismarck wrote:

Code: Select all

#If WinActive("ahk_id " WinExist("Ahk_PID " DllCall("GetCurrentProcessId")))
This should work, or actually, the former should work also as I think the expression is evaluated each time the hotkey is pressed (~at run-time). If not, from what I remember, all Autohotkey windows have the same class (thanks to Guest3456) , or, you could use the Hotkey command to create the hotkey once handles to all of windows are known.
Doesn't work too.
question := (2b) || !(2b) © Shakespeare.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 16:32

Code: Select all

ScriptHwnd_A := WinExist("ahk_class AutoHotkey Ahk_PID " DllCall("GetCurrentProcessId"))
#If WinActive("ahk_id " ScriptHwnd_A)
...
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 16:38

the hwnds of the Gui Windows are different from the ScriptHwnd. you haven't said whether you only want Gui windows or if you want the main script window as well

if you only want guis, you should try ahk_class AutoHotkeyGUI

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 17:04

kidbit wrote:

Code: Select all

ScriptHwnd_A := WinExist("Ahk_PID " DllCall("GetCurrentProcessId"))
#If WinActive("ahk_id " ScriptHwnd_A)
...
doesn't work :-/
There would be two reasons for that:
1) It's still looking for one specific window (the one found by WinExist).
2) ScriptHwnd_A might be blank: If you've written the code exactly as above but combined with some other script, you might not have put the assignment in the auto-execute section.

I suggest the following method, which is more reliable than #if:

Code: Select all

GroupAdd Self, % "ahk_pid " DllCall("GetCurrentProcessId")
;...
#ifWinActive ahk_group Self
Alternatively,

Code: Select all

#If WinActive("ahk_pid " DllCall("GetCurrentProcessId"))
Don't use "ahk_id " WinExist() unless you actually want to restrict it to the first matching window.
kidbit
Posts: 168
Joined: 02 Oct 2013, 16:05

Re: How to make #IfWinActive match THE script's window(s)?

09 Oct 2013, 18:26

lexikos
Thanks.
You are probably right about the reasons of why that WinExist didn't work out for me.
I don't know the reasons why the "directives are evaluated before variables" which means that they accept only some (not all) variables, but thanks for sharing both of the tricks to make it work the way I wanted.
Each of them works, but since you didn't go into details of why #If is not so reliable - I'll use the GroupAdd method.
Also, don't you think it's worth adding that case to the manual (to the #IfWinActive page)?

Issue is solved, I'm copying the answering code to the 1st post in this thread (I believe, this is what moderators should do on a regular basis in all the "resolved" threads).
question := (2b) || !(2b) © Shakespeare.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to make #IfWinActive match THE script's window(s)?

10 Oct 2013, 02:50

kidbit wrote:I don't know the reasons why the "directives are evaluated before variables" which means that they accept only some (not all) variables,
No, that was misinformation. Directives are processed at an early stage, and typically don't support variable references. However, in this case, #if simply sets an expression to be evaluated at a later stage. When the hotkey is potentially being fired, the expression is evaluated (including variable references).
Each of them works, but since you didn't go into details of why #If is not so reliable - I'll use the GroupAdd method.
See below.
When the key combination which forms a hotkey is pressed, the #If expression is evaluated to determine if the hotkey should activate. The system may not respond to keyboard or mouse input until expression evaluation completes or times out.
If the script thread is unresponsive for any reason, it can cause input lag.
Also, don't you think it's worth adding that case to the manual (to the #IfWinActive page)?
Perhaps, but I'm lazy.
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: How to make #IfWinActive match THE script's window(s)?

10 Oct 2013, 03:52

lexikos wrote:
kidbit wrote:Also, don't you think it's worth adding that case to the manual (to the #IfWinActive page)?
Perhaps, but I'm lazy.
@kidbit: You are always welcome to enhance documentation and create a pull request on https://github.com/Lexikos/AutoHotkey_L-Docs. This would allow Lexikos being lazy furthermore ;) - unless he still has to merge it into documentation ...
marktherob1950
Posts: 12
Joined: 21 May 2016, 14:25
Location: Toronto, Canada

Re: How to make #IfWinActive match THE script's window(s)?

22 May 2016, 12:33

can this be adapted to only execute for a particular program.

I have a script that executes for all programs and I only want it to work in one windows title is filename - Nitro Pro 10

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Insanibaccha and 208 guests