Lesson 1

From AutoHotkey

Jump to: navigation, search

This is part 1 of the AHK Tutorial.

Contents

[edit] Introduction to AHK Scripts

[edit] Creating a script

First, if you haven't you should download and install the latest version of AHK.

Right click in the background of a window or the desktop and go to New >> AutoHotkey Script. Alternately, you can simply save a text file with the extension .ahk

Now that we have a script file, we will begin scripting.

[edit] Hotkeys

Now that you have a blank AHK Script in front of you, it's time to start scripting. The first aspect of AutoHotkey we will explore will be hotkeys.

A "Hotkey" is a key or set of keys that performs a predefined function. These functions can often be done via some other, more indirect mechanism, such as using a menu, typing a longer command, and/or using a pointing device. By reducing such sequences to a few keystrokes, this can often save the user time. AutoHotkey 's primary function is the creation, utilization and modification of hotkeys.

Though there are many commands availible in AHK, this tutorial will be using only the run and msgbox commands to illustrate the different types of hotkeys.

Before we begin, it is important to note that certain keys on the keyboard can be represented in AHK as symbols. Here is a list of the most common:

# = Windows Key
^ = Control
! = Alt
+ = Shift

[edit] Single Line Hotkey

First is a simple one line hotkey. Put this line into your blank AHK script, save it, and double click it to run it.

#g::run, www.google.com

Now, hold the windows key and press g. Google will be launched with your default browser.

How does this work? The part before the :: is the key combination that launches the action, which is the part after the ::. The # stands for the windows key, so Windows-g launches the action to run Google.

So...there are four parts to this one line. They are:

  1. The hotkey "#g"
  2. The hotkey indicator "::"
  3. The command "run"
  4. The parameter "www.google.com"

The indicator tells AHK that the key sequence before it will execute commands that follow it.

And the parameter of the command tells AHK more specific information about the command. (In this example, it tells AHK what you what to run. Parameters will be different for every command.)

[edit] Multi Line Hotkey

Next we will look at a multi line hotkey. Leaving that google line in your script, add the following:

^!1::
msgbox, You pressed crtl-alt-1, Microsoft Word will now be launched.
run, winword.exe
return

Save the script, and double click it. You should get an error saying that the script is already running. Say Yes. Now hold Crtl and Alt and press 1. A message box will appear, notice that word is launched after you press OK on the message box. Msgboxs cause the current thread to pause until they are dismissed.

With multi line hotkeys, think of the hotkey decleration ^!1:: as the instruction "When the user presses that key combination start executing the code below here." It will stop executing when a return is encountered. If the first instruction is on the same line as the hotkey declaration, only that line is run, and no return is necessary. (That is a single line hotkey like the Google example we just did.)

[edit] Multi Key Hotkeys

The hotkeys we have explored so far have been one key, with modifiers (ctrl, shift, alt, and win). Another way to do hotkeys is with &. Add this to our script:

CapsLock & c::run, calc.exe

Save, and Run, (click Yes to the error). Now hold capslock and press c and Windows Calculator will be launched. Notice that after the hotkey is launched CapsLock is left in its previous state. Using this method, any key can become a modifier for hotkeys. (Unfortunately, Capslock & ^!c::run calc.exe does not act as planned. See Ampersand Modified Hotkeys for an advanced trick to get around this)

[edit] Left or Right modifiers

To specify an action only to a specific modifier (like left-shift, or right-alt) you can use < and >. Here are some examples:

<!a::msgbox left-alt and a was pressed
>!a::msgbox right-alt and a was pressed
>!+a::msgbox right-alt, either shift, and a was pressed
>^<!a::msgbox, right-control, left-alt, and a was pressed

[edit] Up Hotkeys

To launch a command when a key is released follow the key name with UP. Here is an example:

LWin UP::msgbox, The left windows key was just released.

Now, you will notice the message box will pop up when you release the key, but also notice that the left windows key now does not open the Start menu. Also, hotkeys like Windows-D will no longer work. This issue brings us to the next topic:

[edit] The ~ Prefix

If you want the hotkey to be launched, but still tell the operating system and the active program the key was pressed, put a ~ before the hotkey. For example:

LWin UP::msgbox, This hotkey disabled the lwin key, including windows hotkeys
like win-d and win-r
~LWin UP::msgbox, The LWin will not open the start menu (which is usually
launched upon release of the Win keys), but the hotkeys will work now.

Here is another example:

RButton & Up::msgbox, the up arrow was pressed while the right mouse button was down

This hotkey completely disables the right mouse button. The only thing the right mouse button can do is be used to launch this hotkey. But if we put the ~ in front of it it works fine.

~RButton & Up::msgbox, the up arrow was pressed while the right mouse button was down

[edit] Further reading

For more information on hotkeys, please see the Documentation

[edit] Hotstrings

Hotstrings are mainly used to expand abbreviations as you type them, but they can be used to launch any scripting action (which we will learn about later). They are similar to hotkeys, except that they are launched by a sequence of keys.

Put the following into the script we have been creating, and run it:

::btw::by the way

Type "btw " (thats "b t w space") and it will be deleted and "by the way" will replace it. The space is an ending character. The default ending characters are: -()[]{}':;"/\,.?!`n `t (note that `n is Enter, `t is Tab, and there is a plain space between `n and `t). You can change the ending characters for hotstrings using: #Hotstring EndChars -()[]{}:;'"/\,.?!`n `t where you add or remove the chars you want.

[edit] * (Asterisk) Option (no ending char needed)

If you don't want to have to type an ending character for the hotstring to activate, use the **asterisk option**. Here is an example:

:*:j@::johnsmith@example.com

This "j@" will be replaced by "johnsmith@example.com" as soon as you type the "@", this is useful because you don't to put a space after your email in online forms.

[edit] O option (omit ending char)

[edit] C option (case sensitive)

[edit] B0 option (no backspace)

[edit] Long Replacements

[edit] Hotstring commands

[edit] Hotstring Example: 4700 common English misspellings

[edit] Further Reading

[edit] Continue on to Lesson 2

Personal tools