Introduction to AutoHotKey

When you use a system for years, you get used to the annoyances. You stop noticing things that don’t make sense, or work around them. Every day, you do things that could be done a smarter way. It’s just the way things are. Macro programs, today’s topic, are just another step in customizing your workspace, similar to rearranging toolbars or icons.

But I hate macro programs!

Now, when I think “macro program,” I generally think of the thousand iterations of confusing macro support in Microsoft Office programs. Then, I think of the “actions” in Photoshop, which are actually useful. Don’t get me wrong, I’m sure somebody out there uses the Office macro tools to good effect. I’m just saying that I’m not that somebody. They’re useful in Photoshop, and I did use a program to automate fishing in an MMORPG called Tibia a while back. I had a fishing level of about 40 afterwards.

AutoHotKey takes a different approach than those examples. Instead of recording specific mouse movements, you take more of a scripting approach. Now, I’m a fan of strongly typed, compiled languages. Seeing the syntax of Visual Basic makes me nauseous. AHK, on the other hand, seems to handle scripting rather well without looking like the freak of nature that some languages are. VB, etc, I’m looking at you.

Okay, tell me more.

To assign a button to something, you follow the simple syntax of
[button]::[command]. The first command I wrote was #n::Run Notepad, which opens notepad when I press Win+N. That’s a very simple example, though. There are several built-in functions, and many that you can download as your scripting gets more complex. Comments are denoted by semicolonws, by the way.

I’ve always wanted to be able to go to a url in a website that wasn’t linked. For example, let’s say you want to go to my deviantArt page, The way to do that (in firefox) without using an extension is to highlight the url, press ctrl+c, press ctrl+t, press ctrl+v, and then press enter. When you break it down, they’re easy steps, but it’s certainly a process.

Okay, now what?

I know it’s not a very complex process, but look at what you can do with AHK:

;;Firefox: Go to selection in new tab
   Send, ^c^t
   Sleep, 500 ;wait for firefox
   Send, ^v {enter}

Now I can highlight the string above, press Win+G, and firefox will open it in a new tab. It’s not a very difficult script, and was my initial foray into multi-line scripts. As you can see, the usefulness of AHK is only limited by the uses you can brainstorm. To that end, I considered the image-saving problem mentioned in the introduction.

I see what you’ve done here…

Now, I’d like the ability to push a button and save an image. This isn’t as easy, because you can’t just save an image with an easy keystroke. Also, it’d be nice to be able to do this in IE, if only because it’s necessary to use IE every now and then. Plus, some people actually use IE as their everyday browser, and the purpose of this article is to share my script. In writing this, I used a few scripts available as references, but they didn’t do what I was looking for.

So, the typical method of saving an image involves right-clicking, selecting “save image,” and clicking okay. If it’s a duplicate filename, you get another window, have to cancel, rename the image, and then press save. So, about three to six steps. Definitely something that could be sped up. I’ll start with Firefox’s implementation.

Now for the nitty gritty…

Translating that process to keystrokes and mouse clicks, you have to press (right mouse button) v (firefox’s context shortcut for save), and then (enter). In IE, the context shortcut is s. If it’s a duplicate file, the “Save Image” (FF) or “Save Picture” (IE) window will still be open, and you’ll have to rename the file. The easiest way to do this with a low chance of collusion is a randomly generated number. The command Random R, 0, 100000 generates a number R between 0 and 100000, as you might imagine. So, to deal with an existing file in FF, you’d press (enter)(skip to end of filename, before extension)(type random number)(enter). In AHK, that translates to Send {Enter}{Ctrl Down}{Left}{Ctrl Up}{Left}%R%{Enter}. Trust me on that.


Now that you understand most of the parts, the best way to really illustrate this is to show you the final product. Observe:

;; save image using [ctrl]+[right-click]
;;           and [windows]+[right-click]
    Random R, 0, 100000 ; get a random number
    ;Msgbox %R%
    MouseGetPos,,,, Control
    IfInString, Control, Internet Explorer
        Send {RButton}s{Enter}
        Sleep, 500
        IfWinActive Save Picture
            ;Duplicate file!
            Send {Enter}{End}%R%{Enter}
    IfInString, Control, Mozilla
        Send {RButton}v{Enter}
        Sleep, 500
        IfWinActive Save Image
	    ;Duplicate file!
            ;Line split for readability
            Send {Enter}{Ctrl Down}{Left}
            Send {Ctrl Up}{Left}%R%{Enter}

If you paste this into the AHK script you’re building, you’ll get that spiffy functionality. The only problem is that if you don’t press the windows key just right, the windows menu pops up. Now that you’ve been introduced to AutoHotKey, go learn about all of the things you can do with it. It can even create forms, but I never learned how to do that. If I need a form, I’ll write a C# application.

Other uses

I own an xbox. Therefore, I have roughly a billion xbox controllers. Wanting to use one of them on my computer, I went down to Fry’s and bought a cheap ($7.50, I believe) xbox controller-to-usb converter. After installing this driver, it works perfectly. I discovered that AHK will let you assign commands to controller buttons. I even found a script that lets you use the controller as a mouse, but it was difficult to use; it was more or less impossible to move the mouse with precision. It was a simple matter, however, of assigning keyboard shortcuts used in the game I was playing (on VisualBoyAdvance) to the white, black, and start buttons on my controller. Even better, the start button still sends {start} when pressed. Also, I put some winamp functions in there that I found online. I know that winamp has global function keys, but can those also open winamp for you? Didn’t think so. If you’re interested, I’ve posted my entire script below.

Related links

Andrew Guyton

Leave a Reply