AutoHotKey Notes, Recipes & Examples.

Dec 27, 2021

AutoHotkey (AHK) is a niche scripting language build around keyboard & mouse IO. The official AHK Documentation is enormously helpful & provides many wonderful examples. Unfortunately, at the time of this writing it only runs on Windows.

What follows are some quick recipes to get up and running in AHK quickly–I find myself writing AHK scripts just infrequently enough to forget everything. Examples demonstrated here can be found on github If you have any interest in seeing some “real world” AHK scripts, check out my AutoHotKey Scripts Repository.

Index

Examples

Note: In most of these examples, I bind the script to F1 function key.

  1. Bind Script to Function Key
  2. Expanding Variables
  3. Increment Global Value
  4. Evaluate Boolean
  5. Function Call & Return Value
  6. Parameterized Function Call
  7. Type Text Press Enter

Recipes

  1. Sleep for a Variable Duration (Parameterized)
  2. Execute Task Based on Time of Day
  3. Execute Task With Percent Chance of Execution

Bind Script to Function Key

bind script example

with brackets

F1::
{
    Msgbox, hello world
    return
}

without brackets

F2::
Msgbox, hello world 2
return

Expanding Variables

variable expansion example

inline

F1::
{
    name_variable := "john"
    Msgbox, hello there %name_variable%
    return
}

using format

F2::
{
    name_variable := "john3"
    MsgBox, % Format("hello there {}", name_variable)
}

Increment Global Value

value increment example

global counter := 0

F1::
{
    counter++
    MsgBox, counter value: %counter%
}

Evaluate Boolean Variables

evaluate boolean example

global say_hello := true

F1::
{
    if (say_hello)
    {
        Msgbox, hello
    }
}

Function Call & Return Value

function call return value example

F1::
{
    Msgbox, % get_hello_message_function()
    return
}

get_hello_message_function()
{
    message := "hello world"
    return message
}

Parameterized Function Call

function call with parameter example

F1::
{
    Msgbox, % get_hello_message_function("john")
    return
}

get_hello_message_function(name)
{
    message := Format("hello {}", name)
    return message
}

Type Text Press Enter

type & press enter example

F1::
{
    Send, "This is a test message"
    SendInput {Enter}
}

Sleep For Variable Duration Parameterized

sleep variable duration parameterized example

The following script calls a function & provides a range in milliseconds. The function will then sleep for a duratino within the limits provided.

F1::
{
    sleep_duration(2000, 5000)
    Msgbox "finished sleeping"
}

sleep_duration(min_milliseconds, max_milliseconds)
{
    Random, sleepDurationAmount, %min_milliseconds%, %max_milliseconds%
    sleep sleepDurationAmount
}

Execute Task Based on Time of Day

time of day task example

This will need to be adjusted depending on the goal. The following script only checks which hour it is every hour. If this checked more frequently, it would execute multiple times per hour.

global executeHour = 8

global OneMinuteMilliseconds := 60000
F1::
    loop {

        if (shouldExecuteBasedOnTime())
        {
            Msgbox, "Executing Script at Execution Hour"
        }
        Sleep OneMinuteMilliseconds*60
    }


shouldExecuteBasedOnTime() {
    shouldExecute := false
    if (A_Hour = executeHour) {
        shouldExecute := true
    }

    return shouldExecute
}

Execute Task With Percent Chance of Execution

percent chance execution example

global percentChanceOfExecution := 50

F1::
{
    if (shouldExecuteBasedOnChance()) {
        Msgbox, % Format("I executed with {}% of execution", percentChanceOfExecution)
    }
}

shouldExecuteBasedOnChance() {
    shouldExecute := false
    Random, randomNumber, 1, 100

    if (randomNumber <= percentChanceOfExecution) {
        shouldExecute := true
    }

    return shouldExecute
}