Created
August 18, 2024 04:30
-
-
Save surajsharma/45b1081f4a17918409ccab960cb6628d to your computer and use it in GitHub Desktop.
hotkeys ahk2 win11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires AutoHotkey v2.0 | |
#SingleInstance force | |
ScriptStartModTime := FileGetTime(A_ScriptFullPath) | |
SetTimer(CheckScriptUpdate, 100, -1) ; 100 ms, highest priority | |
; Hotkeys for different window types | |
^Space::ToggleSpecificWindow("terminal") | |
^!Space::ToggleSpecificWindow("browser") | |
+Space::ToggleSpecificWindow("notepad++") | |
; Add more hotkeys here for other window types | |
ToggleSpecificWindow(windowType) { | |
; Get a list of all windows | |
windowList := WinGetList() | |
allTitles := [] | |
targetWindow := 0 | |
; Determine the window title condition based on the window type | |
condition := "" | |
exePath := "" | |
switch windowType { | |
case "terminal": | |
condition := (title) => SubStr(title, 1, 1) = "[" or SubStr(title, 1, 6) = "Zellij" or InStr(title, "cmd.exe") | |
case "browser": | |
condition := (title) => InStr(title, "Chrome") or InStr(title, "Firefox") or InStr(title, "Edge") | |
case "notepad++": | |
condition := (title) => InStr(title, "Notepad++") | |
exePath := "C:\Program Files\Notepad++\notepad++.exe" | |
; Add more cases here for other window types | |
default: | |
MsgBox("Unknown window type: " . windowType) | |
return | |
} | |
; Loop through each window | |
for window in windowList { | |
; Get the title of the current window | |
title := WinGetTitle(window) | |
; If the title is not empty, add it to our array | |
if (title != "") { | |
allTitles.Push(title) | |
} | |
; Check if this window matches our condition | |
if (condition(title)) { | |
targetWindow := window | |
break ; Exit the loop after finding the target window | |
} | |
} | |
if (targetWindow) { | |
if (WinActive("ahk_id " targetWindow)) { | |
; If the window is active, send it to the back | |
WinMoveBottom("ahk_id " targetWindow) | |
; Activate the window that was below this one | |
SendInput("{Alt down}{Tab}{Alt up}") | |
} else { | |
; If the window is not active, activate it and maximize | |
WinActivate("ahk_id " targetWindow) | |
WinMaximize("ahk_id " targetWindow) | |
} | |
} else { | |
; If no matching window was found, try to run the application if exePath is set | |
if (exePath != "") { | |
try { | |
Run exePath | |
} catch as err { | |
MsgBox("Failed to run " . windowType . ". Error: " . err.Message) | |
} | |
} else { | |
; If exePath is not set or the application failed to run, show a list of all windows | |
if (allTitles.Length > 0) { | |
titlesText := "No " . windowType . " window found.`n`nAll Window Titles:`n`n" | |
for title in allTitles { | |
titlesText .= title . "`n" | |
} | |
MsgBox(titlesText) | |
} else { | |
MsgBox("No windows found.") | |
} | |
} | |
} | |
} | |
; Function to list all window titles (can be called separately if needed) | |
ListWindowTitles() { | |
windowTitles := [] | |
; Get a list of all windows | |
windowList := WinGetList() | |
; Loop through each window | |
for window in windowList { | |
; Get the title of the current window | |
title := WinGetTitle(window) | |
; If the title is not empty, add it to our array | |
if (title != "") { | |
windowTitles.Push(title) | |
} | |
} | |
; Join all titles into a single string, separated by newlines | |
allTitles := "" | |
for title in windowTitles { | |
allTitles .= title . "`n" | |
} | |
; Show the titles in a message box | |
MsgBox("Window Titles:`n`n" . allTitles) | |
} | |
CheckScriptUpdate() { | |
global ScriptStartModTime | |
curModTime := FileGetTime(A_ScriptFullPath) | |
if (curModTime == ScriptStartModTime) | |
return | |
SetTimer(CheckScriptUpdate, 0) | |
loop { | |
try { | |
Reload() | |
} | |
catch { | |
Sleep(300) | |
result := MsgBox("Reload failed.", A_ScriptName, 2) | |
switch result { | |
case "Abort": ExitApp() | |
case "Ignore": break | |
; "Retry" will continue the loop | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment