Scripts only active when the title window is in focus?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Scripts only active when the title window is in focus?

Post by stevesax » 04 Jul 2017, 07:17

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.

A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

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

Post by A_AhkUser » 04 Jul 2017, 07:29

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.
my scripts

stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

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

Post by stevesax » 04 Jul 2017, 10:27

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.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

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

Post by BoBo » 04 Jul 2017, 10:38

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 (~) ?

robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

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

Post by robertcollier4 » 04 Jul 2017, 10:42

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.

stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

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

Post by stevesax » 04 Jul 2017, 11:17

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.

robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

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

Post by robertcollier4 » 04 Jul 2017, 11:47

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.

stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

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

Post by stevesax » 04 Jul 2017, 14:01

Thanks very much, really appreciated.

S1lver
Posts: 23
Joined: 07 Nov 2020, 04:14

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

Post by S1lver » 26 Jul 2021, 10:14

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

Post Reply

Return to “Ask for Help (v1)”