08 August 2007

The Purring Keyboard

Here I am working in a hospital library every day, so close to people in pain, and yet often neglecting my own body's elaborately evolved electrochemical semaphore of tissue distress. I am one of the vast multitude who toil with computers, clacking away for hours at keyboards designed in the 19th century. And like my suffering comrades I frequently end my day with a stridently complaining forearm or a racking headache. For many it is a gimpy wrist, a spavined neck, or a trounced back. For all those Windows users whose well-being the computer has cruelly compromised, there is balm in Gilead. (We know that you Mac users are here on earth to do good to others by showing them the right path. What the others are here for you haven't been able figure out yet, but Steve will be announcing that at the next Mac Expo.)

AutoHotKey is a little program that allows you to create scripts or macros to automate common activities. The key to its extraordinary power is its ability to hook onto events of the Windows operating system, most notably keystrokes and mouse operations. This versatile “hotkey” ability allows you to:

  • Automate almost anything by sending keystrokes and mouse clicks. You can write a mouse or keyboard macro by hand or use the macro recorder. (Have more than one email signature? Use macros to switch between them.)
  • Create hotkeys for keyboard and mouse. (Start a program, enter today's date with a key combination.)
  • Expand abbreviations as you type them. (Tired of typing MeSH, UpToDate, autochthonous?)
  • Remap keys and buttons on your keyboard and mouse. (Got a better use for that feckless F12 key?)
There are macro programs aplenty out there, many of them costing a fair amount. I have tried all of them and have had nightmares about imaginary ones. Nothing has worked as well as AutoHotKey, and it's free of charge.

The only problem is that AutoHotKey's documentation, while remorselessly thorough, is not designed for the beginner. If I were running the company, a simple beginner's interface would be added to the package. However, for anyone with Windows experience, basic AutoHotKey scripting can be absorbed quickly. There is no need to learn how to create the really elaborate macros. Leave that to the programming aficionados of the AutoHotKey Forum.

If you're game to try, start with the Quick Start Tutorial and read over the Hotkeys and Hotstrings sections. After downloading AutoHotKey you create your scripts in one or more text files with a .AHK extension.

How am I using AutoHotKey?
Here are a few of my favourite shortcuts, hotstrings, abbreviations, and key remappings. On any day my sore arm is spared a great deal of typing and hundreds of mouse clicks. If you are bored already, have a look at the section on AutoHotKey and PubMed. You may perk up.

It is very easy to automate the opening of a file with a simple key combination. The two examples below start my acquisitions spreadsheet and whatever Microsoft Word file I have been working on last.

The first two symbols of the macro text stand for Ctrl-Alt, followed by a letter. Pressing these three keys 'runs' the macro, in the first example Ctrl-Alt-A opens a Microsoft Excel spreadsheet on my network's S: drive. The hotkey is separated from the command by two colons.

^!a::Run S:\njmhsl\sbgh\acquisitions sbgh.xls

To start Word I type Ctrl-Alt-W. The script adds /mFile1 to the program path. This cool DOS-style Microsoft switch opens the last Word file you have worked on. Very handy.

^!w::Run C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE /mFile1

Of course, you can also do this sort of thing by going into the Properties of a Windows icon and adding a key combination to the Shortcut Key box. But what I have shown above is just the most basic AutoHotKey function. An experienced user can program in a host of other actions to be performed with a key combination.

These are simple macros that expand a short form into full text. The standard code is colon-colon-abbreviation-colon-colon-result. Special codes, such as an asterisk, may be inserted between the first two colons. This means that the abbreviation will be expanded to the corresponding full text as soon as it is typed.

:*:asa::as soon as possible
:*:btw::By the way, `

These hotstrings replace the abbreviation with the text that comes after the colons. When there is no asterisk between the first two colons, you must hit the space bar, a period, or Enter to execute the replacement macro. It's up to the user whether to script for instant replacement or not.

The following is just a small sample of my list of little shortcuts. These one-, two- or three-letter macros can really ease common typing headaches, such as repetitive boilerplate, or other long or difficult-to-type text (e.g., internal upper case as in UpToDate).

For macros of this type, I use the semicolon as the first element of the abbreviation. This allows me to employ any combination of letters, numbers or symbols for my macro, without concern for conflict. For example, if you create an auto-replace hotstring to change 'and' to 'AND' you would rarely be able to type the word 'and' in lower case. Upon hitting the spacebar you would be converting it to upper case automatically. Adding a preceding semicolon, as in the PubMed example in the next section, avoids this problem. The semicolon is easy to find on the keyboard, but other initial macro symbols like the slash or vertical bar would work as well.

:*:;al::academic librarian
:*:;hs::Health Sciences Libraries
:*:;my::My NCBI
:*:;th::Thank you for your cooperation.
:*:;us::United States

Employing the same method, signatures and other boilerplate can be entered easily. Use the opening and closing parenthesis to surround text. In the following example, typing semicolon-j-s instantly produces this signature.

Joan Smith
General Hospital

AutoHotKey and PubMed
I spend a great deal of my time in PubMed. I prefer command line searching, avoiding the mouse as much as I can. Searching PubMed professionally is unbearable without macros. The script below includes a few of my favourites. AutoHotKey makes PubMed purr like a kitten.

For example, instead of typing AND in a search box, I only need to type a semicolon and the word 'and' in lower case. The macro erases what I have just typed and replaces it with AND in upper case plus a space so that I can just continue with the next word. The same goes for OR, NOT, [MAJR], [TI], etc. Admit it, have you ever cursed quietly while trying to find those ridiculous square brackets on the keyboard? How often do you hit the wrong one? I have been touch typing for 40 years and I still can't get them right.

I'm very fond of my ';5' macro. It's a lot easier to type than selecting Limits in PubMed and then clicking all over the place just to limit to English language, last 5 years.

The junk macro 'jk' gets rid of some publication types I don't want for certain searches.

(The question mark before the asterisk means the hotstring will be triggered even when it is inside or adjacent to another word. It therefore doesn't matter whether [TI] is attached to or separated by a space from the preceding word. The asterisk means that an ending character (e.g. space, period, or enter) is not required to trigger the hotstring.)

:*:;5::AND english[la] AND "last 5 year"[dp]
:?*:;ab::[TIAB] `
:*:;and::AND `
:*:;jk::NOT (letter[PT] OR comment[PT] OR editorial[PT] OR news[PT] OR interview[PT])
:?*:;mh::[MH] `
:?*:;mj::[MAJR] `
::*:;not::NOT `
:*:;or::OR `
:?*:;ti::[TI] `
:?*:;tw::[TW] `

I would love to hear from any readers who have found ways to automate standard PubMed functions. My most hated feature of PubMed? Having to check those dreadful little boxes to select articles. Many years ago, in the old DOS version on CD-ROM, a citation could be selected merely by pressing the space bar. If only I could get AutoHotKey to do that ...

I use auto-expansion to add diacritics to any text. The scripts below all work the same way. If I type 'xxaf' anywhere, it magically replaces itself with 'â'. And so on. The abbreviations are mnemonic: 'f' stands for circumflex, 'g' for grave, 'u' for umlaut, etc. The double x makes sure that the diacritics show up only when you want them.


This feature is amazingly easy. Find out the AutoHotKey term for the key, type two colons, and then the code for the action you want to perform instead of the key's normal function. Use sparingly. As long as you have the AutoHotKey script loaded, your new choice will replace the key's normal functioning.

I like to use the far right keys on the number pad to close things. The minus key now acts the same as Alt-F4. The plus key is remapped to Ctrl-F4, and the numpad Enter key is Ctrl-W, allowing me to close Firefox tabs with ease. This saves an incredible number of hand movements.

For Internet browsing, the right Alt key, normally unused, is remapped to Page Up. That makes it easy in Firefox to alternate between Space Bar to move down one screen and Right Alt to move one screen up. You don't have to move your hand at all. Finally, the unnecessary F1 and F12 keys are remapped to change the font size in Firefox without requiring the usual Ctrl-Minus and Ctrl-Equal Sign.







As you can see, AutoHotKey is an inexhaustible treasure of techy tweaking for geeks of the strict observance as well as for the geek novitiate. Don the habit, fall on your knees, and make your solemn vows now. Here are some pilgrimages to make for more inspiration:

AutoHotKey Script Listing

10 Ways to Use AutoHotKey to Rock Your Keyboard

Skrommel's AutoHotKey Scripts


Alex said...

Hi, Mark, I'm Alex, the author of Jitbit Macro Recorder. It would be great if you could take a look at our software too and post a couple of thoughts maybe. Count on a free reg.code for it (or any other Jitbit tool) ;) Thanks