Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

AutoHotkey - Getting Started

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT
Published:
Updated:
Edited by: Andrew Leniart
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.
Article Update 20-Dec-2023:
This article is about AutoHotkey v1. All the articles and videos that I've published here at Experts Exchange and all the AutoHotkey code that I've posted in answers to questions here at EE are about AutoHotkey v1. However, as the AutoHotkey home page currently says, "AutoHotkey v2 has been released and will be considered the default/main version. We are in a transition period: the website and forums will be updated accordingly. Please see the announcement for more information." Notwithstanding that statement, I prefer v1 and will continue to post and support v1 code here at EE.
End Article Update

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 gives you this:



(2) As a new user, you may prefer the new v2 version (currently, v2.0). However, as noted in the 20-Dec-2023 Article Update at the top of this article, I prefer the v1 version (currently, v1.1). So, even though it says "deprecated", that is the version on which the rest of this article is based, including all the code posted. When pressing the "Download v1.1 (deprecated)" button. the exact dialog that you get depends on your browser and its settings. Here is an example of what you may see with the Chrome browser (but the version number may be different):



In any case, save that setup.exe file, which is the installer.


(3) Run the installer downloaded in step (2) above. 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.


It says that the app is from an unknown publisher, as the AHK community that publishes the executable does not use a code-signing certificate on the setup file. However, you should feel comfortable installing it as long as you downloaded it from the real AutoHotkey website.

(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, W8/64-bit, W10/64-bit, and W11. They all worked perfectly!


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 (and submenu) picks have keyboard shortcuts (underlined in the screenshot below)

Radio buttons in a 3-button group (also with underlines)

OK and Cancel buttons (also with underlines)


Here's what it looks like when you run it on a Windows 10 system:


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


Sidebar: The underlines on the first letter of the menus and buttons (and the underlines on the File and Help submenu items) are because that feature has been enabled in Windows (it is disabled by default). Here's how to enable it:
• Run Control Panel
• Open Ease of Access Center
• Click Make the keyboard easier to use
• Scroll down and tick the box that says Underline keyboard shortcuts and access keys
End Sidebar

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

30
15,323 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT

Comments (19)

Commented:
Thank's. I'll try that
Andrew LeniartIT Professional, Freelance Journalist, Certified Editor
Author of the Year 2019
Distinguished Expert 2020

Commented:
Great article Joe. Very helpful.
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.
I cheat and do it by capturing the entire action in an Oracle Virtual Machine installation from the Host side. The other benefit to that method is that I need not concern myself about ever accidentally showing sensitive files and/or folders when zooming around in Internet explorer while creating video tutorials :-)

Cheers..
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Andrew,
Thanks for the compliment — I appreciate it! Your idea sounds like a good one for folks who run virtual machines — thanks for the feedback. Regards, Joe

Commented:
Excellent. Thanks Joe.
Carlos
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
You're welcome, Carlos, and thanks to you for the compliment. If you take a moment to click the thumbs-up icon at the bottom of the article, I''ll appreciate it — as you can see, you'll be endorsement #20. :) Regards, Joe

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.