Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AwesomeLemon/ea98c53886fa5dcc808169e647cc6b72 to your computer and use it in GitHub Desktop.

Select an option

Save AwesomeLemon/ea98c53886fa5dcc808169e647cc6b72 to your computer and use it in GitHub Desktop.
Switch between instances of a program via Alt + Backtick (Ubuntu-like functionality for Windows)
#Requires AutoHotkey v2.0
!`::
{
KEY := "SC029" ; grave/tilde key (backtick) scancode
; Build candidate list for the current app (special-case Explorer by class)
win_class := WinGetClass("A")
exe := WinGetProcessName("A")
if (exe = "explorer.exe")
win_list := WinGetList("ahk_exe" exe " ahk_class" win_class)
else
win_list := WinGetList("ahk_exe" exe)
if (win_list.Length < 2)
return
; Start at second-most-recent (index 2). Index 1 is current.
idx := 2
WinActivate("ahk_id" win_list[idx])
; Wait for the initial backtick to be released (debounce)
KeyWait(KEY)
; While Alt is held, each tap of backtick advances the selection
while GetKeyState("Alt", "P")
{
KeyWait(KEY, "D") ; wait for next press
if !GetKeyState("Alt", "P")
break
if GetKeyState("Shift", "P")
idx := (idx = 1) ? win_list.Length : idx - 1
else
idx := (idx = win_list.Length) ? 1 : idx + 1
WinActivate("ahk_id" win_list[idx])
; Debounce so one press = one step
KeyWait(KEY)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment