Focus Previous Window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
techmon
Posts: 54
Joined: 16 May 2017, 13:30

Focus Previous Window

02 Dec 2021, 08:43

Hi,

In my AutoHotKey script, how can I specify to focus the previous window that was focused before the current window that is active, without utilizing Alt Tab (Windows Built-in App Switcher)?

Thanks,
Shawn
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Focus Previous Window

02 Dec 2021, 09:35

Code: Select all

F3::
winNumber = 0
WinGet, win, List
Loop, %win% {
 WinGetTitle, ttitle, % winTitle := "ahk_id " win%A_Index% ; Window title
 WinGet, proc, ProcessName, %winTitle%                     ; Window process
 WinGetClass, class, %winTitle%                            ; Window class
 winNumber += !(class ~= "i)Toolbar|#32770") && ttitle > ""
              && (ttitle != "Program Manager" || proc != "Explorer.exe")
} Until (winNumber = 2)
WinActivate, %winTitle%
Return
techmon
Posts: 54
Joined: 16 May 2017, 13:30

Re: Focus Previous Window

02 Dec 2021, 10:17

Nice, thanks much Mikey! :D

Would there also be a way to use Alt Tab, but have it simply use AutoHotKey to toggle back and forth between windows? (that it would not show / use the typical Windows Switcher UI / logic)
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Focus Previous Window

02 Dec 2021, 10:23

Yes. !Tab is a valid hotkey.
techmon
Posts: 54
Joined: 16 May 2017, 13:30

Re: Focus Previous Window

03 Dec 2021, 16:12

Thanks Mikey!

In the first post of this thread, I wrote:
In my AutoHotKey script, how can I specify to focus the previous window that was focused before the current window that is active, without utilizing Alt Tab (Windows Built-in App Switcher)?
and you replied with:

Code: Select all

F3::
winNumber = 0
WinGet, win, List
Loop, %win% {
 WinGetTitle, ttitle, % winTitle := "ahk_id " win%A_Index% ; Window title
 WinGet, proc, ProcessName, %winTitle%                     ; Window process
 WinGetClass, class, %winTitle%                            ; Window class
 winNumber += !(class ~= "i)Toolbar|#32770") && ttitle > ""
              && (ttitle != "Program Manager" || proc != "Explorer.exe")
} Until (winNumber = 2)
WinActivate, %winTitle%
Return
How would I modify that code to focus the **next** window, instead of the previous window?
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Focus Previous Window

03 Dec 2021, 16:41

What does "the next window" mean?
techmon
Posts: 54
Joined: 16 May 2017, 13:30

Re: Focus Previous Window

07 Dec 2021, 14:47

For example, if you use Alt Tab (Windows' built-in Windows Switcher), if you keep Alt held down and keep pressing Tab, it continues to goes to the "next" window in the "stack of opened windows."
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Focus Previous Window

07 Dec 2021, 15:27

There are already various AltTab simulators on the forum. viewtopic.php?p=418633#p418633
montie
Posts: 7
Joined: 01 Dec 2022, 16:13

Re: Focus Previous Window

07 Jan 2023, 14:54

mikeyww wrote:
02 Dec 2021, 09:35

Code: Select all

F3::
winNumber = 0
WinGet, win, List
Loop, %win% {
 WinGetTitle, ttitle, % winTitle := "ahk_id " win%A_Index% ; Window title
 WinGet, proc, ProcessName, %winTitle%                     ; Window process
 WinGetClass, class, %winTitle%                            ; Window class
 winNumber += !(class ~= "i)Toolbar|#32770") && ttitle > ""
              && (ttitle != "Program Manager" || proc != "Explorer.exe")
} Until (winNumber = 2)
WinActivate, %winTitle%
Return
I honestly don't get how majority of that works, but how do you make it so that you focus on the 2nd previous window though?
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Focus Previous Window

07 Jan 2023, 17:02

It's been a while, but you could fiddle with (winNumber = 2): raise to 3, etc.

Code: Select all

#Requires AutoHotkey v1.1.33
^F2::focus(2)
^F5::focus(3)

focus(nInStack) {
 winNumber := 0
 WinGet win, List
 Loop % win {
  WinGetTitle ttitle, % winTitle := "ahk_id " win%A_Index% ; Window title
  WinGet proc, ProcessName, %winTitle%                     ; Window process
  WinGetClass class, %winTitle%                            ; Window class
  winNumber += !(class ~= "i)Toolbar|#32770") && ttitle > ""
               && (ttitle != "Program Manager" || proc != "Explorer.exe")
 } Until Min(nInStack, win) = winNumber
 WinActivate % winTitle
}

Code: Select all

#Requires AutoHotkey v2.0
^F2::focus(2)
^F5::focus(3)

focus(nInStack) {
 winNumber := 0
 For each, hWnd in list := WinGetList() {
  ttitle := WinGetTitle(wTitle := 'ahk_id' hWnd)
  class  := WinGetClass(wTitle)
  proc   := WinGetProcessName(wTitle)
  winNumber += !(class ~= "i)Toolbar|#32770") && ttitle != ""
               && (ttitle != "Program Manager" || proc != "Explorer.exe")
 } Until Min(nInStack, list.Length) = winNumber
 WinActivate wTitle
}
montie
Posts: 7
Joined: 01 Dec 2022, 16:13

Re: Focus Previous Window

08 Jan 2023, 03:21

Thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GollyJer, Lamron750, septrinus, shawn_xwang and 243 guests