Alt + numeric keys for quick window operations

When I’m typing, I’m too lazy to leave the keyboard and use the mouse, so I came up with a quick window operations method.

Implementing Alt+1 for Minimize/Restore the current window, +2 for Restore, and +3 for Maximize using AutoHotKey.

!1::
    ; If no window is already selected
    ; or the selected window doesn't exist anymore,
    ; select the currently active window and minimize it:
    IfWinNotExist, ahk_id %selected_id%
    {
        If (IsWindow(WinExist("A")) || WinActive("ahk_class ArtRage 3"))
            WinGet, selected_id, ID, A
        else
        {
            MsgBox, No window selected
            return
        }
    }
    WinGet, WinState, MinMax, ahk_id %selected_id%
    If WinState = -1    ; the selected window is minimized 
    {
        WinRestore
        selected_id := ""      
    }
    else
        WinMinimize
return

; This checks if a window is, in fact, a window,
; as opposed to the desktop or a menu, etc.
IsWindow(hwnd){
    WinGet, s, Style, ahk_id %hwnd%
    return s & 0xC00000 ? (s & 0x100 ? 0 : 1) : 0
}

!2::
    WinRestore,A
return

!3::
    WinMaximize,A
return

Code explanation

  1. !1::: This line defines a hotkey, where ! represents the Alt key, and 1 is the numeric key. So, pressing Alt + 1 triggers this hotkey.
  2. IfWinNotExist, ahk_id %selected_id%: This conditional statement checks if a window with the specified ID (selected_id) exists. If not, it selects the currently active window.
  3. Inside the conditional block:
    1. WinGet, selected_id, ID, A: Retrieves the ID of the currently active window and stores it in the variable selected_id.
    2. WinGet, WinState, MinMax, ahk_id %selected_id%: Retrieves the minimize/maximize state of the selected window.
  4. If WinState = -1: Checks if the selected window is minimized.
    1. If true, it restores the window with WinRestore and clears the selected_id.
    2. If false, it minimizes the window with WinMinimize.
  5. IsWindow(hwnd): This function checks if the specified window handle (hwnd) corresponds to an actual window (as opposed to the desktop or a menu). It examines the window style.
  6. !2:: WinRestore,A: Defines another hotkey (Alt + 2) to restore the active window.
  7. !3:: WinMaximize,A: Defines another hotkey (Alt + 3) to maximize the active window.

However, I always feel that these quick window operations are a bit cumbersome. If you have a better solution, feel free to leave a message and let me know.

Scroll to Top