<

AutoHotkey - Getting Started

Published on
249,434 Points
7,634 Views
28 Endorsements
Last Modified:
Approved
Community Pick
Joe Winograd
CERTIFIED EXPERT
50+ years in computer industry
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
GOLD 2020
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to additional resources for EE members who want to learn more.

Introduction


I have published numerous articles and five-minute video Micro Tutorials here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles and videos has brief information describing where to download the product and how to install it. I have also answered many questions with a reference to AutoHotkey as a component of the solution. As I was about to write another article with an AutoHotkey program and the how-to-download-and-install information, I decided it would be better to publish a more comprehensive write-up on the process, including some background on the language.


Background and History


AutoHotkey is a free (in all senses of the word), open source product, published under the GNU GPL (General Public License, Version 2, June 1991). It is a fork of the popular AutoIt language. AutoHotkey started out as a keyboard shortcuts and mouse clicks/movements capability (hence, "Hotkey" in the name), but has been enhanced into a robust programming/scripting language.


AutoHotkey itself has forked several times, with the release at AutoHotkey.com considered by devotees of the language to be the active/primary branch. If you'd like to learn more about the history of the language, I recommend The AutoHotkey Foundation and Our History pages at its website and the Wikipedia AutoHotkey article.


Download and Installation


With a decent computer and Internet connection, the download and installation of AutoHotkey should take less than a minute. At the time of publishing the most recent update to this article, here are the steps:


(1) Visit the AutoHotkey site and click the Download button:



Clicking that will give you this:



(2) As a new user, click the Download Current Version button. The exact dialog that you get from this depends on your browser and its settings. For example, you may see something like this:



In any case, run the installer.


(3) If you have UAC on, you will see this:



Sidebar: If you're wondering how I captured the UAC dialog box, see my EE article, How to disable the secure desktop when User Account Control (UAC) prompts for elevation.


(4) If it is a new installation, clicking the Yes button on the UAC dialog will give you this:



The Express Installation is a good choice for new AutoHotkey users.


(5) If you already have AutoHotkey on the system, it will display the installed version and offer to upgrade it, such as this:



Close all running scripts before you click the Upgrade to button, although if you don't, it will warn you:


close-running-scripts.jpg


(6) In a very short period of time (seconds, not minutes), you will see this Installation complete dialog:



That's it! You now have AutoHotkey installed on your computer. It owns the file extension AHK, which is a plain text file with the program in it. You may use whatever text editor you want to create an AHK source code file, even the built-in Notepad (just be sure to save it as a file type of AHK). Since AutoHotkey is associated with AHK files, double-clicking on one in Windows/File Explorer (or whatever file manager you use) will cause AutoHotkey to run that script.


Hello World


To get started, let's code the classic Hello World program in AutoHotkey. Open up whatever text editor you want and copy/paste this one line of code in it:

 

MsgBox,0,First AHK script,Hello World


Save the file as HelloWorld.ahk (or whatever you want to call it, as long as the file type is AHK) and then double-click it in your file manager. AutoHotkey will execute it and you'll see this:


Hello-World.jpg


You'll likely use the MsgBox command often, so you may want to study its syntax. In fact, all the AutoHotkey commands and functions are well-documented, with complete syntax and good examples.


Compiler


A standard installation of AutoHotkey also installs a compiler, which converts an AHK source code file into a stand-alone executable (an EXE file). Note that it is not a "compiler" in the traditional sense, i.e., it does not compile the source code into object code. It simply bundles the AutoHotkey interpreter along with the source code of the script into an EXE file. That's why the "compiler" is actually called Convert .ahk to .exe (see below).


The EXE files created by compiling typically run on all versions (and bit levels) of Windows going back to Windows 2000 (although there may be some limitations based on the Windows version). Indeed, I have run these EXE files on W2K, XP/32-bit, Vista/32-bit, W7/32-bit, W7/64-bit, and W8/64-bit. In addition, I just ran the HelloWorld.exe (created on W7) on the W10/64-bit Technical Preview (Build 10061, the latest release at the time of publishing this article) — it worked perfectly:


HelloWorld-EXE-on-W10-64bit.jpg


The easiest way to compile is to right-click on an AHK file in your file manager and select Compile Script from the context menu:


context-menu-Compile-Script.jpg


For more compiling options, such as selecting a custom icon or compressing the EXE file, you may run the compiler separately (the installer creates a shortcut to Convert .ahk to .exe in the AutoHotkey Program Group):


compiler.jpg


Notice in the context menu screenshot above that underneath the Compile Script item is an Edit Script item. This loads the script in whatever text editor you have configured for it. The default is Notepad, but you may change it by modifying this registry key:

HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Edit\Command

For example, to change it to Notepad++ (assuming the standard installation location for it), modify the value of that key to this:

"C:\Program Files\Notepad++\notepad++.exe" "%1"


However, rather than modifying the registry, you may simply open the AHK file with your preferred text editor by using Windows/File Explorer or whatever file manager you use (that's what I do).


Component Object Model (COM)


One of the most powerful AutoHotkey features is its native/built-in support for the Component Object Model (COM). It is extremely valuable in creating automation scripts for use with Microsoft Office apps, such as Excel and Word (similar to what can be done with VBA). The documentation begins at the ComObjActive() call and runs through the ComObjValue() call. Here's a small (working) script that shows Excel COM usage (of course, change the InputFile value to test it on your system):


InputFile:="c:\temp\test.xlsx"
WorksheetNames:=""
oWorkbook:=ComObjGet(InputFile)
WorkheetsCount:=oWorkbook.Worksheets.Count
Loop,%WorkheetsCount%
  WorksheetNames:=WorksheetNames . oWorkbook.Worksheets(A_Index).Name . "`n"
MsgBox,4160,Worksheet Names,Number of Worksheets: %WorkheetsCount%`n`n%WorksheetNames%`n`n
ExitApp


Using the MsgBox command discussed earlier, it displays the number of worksheets in the Excel file and shows each worksheet name. Here's a screenshot of sample output:



Graphical User Interface (GUI)


Another very powerful AutoHotkey feature is its ability to create programs with a Graphical User Interface (GUI). The documentation on it is quite good, with many examples. Here's a modification of Example #2 there with some enhancements:


Menu,FileMenu,Add,E&xit,FileMenuExit
Menu,MenuBar,Add,&File,:FileMenu
Menu,HelpMenu,Add,&About,HelpMenuAbout
Menu,MenuBar,Add,&Help,:HelpMenu
Gui,Menu,MenuBar
Gui,Add,Text,,First name:
Gui,Add,Text,,Last name:
Gui,Add,Edit,vFirstName ym
Gui,Add,Edit,vLastName
Gui,Add,Radio,vAgeRange xm,Under 21
Gui,Add,Radio,Checked x+20,21-65
Gui,Add,Radio,x+20,Over 65
Gui,Add,Button,xm Default,&OK
Gui,Add,Button,x+20,&Cancel
Gui,Show,,GUI Test
Return

ButtonOK:
Gui,Submit
MsgBox,4160,GUI Example,First Name: %FirstName%`nLast Name: %LastName%`nAge Range: %AgeRange%
ButtonCancel:
FileMenuExit:
GuiClose:
GuiEscape:
ExitApp

HelpMenuAbout:
Gui,+OwnDialogs
MsgBox,4160,About,Version 1.0
Return


The script shows these GUI features:


Text
• Data entry (Edit) fields
Menu bar with File and Help menus
File menu has Exit pick
Help menu has About pick
About dialog box is modal
• All menu picks have keyboard shortcuts (underlined in the screenshot below)

Radio buttons in a 3-button group

OK and Cancel buttons


Here's what it looks like when you run it:



It's extremely simplistic, but shows the power of GUI in a small amount of code.

Other Resources

For more examples of AutoHotkey code in full, working scripts, please see the many articles and five-minute video Micro Tutorials that I published here at EE. This is the current list:


Articles

Test IP Addresses with PING
Answer Skype Calls with Bluetooth Headset
How to copy the short link of an ID at Experts Exchange to the clipboard with a single keystroke

How to solve the problem of incorrect System Uptime being reported when a system has been up for a long time
How to create an on/off toggle to mute the system audio/sound with a single mouse click or single keystroke

How to download number of Views, Endorsements, Points for Experts Exchange Articles and Videos

How to move all windows on one monitor to another monitor with a single keystroke

AutoHotkey Switch-Case

Keyboard shortcuts (hotkeys) to move mouse to monitors in a multi-monitor configuration - AutoHotkey Script

Exclude words when converting to "Title Case" - AutoHotkey Script

Automatically download files from the web - AutoHotkey Script


Videos

On-Screen Display for CapsLock State (On or Off)
On-Screen Display for CapsLock State (On or Off) - Enhanced
Check for and download updated file - Example - FileZilla
How to download number of Views, Endorsements, Points for Experts Exchange Articles and Videos--Demo
ArticlesVideosEE: Download statistics on Experts Exchange Articles and Videos - Demo of Enhancements


Conclusion

My purpose in writing this article is to provide an easy, quick, yet comprehensive Getting Started  guide to which I can refer EE members. Going into more detail than presented in this article is not necessary, as there is an excellent, thorough AutoHotkey Beginner Tutorial at the AutoHotkey site, along with my articles and videos mentioned above.


If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

28
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free