0
votes

I have this code that if you hold down ESC key it will get the current window and then close that window after 5 seconds of sleep time.

However i want to be able to switch to a different window after pressing this hotkey. So during this 5 second period this window will close in the background even if i open another window. Currently if i quickly switch to a different window it closes that one instead.

My end goal it to set a timer to kill a specific window "the one open" and then it closes in the background even if i switch to new window before it executes (because of the sleep timer).

Any help much appreciated. Thanks!

Code:

    $Escape::                                           ; Long press (> 0.5 sec) on Esc closes window - but if you change your mind you can keep it pressed for 3 more seconds
    KeyWait, Escape, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel                                       ; timeout, so key is still down...
        {
            WinGet, winid
            SplashTextOn,,150,,`nRelease button to close %x%`n`nKeep pressing it to NOT close window...
            KeyWait, Escape, T3                         ; Wait no more than 3 more sec for key to be released  
            SplashTextOff
            If !ErrorLevel                              ; No timeout, so key was released
                {
                    sleep 2000 
                    PostMessage, 0x112, 0xF060,,, A     ; ...so close window      
                    Return
                }
                                                        ; Otherwise,                
            KeyWait, Escape                             ; Wait for button to be released
                                                        ; Then do nothing...            
            Return
        }
        
        Send {Esc}
Return
1

1 Answers

0
votes

For anyone who is interested i figured it out.

$Escape::                                               ; Long press (> 0.5 sec) on Esc closes window - but if you change your mind you can keep it pressed for 3 more seconds
    KeyWait, Escape, T0.5                           ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel                                       ; timeout, so key is still down...
        {
            WinGet, active_id, ID, A
            theWindow = ahk_id %active_id%
            SplashTextOn,,150,,`nRelease button to close %x%`n`nKeep pressing it to NOT close window...
            KeyWait, Escape, T3                         ; Wait no more than 3 more sec for key to be released  
            SplashTextOff
            If !ErrorLevel                              ; No timeout, so key was released
                {
                    sleep 2000 
                    if WinExist(theWindow )
                    WinClose ;
                    Return
                }
                                                        ; Otherwise,                
            KeyWait, Escape                             ; Wait for button to be released
                                                        ; Then do nothing...            
            Return
        }
        
        Send {Esc}
Return