Switching focus to Active Window with same class

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
diophantus
Posts: 6
Joined: 13 Apr 2021, 12:10

Switching focus to Active Window with same class

Post by diophantus » 13 Apr 2021, 12:32

I'm trying to gain focus on a certain window with a certain title. There is another window that has a different title but the same ahk_class and ahk_exe as the first.
I would like to have a script running so that if I want TitleA (that particular window to have focus), I can just hit a hotkey and it will focus it.
Another hotkey would be for TitleB. I am assuming the script would need to be in a loop or have a WinActive() function...?

I have used functions like WinGetTitle, SetTitleMatchMode, WinActivate and WinActivateBottom.

I have a hotkey and a function like:

Code: Select all

F13::WinActivate, TITLE-A ;
F14::WinActivate, TITLE-B ;
This works and is very simple but the window must lose focus for it to work. I have looked at GroupAdd and Activate but I don't think I need that.
Other solutions seen over the years do basically the same thing as far as focusing on the window but when I need to switch focus to the other window it does nothing.

Any help or guidance? Thanks!
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: Switching focus to Active Window with same class

Post by boiler » 13 Apr 2021, 14:01

You don't need a loop or any of those other commands. Your hotkeys as you showed them is all you need. What do you mean it needs to lose focus for it to work? If it didn't lose focus, then it's already active, so what would you expect that hotkey to do in that case? In other words, if the window you want to have focus hasn't lost focus, what are you trying to accomplish by pressing the hotkey that is meant to focus that window?
diophantus
Posts: 6
Joined: 13 Apr 2021, 12:10

Re: Switching focus to Active Window with same class

Post by diophantus » 13 Apr 2021, 14:15

boiler wrote:
13 Apr 2021, 14:01
You don't need a loop or any of those other commands. Your hotkeys as you showed them is all you need. What do you mean it needs to lose focus for it to work? If it didn't lose focus, then it's already active, so what would you expect that hotkey to do in that case? In other words, if the window you want to have focus hasn't lost focus, what are you trying to accomplish by pressing the hotkey that is meant to focus that window?
If Title-A has focus and is the active window, Title-B is not active nor in focus, when I press F14 for the hotkey to switch to Title-B it does nothing. If I select any other window (I am assuming different class and exe as well) and then hit either of the hotkeys it does bring that particular window into focus.
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: Switching focus to Active Window with same class

Post by boiler » 13 Apr 2021, 14:33

There is no reason for it to work that way, even if their ahk_class and ahk_exe are the same. You're actually using their titles, not just their class and process names, right? Can you show the actual title info for the windows as shown in the Window Spy tool and what you actually have in your script?
diophantus
Posts: 6
Joined: 13 Apr 2021, 12:10

Re: Switching focus to Active Window with same class

Post by diophantus » 13 Apr 2021, 14:48

Yes, using the spy tool it does show the WinTitle as being what is in the code.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 3

F13::WinActivate, Title-A ;
F14::WinActivate, Title-B ;
Return
As I said this works if neither title has focus. But if Title-A has focus and I need to switch to B nothing happens. If neither has focus it works and both hotkeys work independently of one another.
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: Switching focus to Active Window with same class

Post by boiler » 13 Apr 2021, 15:08

I asked if you could show the actual info from the spy tool. Now you've shown different title information than what you showed earlier. The titles are case sensitive, so that could be one issue. You might think it's not important to show exactly what is there, but if you want to find the problem, it is important to do so because the problem has to be in those details. I can tell you that windows titled the same exact thing as yours work with your hotkeys here, even though the class name and process name are the same (both Notepad windows titled TITLE-A and TITLE-B).
User avatar
boiler
Posts: 16901
Joined: 21 Dec 2014, 02:44

Re: Switching focus to Active Window with same class

Post by boiler » 13 Apr 2021, 15:12

It would be best if you either showed screenshots of the Window Spy tool for each window or at least copied and pasted all the text from that top section of the tool.
diophantus
Posts: 6
Joined: 13 Apr 2021, 12:10

Re: Switching focus to Active Window with same class

Post by diophantus » 13 Apr 2021, 15:18

Thanks Boiler for all the help thus far.
The names for the title are correct (just hidden to protect the innocent). The spy tool does show the correct title and case, so does the script and it is the correct title and case.

Again just to clarify, the hotkeys work if they are not in focus. However, if title-A has the focus and I can see (just not in focus) title-B and I want B to have focus the hotkey does not work. If I have a different window in the foreground and in focus, it works. I just can't switch between the two if either has focus back to the other one.
diophantus
Posts: 6
Joined: 13 Apr 2021, 12:10

Re: Switching focus to Active Window with same class

Post by diophantus » 13 Apr 2021, 15:23

Here are the 2 titles with class and exe.
Attachments
Capture1.PNG
Capture1.PNG (7.24 KiB) Viewed 2108 times
Capture.PNG
Capture.PNG (7.05 KiB) Viewed 2108 times
diophantus
Posts: 6
Joined: 13 Apr 2021, 12:10

Re: Switching focus to Active Window with same class

Post by diophantus » 13 Apr 2021, 15:53

I realize now what is happening. Thanks Boiler for your thoughts and guidance. This issue lies with two different keybinds. I must remap what is going on with the mapping in the apps themselves. Since they are the focus it doesn't know what F13 or F14 are.
Post Reply

Return to “Ask for Help (v1)”