Checking focus is in the correct window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Chessel
Posts: 12
Joined: 01 Jan 2022, 04:27

Checking focus is in the correct window

29 Jan 2022, 17:16

Hi,
I am writing a script for Session Guitarist, a VST control that goes into a DAW music application. I'm using Reaper.
The most sure fire way to check the identity of the window is to do an image search based on the word "Session Guitarist". After this I have about 12 keystrokes I need to code for to allow the strumming pattern of the guitar to be selected via the keyboard.

I hope it is okay to quote my script below. It isn't working as I want currently and I think it is because I don't understand the control flow.
I want to do some tests before handling hot keys. I do two tests. One that the Sunburst logo file is present. Then I check this logo can be found. Only if these two pass would I then go on to handle hot keys.

I've put MsgBox commands in to tell me what happens. What appears to be happening is that the two tests run when I first start the script by hitting enter on the file from File Explorer as I get the message that the logo can't be found. This makes sense as the logo isn't in File Explorer.

When I navigate to the Sunburst window though and hit either one of my hotkeys, '1' or down cursor key,, it isn't telling me that the Sunburst logo is found or not. One of them should happen. So it looks like the two tests only get run the once. On reading about autoExec I thought the entire script was run every time it was called but perhaps the presence of the hot keys is making it a persistent script so the top section doesn't get run again?

So should I put the code I have at the top into a function and call this function in the code that handles each hot key?
sorry for the long winded message and the thinking as I go along bit.
Here is my code. Many thanks for any help,

Chessel

Code: Select all

; Script for keyboard access to Session Guitarist Electric Sunburst 

; Test that the Sunburst logo file exists 
IfNotExist, ElectricSunburstLogo.png
{
	MsgBox Error: Your Sunburst logo file either doesn't exist or isn't in this location.
	return
}

; Test the Sunburst window has the focus 
ImageSearch, FoundX, FoundY, 0, 0, 2000, 1100, ElectricSunburstLogo.png
if (ErrorLevel = 2)
{
	MsgBox Could not conduct the search for Sunburst logo.
	return
}
else if (ErrorLevel = 1)
{
	MsgBox Sunburst icon could not be found on the screen.
	return
}
else
{
	MsgBox The Sunburst icon was found at %FoundX%x%FoundY%.}
	return
}
;Handle the 1 key being pressed which then shows pattern selection arrows for slot 1 (C1)
1::
ImageSearch, FoundX, FoundY, 0, 0, 2000, 1100, C1.png
if (ErrorLevel = 2)
{
	MsgBox Could not conduct the search for C1 logo 
	return
}
else if (ErrorLevel = 1)
{
	MsgBox C1 icon could not be found on the screen.
	return
}
else
{
	MsgBox The C1 icon was found at %FoundX%x%FoundY%.}
;Click, %FoundX% %FoundY%
}

Down::
ImageSearch, FoundX, FoundY, 0, 0, 2000, 1100, RightArrow.png
if (ErrorLevel = 2)
{
	MsgBox Could not conduct the search for right arrow logo 
	return
}
else if (ErrorLevel = 1)
{
	MsgBox Right arrow icon could not be found on the screen.
	return
}
else
{
	MsgBox The right arrow icon was found at %FoundX%x%FoundY%.}
;Click, %FoundX% %FoundY%
	return
}

Esc::ExitApp  ; Exit script with Escape key
return
Last edited by BoBo on 29 Jan 2022, 17:29, edited 2 times in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Checking focus is in the correct window

29 Jan 2022, 21:09

You're right, but your questions are also readily answered through simple testing. A demonstration is below. Auto-exec is executed exactly once. The script is persistent, which is why the hotkeys work.

A hotkey does not make the auto-exec section execute. It only makes its own section execute, though that section can call others by name. That is what this script demonstrates.

Code: Select all

MsgBox, The script is starting now.

1::
Gosub, Common
MsgBox, This is 1.
Return

2::
Gosub, Common
MsgBox, This is 2.
Return

Common:
MsgBox, This is the common routine.
Return
Chessel
Posts: 12
Joined: 01 Jan 2022, 04:27

Re: Checking focus is in the correct window

30 Jan 2022, 11:22

Thanks. I've now updated my script to call a function at the start of each key which is testing if a particular image can be found in the current window. Function returns 0 if not present and 1 if present.
if CheckWindow() == 0
return
is now under each hotkey.
Many thanks,
Chessel

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 308 guests