Search code examples
windows-10autohotkeyminimizelost-focus

Autohotkey: Auto minimize a program when it loses focus


I am using the following Autohotkey script win Win 10 to auto minimize a program when it loses focus, however it still minimizes the whole window even if I am open a sub menu or a popup options inside that same program. So how can I make respect all child windows from that program? Or any other script that does that.

#NoEnv
#Warn
var := true
SetTimer, subroutine, -1
return

subroutine:
WinWaitNotActive, ahk_exe word.exe
sleep, 2000     
if (var) {
    WinMinimize
    SetTimer, subroutine, -1
}
return

!t::SetTimer, subroutine, % (var:=not var) ? -1 : "Off" 
!x::ExitApp ; ALT+X terminates the script

Solution

  • ; autohotkey v1
    #NoEnv
    #Warn
    #Persistent
    SetTimer, subroutine, 5000
    
    subroutine()
    {
        If WinActive("ahk_class Notepad++")
        {
            ToolTip, "notepad++ active"
        }
        else
        {
            WinMinimize, ahk_class Notepad++
            ToolTip, "notepad++ not active auto minimize after 5 second"
        }
    }
    
    
    ; autohotkey v2
    SetTimer "subroutine", 5000
    
    subroutine()
    {
        If WinActive("ahk_class Notepad++")
        {
            ToolTip "notepad++ active"
        }
        else
        {
            WinMinimize "ahk_class Notepad++"
            ToolTip "notepad++ not active auto minimize after a while"
        }
    }