AutoHotkey examples

Every snippet below runs on its own. Add #Requires AutoHotkey v2.0 at the top of the file, paste one in, and try it.

Remap a key you never use

CapsLock::Ctrl
+CapsLock::CapsLock   ; Shift+CapsLock still toggles caps

Append the clipboard to a log file

^!l::
{
    stamp := FormatTime(A_Now, "yyyy-MM-dd HH:mm")
    FileAppend stamp " " A_Clipboard "`n", A_Desktop "\clips.txt"
    ToolTip "Saved"
    Sleep 800
    ToolTip
}

Restore a two-window layout

#l::
{
    half := A_ScreenWidth / 2
    WinMove 0, 0, half, A_ScreenHeight, "ahk_exe code.exe"
    WinMove half, 0, half, A_ScreenHeight, "ahk_exe chrome.exe"
}
^!g::
{
    saved := A_Clipboard
    A_Clipboard := ""
    Send "^c"
    if ClipWait(1)
        Run "https://duckduckgo.com/?q=" A_Clipboard
    A_Clipboard := saved
}

ClipWait is what keeps this honest: it waits until the copy actually lands instead of reading a stale clipboard.

A small dialog

MyGui := Gui()
MyGui.Add("Text", , "What should I call you?")
MyGui.Add("Edit", "w180 vName")
MyGui.Add("Button", , "OK").OnEvent("Click", Greet)
MyGui.Show()

Greet(*)
{
    saved := MyGui.Submit()
    MsgBox "Hello, " saved.Name
}

Cycle through window states

#space::
{
    static maximized := false
    maximized := !maximized
    if maximized
        WinMaximize "A"
    else
        WinRestore "A"
}

A static variable keeps its value between calls, which is how a hotkey remembers what it did last time.

More context for these patterns is on the commands and functions pages; longer builds live under Windows automation.

Frequently asked questions

Do these examples work in AutoHotkey v2?

Yes. All of them use v2 syntax; add #Requires AutoHotkey v2.0 to the top of your file.

Why use ClipWait after sending Ctrl+C?

Copying is not instant. ClipWait pauses until the clipboard has new content so the script does not read the previous value.


AutoHotkeyTools.com is an independent Windows resource covering AutoHotkey tools, scripts, hotkeys, tutorials and desktop automation. It is not affiliated with the AutoHotkey project.