AutoHotKey Notes, Recipes & Examples

Code RecipeDecember 27, 20211 min read
autohotkeyautomationwindowsscriptingproductivity

Overview

AutoHotkey (AHK) is a Windows-based scripting language focused on keyboard and mouse automation. This guide serves as a quick reference for common patterns and techniques.

Basic Examples

1. Binding to Function Keys

Using F1/F2 with and without bracket syntax for executing keyboard commands.

2. Variable Expansion

Two methods for working with variables:

  • Inline interpolation
  • Format() function method

3. Global Counters

Incrementing persistent values across script executions.

4. Boolean Evaluation

Conditional logic with true/false variables.

5. Function Calls

Creating functions and returning values.

6. Parameterized Functions

Passing arguments to custom functions.

7. Text Input

Sending text and triggering keyboard actions programmatically.

Advanced Recipes

Variable Duration Sleeping

Functions accepting min/max millisecond parameters for randomized delays between actions.

Sleep, Random, %minDelay%, %maxDelay%

Time-Based Execution

Scripts that trigger at specific hours using the A_Hour built-in variable.

if (A_Hour >= 9 and A_Hour < 17) {
    ; Execute during business hours
}

Probability-Based Actions

Random number generation determining whether code executes.

Random, probability, 0, 100
if (probability < 50) {
    ; 50% chance of execution
}

Use Cases

AHK excels at:

  • Repetitive data entry automation
  • Custom keyboard shortcuts
  • Text expansion (snippets)
  • Window management
  • Mouse automation for testing
  • Gaming macros (use responsibly)

Note

Since AHK scripting happens infrequently, these recipes serve as quick refreshers. Full code examples are available on GitHub.