Home |
Last modified: 23-02-2024 |
AHK is meant to automate tasks, either through hotkeys/hotstrings, or driving GUI apps (either desktop or web.) In the absence of hotkeys and hotstrings, using AHK/AutoIt is questionable, and it might make more sense to use richer languages like VB or Python, or even compact BASIC compilers like PowerBasic, PureBasic, or FreeBasic. The list of commands supported by AHK can be found in the "Alphabetical Command and Function Index" section of the help file.
AHK is the open-source alternative to AutoIt on which it is based, until AutoIt was closed-source some years ago. For some differences, read "AutoIt vs AutoHotkey".
"The first public beta of AutoHotkey was released on November 10, 2003, as a fork of AutoIt after author Chris Mallett's proposal to integrate hotkey support into AutoIt v2 failed to generate response from the AutoIt community. The author began his own program from scratch basing the syntax on AutoIt v2 and using AutoIt v3 for some commands and the compiler. Later, AutoIt v3 switched from GPL to closed source because of "other projects repeatedly taking AutoIt code" and "setting themselves up as competitors". (Source)
AHKv2 fixes a bunch of idiosyncrasies, although I still find AutoIt's syntax better.
AHK scripts use the .ahk extension. They can be converted into self-standing applications usin the Ahk2Exe application.
By default, a running script with add an item in the icon bar.
Setting up AHK through the installer adds the following items to the pop-up menu when right-clicking an .ahk file:
By default, an AHK script will sit in the icon tray while running but it can be changed through the #NoTrayIcon directive; Double-clicking it displays the applications's main window.
AHK provides a number of useful variables. They are usually read-only and start with A_ by convention (exceptions: of Clipboard, ErrorLevel, etc.)
While AutoIT looks like BASIC, AHK uses a somewhat C-looking syntax, supports #include, and strings are null-terminated:
Quoted strings can contain escape sequences such as `t (tab), `n (linefeed), and `r (carriage return).
String concatenations uses either a . or just a space: "The value is " . MyVar
It can also be done with Format() : MsgBox % Format("You are using AutoHotkey v{1} {2}-bit.", A_AhkVersion, A_PtrSize*8)
To assign a value to a variable, use the := assignment operator, as in MyVar := "Some text".
AutoHotkey supports the ternary operator : condition ? valueIfTrue : valueIfFalse
There must not be any space between the function name and open parenthesis: GetKeyState("Shift")
MyVar := "This is text."
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, the script's first hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).
A subroutine (or sub) is a reusable block of code which can be called to perform some task. Scripts use subroutines to define what should happen when a particular hotkey is pressed or some other event occurs. Scripts can also call subroutines directly, by using Gosub. A subroutine has no explicitly marked ending point, but instead ends if and when control is returned to the subroutine's caller by Return or when the thread is exited.
User-defined functions differ from subroutines in that they can accept parameters and return a value, and they can have local variables.
Normal labels consist of a name followed by a colon. Hotkey labels consist of a hotkey followed by double-colon. Hotstring labels consist of a colon, zero or more options, another colon, an abbreviation and double-colon.
For a list of hotkeys and hotstrings, see the "Hotkeys (Mouse, Joystick and Keyboard Shortcuts)" and "Hotstrings" sections of the help file.
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.
Note: While the script's first hotkey/hotstring label has the same effect as return, other hotkeys and labels do not.
A script that is not persistent and that lacks hotkeys, hotstrings, OnMessage(), and GUI will terminate after the auto-execute section has completed. Otherwise, it will stay running in an idle state, responding to events such as hotkeys, hotstrings, GUI events, custom menu items, and timers.
As of 1.1.33.06, AHK supports the following widgets:
Clipboard =
Send ^a
Send ^c
Stuff := Clipboard
MsgBox, Some title`n%Stuff%
;Download the sqlite3.dll and store it in the script's folder
;Download the following AHK include file from the AHK forum
#Include Class_SQLiteDB.ahk
MyDB := New SQLiteDB
MyDB.OpenDB("dummy.sqlite")
MyDB.Exec("CREATE TABLE IF NOT EXISTS MyTable (id INTEGER, data TEXT)")
MyDB.Exec("INSERT INTO MyTable VALUES (55, 'blablah')")
MyDB.CloseDB()