Basic Hotkeys: The Gateway to AutoHotkey Script Writing Addiction (Beginning Hotkeys Part 1)

With AutoHotkey, It’s So Simple to Add Hotkeys to Your Windows System That You May Not Be Able to Quit

How to Get Addicted to Programming

One of the features that distinguishes AutoHotkey is its simple implementation of Hotkeys. The creation of Hotkeys is so easy with AutoHotkey that anyone can quickly add them to their Windows computer. No programming knowledge or experience required! This is one of the reasons that AutoHotkey is becoming so popular. There are an unlimited number of tricks which anyone can immediately make a part of their everyday Windows computing.

*          *          *

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*          *          *

The straightforward assignment of Hotkeys (and its close sibling Hotstrings) distinguishes AutoHotkey from other Windows automation programs such as AutoIt. In fact, adding Hotkeys is so painless with AutoHotkey it could be called the gateway drug to programming. Many people start by inserting a few favorites tasks as single-line Hotkeys, then, little by little, begin making small modifications to their scripts. Before they know it, they’re writing full-blown applications which combine many labor saving functions. This accounts for the growing use of AutoHotkey by people who are completely new to any type of programming.

The Basic AutoHotkey Hotkey—What Is a Hotkey?

The format for the basic AutoHotkey Hotkey consists of a keyboard combination followed by a set of double colons (::) followed by an AutoHotkey command (e.g. key combination::command). Once loaded, whenever the keyboard combination is pressed simultaneously, the command is executed. (This differs from AutoHotkey Hotstrings where the keys are pressed sequentially.)

Beginner's Guide to AutoHotkeyNote: If a Hotkey combination needs more than one command, then, after leaving the space to the right of the double colons blank, the first command starts on the next line—terminating the entire set of command lines with a lone Return command on the last line.

When a new user starts with AutoHotkey, defining a Hotkey is often the first step . The Hotkey is the structural backbone of most scripts—especially for beginners. These keyboard combinations can run programs or scripts, open favorite Web pages, quickly access Windows folders, pop-up informational windows, manipulate and insert text, or initiate virtually any other Windows action. The possibilities for AutoHotkey Hotkeys are virtually unlimited.

Run Programs with Hotkeys

Starting out you may want to add a few one-line Hotkeys to simplify the launching of a few common Windows tasks. For example, suppose you would like to quickly run a particular program (e.g. Notepad) with a simple key combination. Adding one line to a text file with the AHK extension (e.g. filename.ahk) and running it while the main AutoHotkey program is loaded makes this possible:

!n::run Notepad

The character combination !n tells AutoHotkey to activate the command following the double colons (::) whenever the ALT key and n key are pressed simultaneously. In this case, responding to the Run command the text editor Notepad opens. It doesn’t get any easier than that.

Note: Once the free AutoHotkey software is downloaded and installed on your Windows computer, it automatically loads—ready to use—every time you boot up your system.

Open a Web Page with a Hotkey Combination

Suppose you want the Hotkey combination to open a favorite Web page:

!l::run www.computoredge.com

Now, ALT+l (!l) opens the main ComputorEdge Web page with your default browser.

Use Hotkeys for Easy Access to Needed Folders

Maybe there is an often-used folder you would like to open with a Hotkey:

!p::run C:\Users\Jack\Pictures

The added Hotkey combination (!p) opens my Pictures folder.

Tip: Each Hotkey line (or snippet) can appear almost anywhere in an AutoHotkey script—except within the auto-execute section at the beginning of the script. The auto-execute stops processing commands if it encounters any Hotkey or Hotstring structure. Regardless of location, AutoHotkey scans the entire script loading any Hotkeys found. Avoid placing Hotkeys inside functions or other subroutines. You will get an error if AutoHotkey finds a Hotkey inside a function and a Hotkey may cause unusual behavior inside a subroutine (Label).

Informational Pop-up Windows

Hotkeys can be used to run any AutoHotkey command:

!i::MsgBox, Hello, World!

This time ALT+i (!i) runs the AutoHotkey MsgBox command opening a pop-up window saying “Hello, World!”

Insert Text with the Send Command

Do you want to enter today’s date into any Windows word processing or Web text editing field?

!d::Send %A_MMMM% %A_DD%, %A_YYYY%

When the script is loaded while the main AutoHotkey program is running (the default condition for an AutoHotkey software installation), the ALT+d (!d) key combination now executes the Send command inserting today’s date (e.g. February 08, 2016) into any text editing window with built-in date variables (A_MMMM for the month name, A_DD for the two-digit day of the month, and A_YYYY for the four digit year). Variables are evaluated by placing them between two percent signs (%variable%).

When it comes to building AutoHotkey Hotkeys, not all key are created equal.

Hotkey Modifiers

Most single keys on your keyboard may be used as a Hotkey, but in the vast majority of cases, you will want to create a key combination using at least one modifier with another standard keyboard character. Otherwise, you will lose the original function of the single key. For example, if you turn the letter n into a Hotkey, then you will no longer be able to use the n key in its normal manner—to type “n” from your keyboard. (There is a way to force the key to continue to do its original job plus the Hotkey action (the ~ option), but in most cases, you want to avoid using single common letters or numbers as Hotkeys.)

The four primary AutoHotkey Hotkey modifiers are CTRL (^), ALT (!), SHIFT (+), and the WIN (Windows logo microsoft_key) key (#). There are other symbols which can be used in conjunction with your Hotkey combinations (e.g. $, ~, >, etc.—for later discussion), but these are most often options used in special circumstances. These main modifiers either individually modify a standard key (!n) or act in combination with one or more other modifiers (^!n)—all keys pressed simultaneously.  (See the AutoHotkey Keylist for the names of special keys and mouse buttons.)

Unfortunately, assigning AutoHotkey Hotkey modifiers is not as simple as just picking the one that makes the most sense to you. Whenever a Hotkey combination is assigned it most often blocks the same assigned key combo (or shortcut) found within either Windows or a specific Windows program. There are some key combinations you will want to avoid. In any case, it’s a good idea to test a combination to see its effect (if any) before implementation.

Tip: I often use CTRL and ALT together (e.g. ^!n) since it is rare that the two will occur in any other Windows or program shortcut—except, of course, CTRL+ALT+DELETE.

The CTRL (Control) Key — ^

The CTRL (Control) key represented in AutoHotkey by the caret (^) is one of the most common modifiers used in Windows system and Windows program shortcuts. CTRL+C (copy) and CTRL+V (paste) are two regularly accessed shortcuts employed throughout Windows, as well as, within AutoHotkey scripts. Assigning either one as an AutoHotkey Hotkey would cause its original function to stop working. Because of these possible conflicts, I avoid using the lone CTRL with only one other letter or number character. Fix this by adding another modifier (e.g. ^!c or ^+c).

The ALT (Alternate) Key — !

When pressed, the ALT key often accesses the top menu bar in a Windows program. Depending upon what action your AutoHotkey Hotkey activates, this may or may not be a problem. Again, adding another modifying key often resolves any issues.

Tip: I’ve found that using a modifier in combination with one of the function keys (F1, F2, F3, …) is usually safe. (See the AutoHotkey Keylist for the names of special keys and mouse buttons.) In fact, there are good reasons for blocking CTRL+F4 and ALT+F4 with AutoHotkey Hotkeys since either one can instantly close a Web browser window causing the loss of work-in-process.

The following Hotkeys block those dangerous F4 browser shortcuts:

!F4::
^F4::MsgBox Oops! You pressed ALT+F4 or CTRL+F4

Those are crazy dangerous Windows shortcuts.

The  SHIFT Key — +

I only use the SHIFT key (+) as an additional modifier for other modifying keys. Otherwise,  SHIFT+any letter (as happens when capitalizing a letter in any document) runs the risk of activating the Hotkey action at the wrong time.

The WIN (Windows Logo microsoft_key) Key — #

Both Windows 8 and Windows 10 contain a ton of shortcuts which use the WIN (Windows logo microsoft_key) key (#). Unless you want to deliberately block a particular Windows key combination, avoid using the WIN key as a modifier on its own. Adding CTRL, ALT, SHIFT, or any combination of those three, will greatly reduce the chance of a conflict.

These are the Hotkey basics, but we will continue to pursue more ways to tailor your keyboard combinations, plus techniques for getting more out of your mouse and/or joystick buttons. There is plenty more to explore.

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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