Search code examples
windowsautomationwindows-10autohotkeyhotkeys

How to create context-sensitive hotkeys when no window is active?


I want to create some hotkeys when no window is active:

enter image description here

But there's no statement like #IfNoWinActive, how to achieve this?


Solution

  • There is always an active window (the one that will get input).

    E.g. the Win+X Menu has no title but a hidden ahk_class (LauncherTipWnd). To detect it you have to add DetectHiddenWindows, On in your script:

    #NoEnv
    #SingleInstance Force
    DetectHiddenWindows, On
    
    #IfWinActive ahk_class LauncherTipWnd ; Win+X Menu
    
        F1:: Run notepad
        F2:: Send m ; starts the Device Manager
        
    #IfWinActive
    

    EDIT:

    To get the title and ahk_class of the (hidden) active window, run this script and press F1 as soon as that window is active:

    #NoEnv
    #SingleInstance Force
    
    DetectHiddenWindows, On
    
    F1::
        WinGetTitle, ActiveTitle, A
        WinGetClass, ActiveClass, A
        MsgBox, ActiveTitle: %ActiveTitle%`nActiveClass: ahk_class %ActiveClass%
    return
    
    F2::
        WinSetTitle, A, , NewTitle
        WinGetTitle, ActiveTitle, A
        MsgBox, ActiveTitle: %ActiveTitle%
    return
    
    F3::
        WinGet, ActiveExe, ProcessName, A
        MsgBox, ProcessName: "%ActiveExe%"
    return
    

    Press Ctrl+C to copy the content of the MsgBox.

    EDIT2:

    In case the active window has no title and no ahk_class you can try this:

    F1:: 
        WinGetTitle, ActiveTitle, A
        WinGetClass, ActiveClass, A
        If (ActiveTitle = "" && ActiveClass = "")
            Run notepad
        ; else If WinActive("WinTitle ahk_class WinClass", "WinText", "ExcludeTitlePart")
            ; do this
        else
            Send {F1}
    Return