AutoHotkey Basics: Your First Script

AutoHotkey Basics: Your First Script featured illustration

πŸ“Ž This article includes 1 downloadable practice file ↓

⏱ 3 min readUpdated 11 November 2026

πŸ“˜ AutoHotkey Beginner Course Β· Lesson 2 of 6

In this article
  1. What is AutoHotkey?
  2. Basic Level: Writing Your First AutoHotkey Script
  3. Common Beginner Commands Reference
  4. Intermediate Level: Variables, Loops and Windows Automation
  5. Expert Level: GUI Applications and Advanced Automation
  6. AutoHotkey Real-World Use Cases
  7. Continue learning

AutoHotkey (AHK) is a free, open-source scripting language for Windows that lets you automate repetitive tasks, create keyboard shortcuts, and build productivity tools with just a few lines of code. Used by millions of developers, gamers, and power users worldwide, AHK can save hours of daily repetitive work.

Advertisement

What is AutoHotkey?

AutoHotkey was created in 2003 by Chris Mallett and has grown into one of the most popular Windows automation tools, with over 1.4 million registered users on the official forum. It runs as a background process and listens for your defined triggers β€” hotkeys, hotstrings, or timed events.

Basic Level: Writing Your First AutoHotkey Script

An AHK script is a plain text file with the .ahk extension. Download AutoHotkey from autohotkey.com, install it, right-click anywhere on your desktop, and choose New β†’ AutoHotkey Script.

Advertisement
; This is a comment. Lines starting with ; are ignored.

; Press Win+H to show a greeting message
#h::MsgBox Hello, World! AutoHotkey is working!

; Press Ctrl+Alt+N to open Notepad
^!n::Run Notepad

; Replace "btw" with "by the way" anywhere you type it
::btw::by the way

; Press F1 to type your email address instantly
F1::Send your.email@gmail.com

; Press Ctrl+Shift+C to copy formatted date to clipboard
^+c::
FormatTime, today,, dd-MMM-yyyy
Clipboard = %today%
MsgBox Copied: %today%
Return

Key symbols: # = Win key, ^ = Ctrl, ! = Alt, + = Shift. Double colon :: separates the trigger from the action.

Common Beginner Commands Reference

CommandWhat It DoesExample
SendTypes text or keystrokesSend Hello{Enter}
MsgBoxShows a popup messageMsgBox Done!
RunOpens a program or fileRun chrome.exe
SleepWaits (milliseconds)Sleep 1000
WinActivateBrings a window to frontWinActivate Excel
InputBoxGets user inputInputBox, name, Enter Name
ClipboardRead/write clipboardClipboard = "Hello"

Intermediate Level: Variables, Loops and Windows Automation

; Variables and input boxes
InputBox, UserName, Enter Name, What is your name?
MsgBox Hello, %UserName%! Welcome to AHK.

; Loop: type numbers 1-10 into the active window
^!L::
Loop 10 {
    Send %A_Index%{Enter}
}
Return

; Move and resize the active window to a fixed position
^!M::
WinMove, A,, 100, 100, 1200, 700
MsgBox Moved to 100,100 and resized to 1200x700
Return

; Clipboard automation: convert selected text to UPPERCASE
^!U::
Send ^c
Sleep 100
Clipboard := StrUpper(Clipboard)
Send ^v
Return

; Loop through files in a folder
^!F::
Loop, C:MyFolder*.txt
{
    MsgBox % A_Index ": " A_LoopFileName
}
Return

Expert Level: GUI Applications and Advanced Automation

; Build a fully functional GUI application
^!G::
Gui, Add, Text, x10 y10 w200, Task Description:
Gui, Add, Edit, vTaskText x10 y30 w300 h80
Gui, Add, DropDownList, vPriority x10 y120 w150, High|Medium|Low
Gui, Add, Button, Default x10 y155 w100, Save Task
Gui, Add, Button, x120 y155 w100, Clear
Gui, Show, w330 h200, Quick Task Manager
Return

ButtonSaveTask:
Gui, Submit, NoHide
if (TaskText = "") {
    MsgBox Please enter a task description!
    Return
}
FileAppend, [%Priority%] %TaskText%`n, %A_Desktop%	asks.txt
GuiControl,, TaskText,
MsgBox Task saved to your desktop!
Return

ButtonClear:
GuiControl,, TaskText,
Return

GuiClose:
Gui, Destroy
Return

AutoHotkey Real-World Use Cases

  • Data entry automation: Fill forms with one keypress using stored templates
  • Text expansion: Type ;addr to auto-expand to your full address
  • Window manager: Snap windows to precise positions with hotkeys
  • Gaming macros: Automate repetitive in-game actions (where allowed)
  • Excel helper: Launch macros, navigate ribbons, automate copy-paste workflows

Pro tip: Put #NoEnv and SetBatchLines -1 at the top of every script for best performance. Add ^!R::Reload during development to quickly restart after edits.

πŸ“Ž Practice files for this article

  • ⌨️
    Lesson 2 script (AutoHotkey v2)Double-click to run after installing AutoHotkey v2.
    ⬇ AHK Β· 310 B

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong