A module is basically a script within the script. Each module has its own:
Splitting the components of a script into modules can make the components easier to reuse and maintain.
By default, all lines of code and global variables (including functions and classes) are added to an implicitly-defined module named "__Main". The script can start a new module with #Module or load one from file with #Import.
An #Import declaration can be used to add a module or some of its functions, classes and variables into the current module's global namespace. These need not be explicitly marked as exports, but doing so with an Export declaration permits them to be imported by a wildcard import.
All modules implicitly import from the built-in "AHK" module, which contains all built-in classes, variables and functions. A declaration or assignment within a module can reuse the name of a built-in class or function, in which case that class or function is not accessible except through the AHK module (e.g. #Import AHK, AHK.MsgBox()). However, built-in (A_) variables do not require a declaration or explicit import to allow assignment, and their names cannot be reused. A_Args is the exception (it is a normal variable defined in __Main).
A module name is added to a module's global namespace only when imported, and conflicts can be resolved by giving the imported module an alias within the destination module.
#Include is able to include a file once per module.
#Warn can enable or disable warnings within the current module without affecting other modules. For details, see the #Warn remarks.
#HotIf affects only the current module. #HotIf and HotIf expressions are module-scoped, as they may contain references to module-level variables. For instance, the effect of #HotIf myToggle depends on what value myToggle has in the current module.
The built-in KS module exports Keysharp-specific classes, functions and variables. These are not available to a script until they are imported, which keeps the global namespace the same as AutoHotkey's.
#Import "Ks" { Image, A_DirSeparator }
Capture := Image.FromDesktop()
MsgBox A_DirSeparator
New methods or properties added to an existing built-in class, such as Array or Buffer, are available without importing KS.
The full set of exports is listed below.
| Export | Description |
|---|---|
| Clr | Loads .NET assemblies and calls into them from a script. |
| HashMap | An unordered hash table, faster than Map where ordering is not needed. |
| Highlight | Outlines a region of the screen with a colored, click-through border. |
| Image | Captures, loads, manipulates and saves images. |
| Json | Converts between JSON text and Keysharp objects. |
| NamedArgs | Carries the named arguments of one call, for building or inspecting them at run time. |
| Overlay | Draws a click-through, always-on-top surface over the screen. |
| RealThread | Runs a script on a real operating-system thread. |
| StringBuffer | A mutable text buffer for building large strings efficiently. |
| WinEvent | Calls a function when windows are created, activated, moved or closed. |
| Export | Description |
|---|---|
| AES | Encrypts or decrypts data using AES. |
| ATan2 | Returns the arc tangent of y/x, using the signs of both values to determine the quadrant. |
| Base64Decode | Decodes Base64 text into a Buffer. |
| Base64Encode | Encodes binary data as Base64 text. |
| ClipCursor | Confines the mouse cursor to a region of the screen, or releases it. |
| Collect | Runs a garbage collection. |
| CopyImageToClipboard | Places an image on the clipboard. |
| Cosh | Returns the hyperbolic cosine. |
| CRC32 | Returns the CRC32 checksum of a value. |
| FileCreateTemp | Creates a uniquely named temporary file. |
| FileDirName | Returns the directory portion of a path. |
| FileFullPath | Expands a path to its absolute form. |
| FormatCs | Formats a string using .NET composite formatting. |
| GetKeyboardLayout | Returns the keyboard layout of a window or thread. |
| GetKeyInfo | Returns the characters a key produces under a keyboard layout. |
| IsClipboardEmpty | Returns 1 if the clipboard holds no data. |
| Join | Concatenates values with a separator. |
| LockRun | Runs a program while holding a named lock, preventing concurrent runs. |
| Sends an email message over SMTP. | |
| MD5 | Returns the MD5 digest of a value. |
| MonitorFromPoint | Returns the monitor containing a screen point. |
| MonitorGetScale | Returns the DPI scale factor of a monitor. |
| NormalizeEol | Converts line endings in a string to a single form. |
| OutputDebugLine | Writes a line to the system debug output. |
| ParseScript | Parses source code and returns its syntax tree. |
| RandomSeed | Seeds the pseudo-random number generator. |
| RegExMatchCs | Matches a .NET regular expression. |
| RegExReplaceCs | Replaces text using a .NET regular expression. |
| RequestCapabilities | Requests optional platform capabilities the script needs. |
| RunScript | Compiles and runs source code, returning a process object. |
| SecureRandom | Returns a cryptographically secure random number. |
| SHA1 | Returns the SHA-1 digest of a value. |
| SHA256 | Returns the SHA-256 digest of a value. |
| SHA384 | Returns the SHA-384 digest of a value. |
| SHA512 | Returns the SHA-512 digest of a value. |
| ShowDebug | Shows or hides the debug output window. |
| Sinh | Returns the hyperbolic sine. |
| Tanh | Returns the hyperbolic tangent. |
| WinFromPoint | Returns the window at a screen point. |
| WinMaximizeAll | Maximizes all windows. |
The KS module's built-in variables cover thread identity, millisecond timestamps, screen metrics, hotstring defaults and directive state. They are listed with descriptions under KS Module Variables.
The built-in AHK module contains every built-in class, function and variable. Each module implicitly imports it, so these names are available without any declaration and the module rarely has to be named.
Importing it explicitly is useful when a script defines its own function or class using the name of a built-in one. The built-in remains reachable through the module, which lets a script wrap a built-in rather than replace it, as in the example below.
#Import AHK
AHK.MsgBox("Shown by the built-in MsgBox.")
Because the AHK module forwards the built-in API rather than declaring its own, its exports are documented throughout this reference and not listed here. Built-in (A_) variables are the exception to shadowing: their names cannot be reused, so they never need to be reached through the module.
The body of each module is executed at program startup, by the auto-execute thread. Execution begins with the module which was defined or loaded last. If the module has no imports, the second-last module is executed next, and so on, until all modules have executed.
[v2.1-alpha.21+]: If a module has not begun execution by the time one of its variables or constants is referenced, the module is executed immediately. The #Import directive itself has no effect on the order. For example:
#module A
MsgBox "A executing" ; Shown second.
global ANSWER := 42
#module B
#import A {ANSWER}
MsgBox "B executing" ; Shown first.
MsgBox ANSWER ; Shown third.
[v2.1-alpha.20] and earlier: Each module executes its imported modules before its own body, excluding modules which have already begun execution. This doesn't work well when modules have circular dependencies.
The module search path is a list of directories which the #Import statement looks in to find module files. The directory of the file which contains the #Import directive is always searched first. The AhkImportPath environment variable can contain a semicolon-delimited list of additional directories to search. If it is not defined, the default list is as follows:
%A_ScriptDir%;%A_MyDocuments%\AutoHotkey;%A_AhkPath%\..
Built-in variables may be used in the value of AhkImportPath by enclosing them in percent signs. Any percent signs which are not part of a valid variable reference are interpreted literally. References to environment variables are not resolved by AutoHotkey, as they are typically resolved before the process starts. List items can be absolute paths or relative to the directory containing the script (A_ScriptDir). The list is resolved only once, so A_LineFile always refers to the main script.
Directories are searched in the order they are listed. Within each directory, files are considered in this order:
ModuleNameModuleName\__Init.ahkModuleName.ahkFor example, #Import M may load the file with exact name M, M\__Init.ahk or M.ahk. #Import "M" is the same except that the name M is not added to the current module.
Each module has its own global variables (MyVar and ShowVar).
import Other as Other ; This works with v2.1-alpha.19 and v2.1-alpha.20.
;#Import Other ; This requires v2.1-alpha.20+.
;import Other {} ; This would make Other accessible only in v2.1-alpha.20+.
MyVar := 1
ShowVar() ; Our MyVar is 1.
Other.ShowVar() ; Other MyVar is still 2.
MsgBox "Within main, Other.MyVar = " (Other.MyVar ?? "inaccessible") ; MyVar is accessible in v2.1-alpha.19+.
ShowVar() => MsgBox("Main MyVar = " MyVar)
#Module Other
MyVar := 2
export ShowVar() => MsgBox("Other MyVar = " MyVar)
Use an alias to resolve a conflict. Each module has its own Calculate, which takes precedence over any wildcard import.
import {Calculate as CalculateX} from X
import * from Y
MyVar := 1
MsgBox Calculate()
MsgBox CalculateX()
MsgBox Check(3)
MsgBox "X = " (X ?? "not imported")
MsgBox "Y = " (Y ?? "not imported")
Calculate() => 1
#Module X
export Calculate() => 2
#Module Y
export Calculate() => 3
export Check(n) => n = Calculate()