Disable the hotkey when no window is active Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Disable the hotkey when no window is active

07 May 2020, 08:31

I use code:

Code: Select all

^Backspace::
             WinClose A
return
to close the active window of every application.

However, I noticed that when no application window is active, pressing this hotkey causes the Windows shutdown window to appear. How to modify the above code so that the hotkey doesn't work when the window of any application isn't active?
mshafer1
Posts: 49
Joined: 16 Jul 2017, 14:49
GitHub: mshafer1
Contact:

Re: Disable the hotkey when no window is active

07 May 2020, 08:44

Unfotunately, from AHK's perspective there's never a case of "no active window" - however you can detect if the desktop is the "active window" by checking for "ahk_exe Explorer.exe ahk_class WorkerW"

So, your script would change to be something like (untested):

Code: Select all

^Backspace::
    IfNotWinActive, ahk_exe Explorer.exe ahk_class WorkerW
    {
        WinClose ; uses last found window
    }
    return
User avatar
boiler
Posts: 6131
Joined: 21 Dec 2014, 02:44

Re: Disable the hotkey when no window is active

07 May 2020, 08:44

You can use a directive so the hotkey is not even active when the desktop is the active "window".

Code: Select all

#IfWinNotActive, Program Manager ahk_class Progman
^Backspace::WinClose A
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 08:54

@boiler, it doesn't work correctly. Yes, it doesn't display the Windows shutdown window when the Desktop is active. But sometimes there is a situation that the application window is maximized, but inactive. For example, after pressing the Start bar or closing another application. Then pressing this hotkey also displays the Windows shutdown window.
vsub
Posts: 476
Joined: 29 Sep 2015, 03:39

Re: Disable the hotkey when no window is active

07 May 2020, 09:24

How about

Code: Select all

^Backspace::
Keywait,Backspace
WinGetTitle,Title,A
If Title !=
WinClose,A
Return
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 09:31

@vsub, works OK, but now displays the Windows shutdown window when the Desktop is active.
User avatar
boiler
Posts: 6131
Joined: 21 Dec 2014, 02:44

Re: Disable the hotkey when no window is active

07 May 2020, 09:31

Rostov wrote:
07 May 2020, 08:54
@boiler, it doesn't work correctly. Yes, it doesn't display the Windows shutdown window when the Desktop is active. But sometimes there is a situation that the application window is maximized, but inactive. For example, after pressing the Start bar or closing another application. Then pressing this hotkey also displays the Windows shutdown window.
This addresses the above issue. File Explorer is identified as a title to exclude so you can still close File Explorer windows if you want (i.e., don't include them with the rest of the explorer.exe processes).

Code: Select all

GroupAdd, ProgManGroup, ahk_exe explorer.exe,,, File Explorer
#IfWinNotActive, ahk_group ProgManGroup
^Backspace::WinClose, A
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 12:39

@boiler, it still doesn't work. When I use this hotkey with a maximized but inactive window, the Windows shutdown window appears again. When I use this hotkey with an active Desktop, all icons disappear from it.
vsub
Posts: 476
Joined: 29 Sep 2015, 03:39

Re: Disable the hotkey when no window is active

07 May 2020, 12:46

What do you see when you run this when the desktop is active(when the shut down window usually appear)

Code: Select all

^Backspace::
Keywait,Backspace
WinGetTitle,Title,A
If Title !=
Msgbox,% Title
Return
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 13:08

@vsub, using this hotkey, when the Desktop is active, I get a window with the words "Program Manager". The same happens when I use this hotkey in any other application - I get a window with the name of the active application.

The effect I want appears after using this code:

Code: Select all

#IfWinNotActive ahk_exe explorer.exe
^Backspace::
             WinClose, A
return
but then I can't close any Windows Explorer windows with this hotkey. How to prevent this?
User avatar
boiler
Posts: 6131
Joined: 21 Dec 2014, 02:44

Re: Disable the hotkey when no window is active

07 May 2020, 13:50

Rostov wrote:
07 May 2020, 13:08
The effect I want appears after using this code:

Code: Select all

#IfWinNotActive ahk_exe explorer.exe
^Backspace::
             WinClose, A
return
but then I can't close any Windows Explorer windows with this hotkey. How to prevent this?
Seems strange that the above does what you want but the following doesn't, because it looks like the only difference is that Explorer windows aren't included. Are you saying the above code works in all the instances that you said (maximized but inactive window, active Desktop and all icons disappear) but the one below doesn't?

Code: Select all

GroupAdd, ProgManGroup, ahk_exe explorer.exe,,, File Explorer
#IfWinNotActive, ahk_group ProgManGroup
^Backspace::WinClose, A
vsub
Posts: 476
Joined: 29 Sep 2015, 03:39

Re: Disable the hotkey when no window is active  Topic is solved

07 May 2020, 14:09

How about this then

Code: Select all

#IfWinNotActive,Program Manager
^Backspace::
Keywait,Backspace
WinGetTitle,Title,A
If Title !=
WinClose,A
Return
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 14:29

@vsub, now it works excellent! Thank You. :)
But could you explain this piece of code to me?

Code: Select all

If Title !=
This expression looks unfinished. What is its function in this code?
vsub
Posts: 476
Joined: 29 Sep 2015, 03:39

Re: Disable the hotkey when no window is active

07 May 2020, 14:50

If the window have a title,close it(there is no such visible window,all visible windows have some kind of title)
Firefox - something something - close it
Notepad something - close it
No title - leave it alone

This check is needed because the taskbar for example is also some kind of window but it have no title
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 14:59

What about the Desktop? Isn't it also a kind of window with the title 'Program Manager'?
gregster
Posts: 5146
Joined: 30 Sep 2013, 06:48

Re: Disable the hotkey when no window is active

07 May 2020, 15:00

Rostov wrote:
07 May 2020, 14:29

Code: Select all

If Title !=
This expression looks unfinished. What is its function in this code?
It is valid syntax, but traditional (or legacy) syntax: See If (traditional). (It compares the contents of the variable Title to an empty string (blank).)
As such it is not longer recommended - better use If (expresssion) syntax instead.

The equivalent in if-expression syntax would be:

Code: Select all

If (Title != "")
vsub
Posts: 476
Joined: 29 Sep 2015, 03:39

Re: Disable the hotkey when no window is active

07 May 2020, 15:21

Rostov wrote:
07 May 2020, 14:59
What about the Desktop? Isn't it also a kind of window with the title 'Program Manager'?
The explorer.exe have many visible windows that are not actually windows and they have no name except "Program Manager"
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 15:27

OK and one more question. Forgive my inquisitiveness, but I want to understand everything thoroughly. :)
What did you use this piece of code for?

Code: Select all

Keywait, Backspace
Could the code not work without waiting for the Backspace key to be released?
vsub
Posts: 476
Joined: 29 Sep 2015, 03:39

Re: Disable the hotkey when no window is active

07 May 2020, 15:32

I prefer code to be executed when I release the button when it is a keyboard combination
It is waiting for the backspace to be released
You can remove it if you want
Rostov
Posts: 30
Joined: 09 Apr 2020, 07:57

Re: Disable the hotkey when no window is active

07 May 2020, 15:40

Now I understand everything. Thanks again for your help! :)

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], hasantr, songdg, Xtra and 47 guests