#Requires

Declares the script's requirements and potentially sets the compatibility mode.

#Requires Requirement
#Requires capability CapabilityNames

Parameters

Requirement

Type: String

A version requirement begins with Keysharp or AutoHotkey. Use Keysharp to require a runtime version, or AutoHotkey to require the implemented language-compatibility version. An unrecognized product name causes an error and the program exits.

The product name may be followed by any combination of the following, separated by spaces or tabs:

CapabilityNames

One or more case-insensitive capability names, separated by commas or whitespace. The permissions are requested together at startup, before hotkeys are registered. If any requested capability is denied or unsupported, an error is reported and the script exits.

For the names, and to request capabilities at run time without exiting, see RequestCapabilities.

Error Message

The error identifies the unmet requirement and the version available to the script.

For a capability requirement, the error names the unmet capabilities and includes any available permission details. It appears in a dialog by default; #ErrorStdOut sends it to standard error instead.

If the script is launched with an interpreter that does not support this directive, the error message may instead resemble:

Line Text: #Requires %Requirement%
Error: This line does not contain a recognized action.

Compatibility Mode [v2.1-alpha.34+]

Specifying a version requirement (or multiple requirements in one directive) may also set the compatibility mode for the current module, subsequent functions and library includes. v2.0-compatible mode is enabled if version "2.0.x" would meet the requirements, otherwise it is disabled. A Keysharp requirement disables it, whatever version it names. If the directive is not used, v2.0 mode is used by default. Using the directive without a version requirement does not affect the mode.

Note: Runtime behavior depends on the mode of the current module or function, not the position of the directive in relation to the current line or file.

The first version directive used within each module sets the runtime mode for all startup code in that module across all files.

Version directives are parsed at load time to determine the mode for subsequent function or method definitions and #Include <LibName> directives. This effect is positional and can vary any number of times within a single module.

#Module sets the default mode of the new module to that of the __Init module.

Version directives are scoped to the current module, file and function. In other words:

In AutoHotkey v2.1, a directive inside a function body affects only nested functions, and a class body is not a scope for version directives, so a directive inside a class body continues to apply after the class and can set the module's mode. In Keysharp, a directive inside a function body also sets the mode of that function, and a directive inside a class body sets the mode of that class's methods, properties and nested classes and reverts at the end of the class, in the same way that an #Import in a class body is scoped to its class.

Correct use of version directives can allow code written for different versions to be mixed in one script.

For details of how the mode affects the script's behavior, see Compatibility Mode (Changes).

The compatibility mode example below demonstrates how version directives affect compatibility mode.

Remarks

If the script uses syntax or functions which are unavailable in earlier versions, using this directive ensures that the error message shows the unmet requirement, rather than indicating an arbitrary syntax error. This cannot be done with something like if (A_AhkVersion <= "1.1.33") because a syntax error elsewhere in the script would prevent it from executing.

When sharing a script or posting code online, using this directive allows anyone who finds the code to readily identify which version of AutoHotkey it was intended for.

Other programs, editors and related tools can inspect this directive to determine how to interpret or highlight a script.

Version strings are compared as a series of dot-delimited components, optionally followed by a hyphen and pre-release identifier(s).

A trailing "+" is sufficient to indicate to the reader that later versions are acceptable, but is not required.

Like other directives, #Requires cannot be executed conditionally.

VerCompare, #ErrorStdOut

Examples

Requires a particular runtime version.

#Requires Keysharp >=0.0.0.17

Requests input monitoring and screen capture before the script begins normal execution.

#Requires capability InputMonitoring, ScreenCapture

Causes the script to run only on v2.0, including alpha releases.

#Requires AutoHotkey v2.0-a
MsgBox "This script will run only on v2.0, including alpha releases."

Causes the script to run only on v2.0, including pre-release versions.

#Requires AutoHotkey >=2.0- <2.1

Causes the script to run only with a 64-bit interpreter (EXE).

#Requires AutoHotkey 64-bit

Causes the script to run only with a 64-bit interpreter (EXE) version 2.0-rc.2 or later.

#Requires AutoHotkey v2.0-rc.2 64-bit

Demonstrates how version directives affect compatibility mode.

; This essential line sets the mode for the current module (__Main).
#Requires AutoHotkey v2.1-alpha.33

; Use try-catch to detect the mode.  Base returns "" in v2.0 mode and unset in v2.1 mode.
; ?? is not used here because it works for "blank-unset" return values in both modes.
try
    MsgBox Type(Any.Prototype.Base), "Main #1"
catch UnsetError
    MsgBox "UnsetError", "Main #1"

; Try swapping these two directives around to see how it affects CompatibleLib's initialization.
#Import "CompatibleLib.ahk" {CompatibleFunc}
; #Include "CompatibleLib.ahk"

CompatibleFunc()
MainFunc()

; Directives in any #Include above have no effect on this module.
try
    MsgBox Type(Any.Prototype.Base), "Main #2"
catch UnsetError
    MsgBox "UnsetError", "Main #2"

MainFunc() {
    try
        MsgBox Type(Any.Prototype.Base), "MainFunc"
    catch UnsetError
        MsgBox "UnsetError", "MainFunc"
}

CompatibleLib.ahk:

#Requires AutoHotkey v2.0

; When #included in a v2.1 module, the following throws an error.
try
    MsgBox Type(Any.Prototype.Base), "CompatibleLib"
catch UnsetError
    MsgBox "UnsetError", "CompatibleLib"

; The function below always uses v2.0 mode, due to the directive above.
CompatibleFunc() {
    try
        MsgBox Type(Any.Prototype.Base), "CompatibleFunc"
    catch UnsetError
        MsgBox "UnsetError", "CompatibleFunc"
}