#If WinActive not working when using #Include Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

#If WinActive not working when using #Include

27 Dec 2018, 09:52

I have a main.ahk script in which I have all my key bindings.

Yesterday I have added a keybindings for changing the brightness.
I am using this https://www.autohotkey.com/boards/viewt ... &p=126135
So I saved this https://gist.githubusercontent.com/qwer ... Setter.ahk in a file named Screen_brightness.ahk . I also added these lines:

Code: Select all

BS := new BrightnessSetter()
#F5::BS.SetBrightness(3)
#F4::BS.SetBrightness(-3)
return
Then I added this at the very top of my main.ahk

Code: Select all

#Include Screen_brightness.ahk
Everything seems working, I am happy!

But when I try to use my keybindings for Google docs, they aren't working anymore. I use this to target all the Google Docs page

Code: Select all

#If WinActive("Google Docs ahk_class Chrome_WidgetWin_1")
To make my keybindings work, I have to target the first work of each of my google docs (which is quite troublesome), like this:

Code: Select all

#If WinActive("Log Docs ahk_class Chrome_WidgetWin_1")|| WinActive("Jan part ahk_class Chrome_WidgetWin_1")|| WinActive("Feb part ahk_class Chrome_WidgetWin_1")|| WinActive("Mar part ahk_class Chrome_WidgetWin_1")

I tried to solve this by moving #Include Screen_brightness.ahk. below the #If WinActive("Google Docs ahk_class Chrome_WidgetWin_1"). Now my Google Docs keybindings work but not my screen brightness key bindings.


Any idea why this is happenign and how to solve it?
gregster
Posts: 9029
Joined: 30 Sep 2013, 06:48

Re: #If WinActive not working when using #Include

27 Dec 2018, 12:19

Without knowing the exact structure and contents of your main.ahk I can only make an educated guess.

But I think you will have to understand the difference between hotkeys and normal lines of code that need to be integrated into the flow of code execution. Hotkeys can be defined anywhere in the script, other commands need to be in the auto-execute section or in functions or labeled subroutines that get called somehow. But the first hotkey ends the the auto-execute section - this is clear.
Consider this:

Code: Select all

msgbox Hello		; gets executed as part of the auto-execute section
gosub jump			; gets executed; code execution continues here because of the return in the subroutine
;---------------------------------------
Space::msgbox Space	; the first hotkey ends the the auto-execute section
msgbox World			; code is unreachable
SetTitlematchmode 2	; also unreachable
a::msgbox a			; another working hotkey

jump:				; gets called from the auto-execute section
msgbox jump			; gets executed
return				; retunrs to the the auto-execute section

Esc::ExitApp
Also note that #Include means that the included portions of the script are treated as if they were copied to the exact place where the #include occurs.

Hence, if you move (via #include or not) the line BS := new BrightnessSetter() below other hotkeys, it is clear that #F5 and #F4 cannot find anymore the BS object, because the line is not executed anymore and no object is created. Compare auto-execute section again.

So why are the other hotkeys not working anymore after the re-arrangement? Probably because you also have lines in front of them that should be executed before, which make the hotkeys work in a desired way (exactly like with the BS object above). Perhaps you made a SetTitlematchmode command unreachable, or something else...

That means, you will have to re-arrange your script in a way so that all preparations for the hotkeys get executed, either in the auto-execute section at the beginning of the main script, before you define the hotkeys or by jumping to labels via gosub or by executing functions that will return to the auto-execute section until everything important is finished.
Net_Wars
Posts: 51
Joined: 10 Oct 2018, 11:11

Re: #If WinActive not working when using #Include

27 Dec 2018, 14:09

Try with adding this:
#Persistent at the beginning of script
and this
#If under

Code: Select all

#If WinActive("Google Docs ahk_class Chrome_WidgetWin_1")
Hope it helps
Hi,
I was trying make this on my own but I'm good only in basics not good even in basics. :facepalm:

Sorry for my bad English. If something is unclear I will try better next time. :headwall:
gregster
Posts: 9029
Joined: 30 Sep 2013, 06:48

Re: #If WinActive not working when using #Include

27 Dec 2018, 14:18

#Persistent doesn't do anything here. There are already hotkeys in the script that make it persistent.
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: #If WinActive not working when using #Include

27 Dec 2018, 15:16

Thanks a lot gregster for your time. I think I understand the part about auto-execute section and hotkey that will break the auto-execute.

I tried many combination but still without being able to use my google docs keybindings and the brightness.

Here is a small portion of my main.ahk (but I think the one that is relevant). https://pastebin.com/VPAMzQJh
Above #Include Screen_brightness.ahk , there are only comments. And below the part, there more key bindings that are working.

(Yes, my code is persistent)
gregster
Posts: 9029
Joined: 30 Sep 2013, 06:48

Re: #If WinActive not working when using #Include  Topic is solved

27 Dec 2018, 15:28

OK, the included part is not the problem - a class and functions can be above/in the auto-execute section.

But the first SetTitleMatchMode, Regex doesn't get executed - it is unreachable because of the #F5 (and #F4) hotkey...(the second occurence of SetTitleMatchMode, Regex is unreachable, too, but it doesn't matter here - you only need the first, the second is redundant)
Put it in the auto-execute section at the top of the script:

Code: Select all

#Include Screen_brightness.ahk	; a class is included - ok
SetTitleMatchMode, Regex
BS := new BrightnessSetter()	
; auto-execute ends here because of the following hotkey
#F5::BS.SetBrightness(3)
#F4::BS.SetBrightness(-3)
;return ; doesn't do anything here
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: #If WinActive not working when using #Include

28 Dec 2018, 00:48

Wow, this is working. You were right, I didn't understand how SetTitleMatchMode, Regex works. Thanks so much for your time and your patience!


Something that I still don't understand though is that the #If WinActive() get registered with the hotkeys even if #If WinActive() is below the first hotkey .... Maybe it's because each time a hotkey calls, it looks for a its attributed "WinActive"?

One last question: if I want different SetTitleMatchMode for different hotkeys, I thought I could simply do this:

Code: Select all

gosub regexsub
gosub noregexsub

regexsub() {			
SetTitleMatchMode, Regex
#If WinActive("Google Docs ahk_class Chrome_WidgetWin_1")
!p::
msgbox This is called with SetTitleMatchMode regex
return
#If WinActive("Google Sheets ahk_class Chrome_WidgetWin_1")
!r::
msgbox This is called with SetTitleMatchMode regex
return

}

noregexsub(){
SetTitleMatchMode, 1
#If WinActive("Jan ahk_class Chrome_WidgetWin_1")
!p::
msgbox This is called with SetTitleMatchMode 1
return
#If WinActive("Feb ahk_class Chrome_WidgetWin_1")
!r::
msgbox This is called with SetTitleMatchMode 1
return
}
But I get an error : hotkeys are not allow in function.

I then tried this

Code: Select all

!p::
SetTitleMatchMode, 1
#If WinActive("Jan  ahk_class Chrome_WidgetWin_1")
msgbox This is called with SetTitleMatchMode 1  
return

!p::
SetTitleMatchMode, Regex
#If WinActive("Google Docs ahk_class Chrome_WidgetWin_1")
msgbox This is called with SetTitleMatchMode regex
return
But I get the message SetTitleMatchMode 1 even on page with a title that doesn't start with Jan. What I am doing wrong?

I look at this https://autohotkey.com/board/topic/1194 ... ne-script/and that https://autohotkey.com/board/topic/2947 ... winactive/but I am still confused

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, OrangeCat and 171 guests