Best way to trigger hotkey when over a window, and not the desktop? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Best way to trigger hotkey when over a window, and not the desktop?

24 Sep 2023, 16:32

I am creating a hotkey like so:

Code: Select all

HotKey setting_Trigger, ShowGUI
But I don't want to show the GUI when the mouse is over the desktop. I want the GUI to only show when the mouse is over an application.

My initial thought is, in my ShowGUI function, I can use MouseGetPos and WinGetClass to check the class of the window under the mouse. If it is one of WorkerW, Shell_TrayWnd, or Shell_SecondaryTrayWnd then it's over the desktop. Code example below.

But I'm wondering if there is a better way to achieve what I am after?

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

HotKey "^d", ShowGUI

ShowGUI(ThisHotKey)
{
    Local CMM := A_CoordModeMouse
    A_CoordModeMouse := "Screen"
    MouseGetPos(&mousePositionLeft, &mousePositionTop, &windowUnderMouse_windowID)

    windowUnderMouse_class := WinGetClass("ahk_id " windowUnderMouse_windowID)

    if (windowUnderMouse_class == "WorkerW") or (windowUnderMouse_class == "Shell_TrayWnd") or (windowUnderMouse_class == "Shell_SecondaryTrayWnd")
    {
        ; over desktop
        return
    }

    msgbox "not over desktop"
}
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: Best way to trigger hotkey when over a window, and not the desktop?

24 Sep 2023, 16:54

andymbody wrote:
24 Sep 2023, 16:43
Would this help?
#HotIf
Yes but I’m not exactly sure how. Like I don’t dee a hotif not over desktop option.
ludamo
Posts: 44
Joined: 25 Mar 2015, 02:21

Re: Best way to trigger hotkey when over a window, and not the desktop?

24 Sep 2023, 17:22

One way to do it is to form a group of Desktop windows and then use #HotIf not WinActive.

Code: Select all

GroupAdd("gDesktop", "ahk_class Progman")		; Desktop	
GroupAdd("gDesktop", "ahk_class WorkerW")		; Explorer Thread. WorkerW comes to the foreground when you press Win + D (minimize all windows)
GroupAdd("gDesktop", "ahk_class Shell_TrayWnd")					; TaskBar
GroupAdd("gDesktop", "ahk_class Shell_SecondaryTrayWnd")		        ; 2nd TaskBar

#HotIf not WinActive("ahk_group gDeskTop")

niCode
Posts: 305
Joined: 17 Oct 2022, 22:09

Re: Best way to trigger hotkey when over a window, and not the desktop?  Topic is solved

24 Sep 2023, 17:33

Perhaps:

Code: Select all

HotIf NotOverDesktop
HotKey setting_Trigger, ShowGUI
HotIf


NotOverDesktop(*) {
    static windowsToAvoid := 'WorkerW Shell_TrayWnd Shell_SecondaryTrayWnd'
    MouseGetPos(,, &windowUnderMouse_windowID)

    if InStr(windowsToAvoid, WinGetClass('ahk_id ' windowUnderMouse_windowID))
        return false

    return true
}
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: Best way to trigger hotkey when over a window, and not the desktop?

25 Sep 2023, 16:21

@ludamo Thanks! Your way works but for other reasons I am going with the other answer. I realized I want to show a notification when the user tries to show the GUI over the desktop. So for that I have to trigger everywhere and then check where the mouse was.

@niCode Thank you!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: RussF, wilkster and 42 guests