Use AutoHotkey to Assign a Hotkey to a Specific Window

started 5 months ago | 9 posts | 28105 views | topic rss | latest reply
  1. [updated with solution]

    I've got a function that toggles a window between being active and being minimized, so I can quickly access my favorite applications with a custom hotkey for each. I've been using this system for years, but I've started having more and more problems with regaining focus once the window is restored...

    Thankfully bobbo33 solved the problem, so I've updated this post with the full, working function. This solves the issue of restoring a window using AutoHotkey when the focus doesn't come with it - Google Chrome and Firefox being the major culprits.

    Here's my simple function:

    ToggleWinMinimize(TheWindowTitle)
    {
     SetTitleMatchMode,2
     DetectHiddenWindows, Off
     IfWinActive, %TheWindowTitle%
     {
       WinMinimize, %TheWindowTitle%
     }
     Else
     {
       IfWinExist, %TheWindowTitle%
       {
           WinGet, winid, ID, %TheWindowTitle%
           DllCall("SwitchToThisWindow", "UInt", winid, "UInt", 1)
       }
     }
     Return
    }

    You can use this function like this:

    !x::ToggleWinMinimize("Mozilla Firefox")

    This would then assign Alt+X to hide or minimize your Firefox window. Personally I use this to assign hotkeys to Google Chrome Application windows for each of my webapps, like Gmail.


    #
    howtogeek
    Reputation:175
    Posted 5 months ago

  2. Alright, I just found a hacked-up workaround that mostly works most of the time.

    WinRestore
    		WinActivate
    		WinGet, active_id, ID, A
    		ControlFocus, , ahk_id %active_id%
    		IfWinActive, ahk_class Chrome_WindowImpl_0
    		{
    			MouseGetPos, , , id, control
    			ControlFocus, %control%, ahk_id %id%
    		}

    The big problem with my workaround is that the Chrome application windows only handle the focus if the mouse is over the window once it's restored. On my laptop it's not a problem, but on my desktop it doesn't work right.


    #
    howtogeek
    Reputation:175
    Posted 5 months ago

  3. I was able to recreate your focus problem on WinXP and Firefox 3.6. To reliably fix it, replace:

    WinRestore
    WinActivate

    With this:

    WinGet, winid, ID, %TheWindowTitle%
    DllCall("SwitchToThisWindow", "UInt", winid, "UInt", 1)

    Looks like it's a Windows bug, because I was able to recreate the same problem by using a DllCall to 'ShowWindow' function directly (likely the same function that AHK uses, see http://msdn.microsoft.com/en-us/library/ms633548%28VS.85%29.aspx ). Might want to report it as a bug to the AHK developer, so they can switch to using the more reliable 'SwitchToThisWindow' function.


    #
    bobbo33
    Reputation:24
    Posted 5 months ago

  4. Oh awesome!


    #
    howtogeek
    Reputation:175
    Posted 5 months ago

  5. I updated the post to show off the full function that bobbo33 fixed, and how to use it.


    #
    howtogeek
    Reputation:175
    Posted 5 months ago

  6. This does not work with apps that have been minimized to the systray. Any way to add that function? TIA.


    #
    rfinney
    Reputation:1
    Posted 5 months ago

  7. I was just coming in say what rfinney said. With the MinimizeToTray add-on for Firefox, I can use your AHK script to minimize the window but it wont call it back up. I have a feeling there is no workaround besides incorporating some sort of "Send" and tabbing over to the systray - seems way too complicated to me. Nevertheless, I still use the script as a bosskey - #w to instantly hide Firefox, with the added bonus that it doesn't still announce itself in my taskbar.


    #
    Brady
    Reputation:75
    Posted 5 months ago

  8. You can try changing this line:

    DetectHiddenWindows, Off

    to this:

    DetectHiddenWindows, On

    It doesn't always work, though.


    #
    howtogeek
    Reputation:175
    Posted 5 months ago

  9. Why not go this route?

    !x::
    WinGet State, MinMax, ahk_class MozillaUIWindowClass
    If State = -1
    WinRestore ahk_class MozillaUIWindowClass
    else
    WinMinimize ahk_class MozillaUIWindowClass
    return


    #
    davidkingham
    Reputation:1
    Posted 5 months ago

You must log in to post.

Daily Email Updates

Just want the best posts in your inbox for free? Enter your name and email below:

Name:
Email

About This Topic

Our Network
About Productive Geek