Page 1 of 1

Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 07:17
by stevesax
Hi. I'm working on an ahk script for a VST, hosted within a DAW (digital audio workstation). The issue is, I'm not at all sure what to use in order to only have this script active when the actual window is in focus.
I have previously used Process, Exist, Name of exe to determine the PID, but as this is a VST, hence being hosted by the DAW.exe, I'm currently unable to stop hotkeys triggering outside of the window when it's in focus.

I'm using the below function to determine this, so I can call it from other functions and scripts within the set.

Code: Select all

GetLocation()
{
IfWinActive, StylusRMX
{
	WinWaitActive, StylusRMX
blnResult:=1
}
Else
{
blnResult:=0
}
Return blnResult
}
StylusRMX is the title of the window, but like I say, keys still trigger no matter what window you're in on the PC.
I've looked at WinExist, WinWait, IfWinActive etc etc, but it's becoming a bit of a grey area for me. So, what's the best approach in making these scripts only active when this plugin is in focus? THX for any help Steve.

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 07:29
by A_AhkUser
Hi stevesax,

Here's two links that seems to me relevant concerning your question:
Create context-sensitive hotkeys and hotstrings
The "Hotkey If" command

Hope this helps.

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 10:27
by stevesax
Hi A_AhkUser and thanks for those links. According to that info, it says
"The #IfWin directives are positional: they affect all hotkeys and hotstrings physically beneath them in the script. They are also mutually exclusive; that is, only the most recent one will be in effect."

So, can I use:
#IfWinActive, StylusRMX
in the top of the script?...will this affect all subsequent hotkeys, or do I need to specify this "If" within every hotkey section? I need to block all keys outside of the window in question, so that those same keys do their default action in other windows.

I'm coming from a language where I would allow the key to be passed through if the scripts wasn't active . So, it's just knowing how AHK allows this and if it can do it on a global/script basis. THX Steve.

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 10:38
by BoBo
I'm coming from a language where I would allow the key to be passed through if the scripts wasn't active . So, it's just knowing how AHK allows this and if it can do it on a global/script basis
https://autohotkey.com/docs/Hotkeys.htm > tilde (~) ?

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 10:42
by robertcollier4
You will need to first get the Window Class of the application (look for the string after ahk_class) using the following tool:
https://github.com/fincs/SciTE4AutoHotk ... owInfo.ahk
1. Install AutoHotkey
2. Download the following file (click on "Raw" then Save As from your browser)- https://github.com/fincs/SciTE4AutoHotk ... owInfo.ahk
3. Run the file ActiveWindowInfo.ahk - this will open a window called "Active Window Info"
4. Open your application, resize the window so that it is next to "Active Window Info" window. Now click anywhere inside the application window and you will see the ahk_class in the "Active Window Info" window, replace this with MyWinClass1 below.

Then specify your hotkeys in between the two "IfWinActive" as shown below

Code: Select all

#IfWinActive, ahk_class MyWinClass1
F5::SendInput, ^z

F6::SendInput, ^c

F7::SendInput,^v
#IfWinActive
stevesax wrote:will this affect all subsequent hotkeys
Yes, it will affect all subsequent hotkeys until the next blank #IfWinActive is found.

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 11:17
by stevesax
Thanks robertcollier4, that's exactly what I needed clarifying.
Will this work simply with the title?, as I can't use the class, because this changes virtually everytime you load the plug, so it's not a static class.
Lastly, is there any need to separate functions from hotkeys? Meaning do hotkeys need to be surrounded by those lines and functions not? or will surrounding all code be OK? THX again Steve.
BTW, Bobo, thanks for the nudge to read the docs :), I'm doing nothing but, just need clarification and a little help in transferring what I read into the structure of my script.

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 11:47
by robertcollier4
stevesax wrote: Will this work simply with the title?, as I can't use the class, because this changes virtually everytime you load the plug, so it's not a static class.
Lastly, is there any need to separate functions from hotkeys? Meaning do hotkeys need to be surrounded by those lines and functions not? or will surrounding all code be OK? THX again Steve.
https://autohotkey.com/docs/commands/_IfWinActive.htm - Title matching behaviour is determined by SetTitleMatchMode
Functions can be anywhere - does not matter if surrounded by those lines or not.

Re: Scripts only active when the title window is in focus?

Posted: 04 Jul 2017, 14:01
by stevesax
Thanks very much, really appreciated.

Re: Scripts only active when the title window is in focus?

Posted: 26 Jul 2021, 10:14
by S1lver
hey m8.
thank you for this script.
I've followed your guide. and it worked with the window. however. I made a loop script that should press a key when a certain color changes. Then when I change the window for instance to notepad, it will constantly press that key, since the color is different from the main window which it should work with.
How do i makes sure the script only will be active in the class window i specified?
Cheers