Beginner Tutorial | AutoHotkey

Table of ContentsThe BasicsDownloading and installing AutoHotkeyHow to create a script you cannot merge commands how to find the help file on your computer hotkeys & HotstringsKeys and their mysterious symbolsWindow specific hotkeys/hotstringsMultiple hotkeys/hot springs per fileExamplesSending KeystrokesGamesRunning Programs & WebsitesCommands vs. Functions()Code blocksVariablesWhen to use percent setting user input other Examples?ObjectsCreating ObjectsUsing ObjectsOther Helpful GoodiesThe mysterious square brackets ending your AHK version trial and ErrorIndentationAsking for HelpOther links1 - The Basics

Before we begin our journey, let me give some advice. Throughout this tutorial, you will see a lot of text and a lot of code. For optimal learning power, it is advised that you read the text and try the code. Then, study the code. You can copy and paste most examples on this page. If you get confused, try reading the section again. a. Downloading and installing AutoHotkey

Before learning to use AutoHotkey (AHK), you will need to download it. After downloading it, you may possibly need to install it. But that depends on the version you want. For this guide, we will use the Installer since it is the easiest to set up.

Text instructions: Go to the AutoHotkey Homepage: https://www.autohotkey.com/Click Download: https://www.autohotkey.com/download/ahk-install.exeDuring installation of AutoHotkey, you will be asked to choose from UNICODE or ANSI. In short, you would probably want to choose UNICODE. It has support for non-English letters and numbers (characters). Keep going until you see an Install button. Once done, great! Continue on to section b.

For video instruction, watchInstall and Hello World on YouTube.b. How to create a script

Once you have AutoHotkey installed, you will probably want it to do stuff. AutoHotkey is not magic, we all wish it was, but it is not. So we will need to tell it what to do. This process is called "Scripting".

Text instructions: Right-Click on your desktop. Find "New" in the menu. Click "AutoHotkey Script" inside the "New" menu. Give the script a new name. It must end with a .ahk extension. For example MyScript.ahkFind the newly created file on your desktop and right-click it. Click "Edit Script".A window should have popped up, probably Notepad. If so, SUCCESS!

So now that you have created a script, we need to add stuff into the file. For a list of all built-in commands, functions, and variables, see section 5.

Here is a very basic script containing a hotkey that types text using the Send command when the hotkey is pressed:^j::Send, My First Scriptreturn

We will get more in-depth later on. Until then, here's an explanation of the above code: The first line: ^j:: is the hotkey. ^ means Ctrl, j is the letter J. Anything to the left of:: are the keys you need to press.  The second line: Send, My First Script is how you send keystrokes. Send is the command, anything after the comma (,) will be typed. The third line: return. This will become your best friend. It literally stops code from going any further, to the lines below. This will prevent many issues when you start having a lot of stuff in your scripts. Save the File. Double-click the file/icon on the desktop to run it. Open notepad or (anything you can type in) and press Ctrl and J.Hip Hip Hooray! Your first script is done. Go get some reward snacks then return to reading the rest of this tutorial.

For video instruction, watch Install and Hello World on YouTube.c. You cannot merge commands

When you are making your code, you might have the urge to put several commands on the same line or inside of each other, don't. In section 5 we'll talk about why it doesn't work as you might expect and what you can do instead. How to find the help file on your computer

There are a few ways to do this, I'll assume you have it installed to the default locations:

Method 1:Find the Start menu or Start Orb on your screen, usually in the lower left. Click Programs or All Programs. Find Auto Hotkey in the list. You should then see Auto Hotkey Help File. Click it. Done!

Method 2:Go to your desktop. Find My Computer or Computer. Open it. Go into your hard drive that contains Auto Hotkey. Probably C:\ drive. Search within all Program Files folders for Auto Hotkey. Look for AutoHotkey.chm or a file that says Auto Hotkey and has a yellow question mark on it. Done! 2 - Hotkeys & Hotstrings

What is a hotkey? A hotkey is a key that is hot to the touch. II... Just kidding. It is a key or key combination that the person at the keyboard presses to trigger some actions. For example:^j::Send, My First Scriptreturn

What is a hot spring? Hotstrings are mainly used to expand abbreviations as you type them (auto-replace), they can also be used to launch any scripted action. For example:::ftw::Free the whales

The difference between the two examples is that the hotkey will be triggered when you press Ctrl+J while the hot string will convert your typed "ftw" into "Free the whales".

"So, how exactly does a person such as myself create a hotkey?" Good question. A hotkey is created by using a single pair of colons. The key or key combo needs to go on the left of the::. And the content needs to go below, followed by a return.

Note: There are exceptions, but those tend to cause confusion a lot of the time. So it won't be covered in the tutorial, at least, not right now.Esc::MsgBox, Escape!!!!return

A hot spring has a pair of colons on each side of the text you want to trigger the text replacement. While the text to replace your typed text goes on the right of the second pair of colons.

Hotstrings, as mentioned above, can also launch scripted actions. That's fancy talk for "do pretty much anything". Same with hotkeys.::btw::MsgBox, You typed btw.return

A nice thing to know is that you can have many lines of code for each hotkey, hot string, label, and a lot of other things we haven't talked about yet.^j::MsgBox, Wow!MsgBox, There are notepads.exeWinActivate, Untitled - NotepadWinWaitActive, Untitled - NotepadSend, 7 lines{!}{Enter}SendInput, inside the CTRL{+}J hotkey.return. Keys and their mysterious symbols

You might be wondering "How the crud is I supposed to know that ^ means Ctrl?!". Well, good question. To help you learn what ^ and other symbols mean, gaze upon this chart: SymbolDescription#Win (Windows logo key)!Alt^Ctrl+Shift&An ampersand may be used between any two keys or mouse buttons to combine them into a custom hotkey.

(For the full list of symbols, see the Hotkey page)

Additionally, for a list of all/most hotkey names that can be used on the left side of a hotkey's double-colon, see List of Keys, Mouse Buttons, and Joystick Controls.

You can define a custom combination of two (and only two) keys (except joystick buttons) by using  &  between them. In the example below, you would hold down Numpad0 and then press Numpad1 or Numpad2 to trigger one of the hotkeys: Numpad0 & Numpad1::MsgBox, You pressed Numpad1 while holding down Numpad0.returnNumpad0 & Numpad2::Run, notepad.return

But you are now wondering if hotstrings have any cool modifiers since hotkeys do. Yes, they do! Hotstring modifiers go between the first set of colons. For example::*:ftw::Free the whales

Visit Hotkeys and Hotstrings for additional hotkey and hot string modifiers, information, and examples. b. Window specific hotkeys/hotstrings

Sometimes you might want a hotkey or hot string to only work (or be disabled) in a certain window. To do this, you will need to use either of these fancy commands with a # in front of them:#IfWinActive#IfWinExist

These special commands (technically called "directives") create context-sensitive hotkeys and hotstrings. Simply specify a window title. But in some cases, you might want to specify criteria such as HWND, group, or class. Those are a bit advanced and are covered more in-depth here: The WinTitle Parameter & the Last Found Window.#IfWinActive Untitled - Notepad#Space::MsgBox, You pressed WIN+SPACE in Notepad.return

Post a Comment (0)
Previous Post Next Post