SEARCH

How-To Geek

Keyboard Ninja: Assign a Hotkey to any Window

When I need to perform a repetitive task such as checking my email or switching to an open IM window, the quickest option is to assign a hotkey directly to the window, so I can toggle the window minimized/restored with nothing more than a single keystroke.

How did I accomplish this? AutoHotkey, a small scripting framework that allows you to automate anything in Windows. Before we begin, I’m going to assume that you’ve downloaded and installed it.

I’ve created a small function that you can add to a script which will do the hard work of finding and toggling the window. All you have to do is assign the hotkeys you want at the top of the script.the Keyboard Ninja

The first thing you will need to do is download the script and save it somewhere useful. You should be able to simply double-click on the script to start it, and then you will notice a new tray icon (The green one with the H)

image

Right-click on the icon, and choose Edit This Script from the menu. You’ll have to add in your own hotkeys since none are defined in the script, so let’s go take a look at the script…

; —————————————————————–
; Declare Your hotkeys in this section
; —————————————————————–
;  —- these are samples —-
;  !j::ToggleWindow(“- Mozilla Firefox”) – Win + J
;  #c::ToggleWindow(“SecureCRT”)         – Win + C
;  !x::ToggleWindow(“cmd.exe”)           – Alt + X

; —————————————————————–
; Function for toggling windows – Do not edit
; —————————————————————–
ToggleWindow(TheWindowTitle)
{
    SetTitleMatchMode,2
    DetectHiddenWindows, Off
    IfWinActive, %TheWindowTitle%
    {
        WinMinimize, %TheWindowTitle%
    }
    Else
    {
        IfWinExist, %TheWindowTitle%
            WinActivate
        Else
        {
            DetectHiddenWindows, On
            IfWinExist, %TheWindowTitle%
            {
                WinShow
                WinActivate
            }
        }
    }
}

It might be a little complicated for some of you, but the only thing we need to do is add in some hotkey lines. You’ll notice that there are a number of sample hotkey lines defined already, but commented out.

Hotkeys are defined in this format:

<keys>::ToggleWindow(“Partial Window Title String”)

For special keys, you’ll use one of the following, which can be combined. (get more information at Autohotkey documentation)

# Windows key
! Alt
^ Control
+ Shift
< Use Left key (for instance <! means left Alt key only)
> Use Right key (for instance >! means right Alt key only)

So for instance, if you wanted to trigger the keyboard shortcut of Ctrl+Alt+F and assign it to Firefox, you would use the following:

^!f::ToggleWindow(“- Mozilla Firefox”)

Personally, I try to use keyboard shortcuts that don’t require me to lift my hands off the keys. I simply use Alt+J assigned to Firefox because I can hit that combination without moving my hands at all.

When you are done editing the script, just save it and then go up to the H icon again, and choose the “Reload This Script” option, which will load all of your changes. If there was a problem, you’ll get an error message, and you can always use Exit to stop the script entirely.

image

You’ll have to decide which key combinations work best for you. My advice is to add one or two at a time, and get used to using them. Within a few days you’ll wonder how you ever lived without them. You should also read up on AutoHotkey as well… it can do much more than just this.

Note: The function ToggleWindowHide function in the script is for the more adventurous – it will toggle the window between hidden and restored… extremely useful for command prompt windows. Essentially gives me Tilda or YaKuake on Windows.

Download geek_autohotkey.ahk (Autohotkey script)

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 06/29/07

Comments (2)

  1. FredPT

    Thanks for this, its an awesome utility!

    If you don’t mind I’ll write some of my scripts:

    #IfWinActive ahk_class CabinetWClass
    MButton::Send {backspace}

    This one checks if the active window is an explorer window and assigns middle click to have the same function as backspace, going to the parent folder, pretty useful for all-mouse browsing.

    I also have some programing functions (for PHP and Flash), for example:

    ::_if::
    Send if () {{}{}}{Left 4}
    return

    If I type “_if” the program will replace that by “if () {}” and will place the typing cursor between the “()” (moving the cursor 4 times to the left) , allowing me to continue coding right away. I also use this for my mail (using _@).

    Again, thank you for showing me this little beauty :)

  2. Lee

    The first sample in the screenshot ( ! j ) …. that should be Alt + J

Enter Your Email Here to Get Access for Free:

Go check your email!