Article: AutoHotKey 2.0

Home Page


Consultancy

  • Service Vouchers
  • Escrow Service

Shop



Programming
  • Articles
  • Tools
  • Links

Search

 

Contact

 

PHPinfo


$_SERVER







AutoHotKey, focussing on migration from AHK1.1 to AHK2.0

category 'KB', language AHK2, created 16-Aug-2025, version V1.0, by Luc Pattyn

with the assistance of Microsoft Copilot.


License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author.

Disclaimer: This work is provided as is, without any express or implied warranties or conditions or guarantees. You, the user, assume all risk in its use. In no event will the author be liable to you on any legal theory for any special, incidental, consequential, punitive or exemplary damages arising out of this license or the use of the work or otherwise.


AutoHotkey v2.0 Migration Notes

Introduction

AutoHotkey (AHK) is a free, open-source scripting language for Windows that allows users to automate tasks, create custom keyboard shortcuts, and build lightweight utilities. It can be downloaded from https://www.autohotkey.com/.

AutoHotkey v2.0 marks a major evolution in the scripting language, bringing it closer to conventional programming standards. This article summarizes key differences from v1.1, offers practical migration advice, and shares personal insights from a recent conversion experience.

Syntax Improvements

  • Hotkeys are now functions, with their code enclosed in curly braces (unless single-line).
  • Literal strings must use double quotes.
  • Commas and percent signs are gone — cleaner syntax overall.
  • Examples:
    • Send("{RButton Down}")
    • KeyWait("RButton")
    • activeClass := WinGetClass("A") (“A” means Active Window)

Think in terms of C, C#, or even VB.NET — with real variables, functions, and objects.

Key Differences to Watch

  • := is for assignment, = is for comparison.
  • Arrays are now 1-based.

Migration Strategy

Manual conversion is entirely doable. Here's a recommended approach:

  1. Replace all /* ... */ block comments with line comments.
  2. Install AutoHotkey v2.0 — do not allow it to install v1.1.
  3. Take your v1.1 script and wrap it in a giant comment block using /* at the top and */ at the bottom.
  4. Iteratively move the /* line downward, fixing issues as AHK v2.0 reports them.

This method avoids juggling two files and allows partial execution of converted code immediately.

Best Practices

  • Include #Warn in your header for better feedback.
  • Use static variables for local state — avoid global unless truly necessary.
  • Take advantage of AHK’s built-in Help system — it supports multiple windows for side-by-side reference.

Recommended Header Settings

#Requires AutoHotkey 2.0    ; Heavily relying on the improved syntax rules
#Warn                       ; Enable warnings to assist with script writing
;#NoTrayIcon                 ; Suppress tray icon for background utilities
#SingleInstance force       ; Ensures only one instance of the script is running
SendMode "Input"            ; Preferred for speed and reliability
SetTitleMatchMode(2)        ; Allows partial window title matching

Closing Thoughts

AutoHotkey v2.0 is a leap forward in clarity, consistency, and capability. While the transition requires effort, the payoff is a cleaner, more maintainable scripting environment. This article reflects my own journey — and I hope it helps others take the leap with confidence.



Perceler

Copyright © 2012, Luc Pattyn

Last Modified 21-May-2025