微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取当前窗口 5 秒后关闭当前窗口即使我在这 5 秒内切换到不同的窗口代码差不多了

如何解决获取当前窗口 5 秒后关闭当前窗口即使我在这 5 秒内切换到不同的窗口代码差不多了

我有这个代码,如果您按住 ESC 键,它将获取当前窗口,然后在 5 秒的睡眠时间后关闭该窗口。

但是我希望能够在按下此热键后切换到不同的窗口。因此,在这 5 秒期间,即使我打开另一个窗口,此窗口也会在后台关闭。目前,如果我快速切换到另一个窗口,它会关闭那个窗口。

我的最终目标是设置一个计时器来杀死一个“打开的”特定窗口,然后它在后台关闭,即使我在它执行之前切换到新窗口(因为睡眠计时器)。

非常感谢任何帮助。 谢谢!

代码

    $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,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

解决方法

对于任何有兴趣的人,我想通了。

$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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。