AutoHotkey puts the HOT in Hotkey

Posted: November 25, 2010 by jellymann in computers, hotkeys, keyboard, mouse, tech, windows

If you’re a Windows user and you don’t know about AutoHotkey, you’re living a sad, sad life.

AutoHotkey unleashes the full potential of your keyboard, joystick, and mouse. For example, in addition to the typical Control, Alt, and Shift modifiers, you can use the Windows key and the Capslock key as modifiers. In fact, you can make any key or mouse button act as a modifier.”

AutoHotkey (AHK) works with scripts. The basic syntax is fairly easy to understand, however some of the more complicated stuff is, well, more complicated.

Lets look at an example of a simple but very useful AHK script; a hotkey that runs a program, eg. Calculator, when WindowsKey and C is pressed:

#c:: Run, calc

The hash (#) represents the Windows Key. The letter after that is the key to be pressed with the Windows Key. Thus ‘#c‘ means the hotkey is activated when the WindowsKey and C is pressed. Other modifiers include Shift, represented by a plus sign (+), Control by a caret (^) and Alt by an exclamation mark (!).

Then there’s two colons (::) directly after defining the keys. Whatever is on the right of the colons is what is executed when the keys are pressed.

Run‘ quite simply runs the command after the comma, and ‘calc‘ is the command in Windows for opening the Calculator. You can do the same thing with ‘notepad‘, ‘cmd‘, ‘iexplore‘, and loads more.

Scripts can obviously be more than one line long, for example:

Sleep 10
Run, calc

Does basically the same thing, except for the ‘Sleep‘ command, which, in this case, waits ten milliseconds before running ‘calc‘.

The ‘return‘ function ends the multi-line hotkey. If you didn’t have this here and you define another hotkey after that one, it might think that hotkey is part of the previous one.

Besides built-in Windows programs like ‘calc‘ and ‘notepad‘, other programs can be ‘Run’. For example, I use Notepad++ and use Windows Key and N to launch it using the following script:

Sleep 10
Run, E:\programs\npp\notepad++.exe

You can even ‘Run‘ websites. For example, you could make Windows Key and F open Facebook in your default browser:

Sleep 10

I have a very useful hotkey that Googles whatever I select by pressing Windows Key and G:

Send, ^c
Sleep 50

The ‘Send‘ command here “sends” Control+C to the computer as though you pressed it on the keyboard. This copies whatever text you might have selected into the clipboard.

After sleeping for fifty milliseconds (to make sure the computer is finished copying your selection) it runs Google with the copied text as the search criteria.

%clipboard%’ puts whatever is copied into the clipboard wherever you want. You can even put it in a ‘Send’ command (i.e. Send, %clipboard%)

Scripts can get very complicated, for instance, this script I have which allows me to move my Windows around without dragging from the titlebar by holding down the Windows Key (like Gnome/Ubuntu):

CoordMode, Mouse ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin%
if EWD_WinState = 0 ; Only if the window isn’t maximized
SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.

GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U ; Button has been released, so drag is complete.
SetTimer, EWD_WatchMouse, off
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D ; Escape has been pressed, so drag is cancelled.
SetTimer, EWD_WatchMouse, off
WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1 ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX – EWD_MouseStartX, EWD_WinY + EWD_MouseY – EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY

Don’t ask me to explain this, I got it from somewhere else. By the way, anything after a semi-colon (;) is a comment and is ignored by AHK.

Here are a few more that I use:

Windows Key and H toggles Show/Hide Hidden Files in Explorer:

RegRead, HiddenFiles_Status, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden
If HiddenFiles_Status = 2
RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden, 1
RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden, 2
WinGetClass, eh_Class,A
If (eh_Class = “#32770” OR A_OSVersion = “WIN_VISTA”)
send, {F5}
Else PostMessage, 0x111, 28931,,, A

Windows Key and Right Mouse Button (Yes you can combine with mouse events!) minimizes the Window under the mouse cursor:

MouseGetPos, , , WinID, control
WinGetPos, WinX, WinY, WinWidth, , ahk_id %WinID%
WinMinimize, ahk_id %WinID%

Windows Key and Q is a lot more comfortable than Alt F4, which is a commonly used hotkey for me. This simple one-liner saves me some wrist aerobics:


Check out the Tutorial on AHK scripting, the list of keys, or the list of commands that you can use.

I hope you find these scripts useful. Also, I’d love to see what scripts you guys are using, too.

Special thanks to @EttVenter for telling me about the awesomeness of AutoHotkey, and @brskln, who’s recent tweets inspired this blog post.

Read my other post for a few more scripts to make your life easier.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s