You are on page 1of 14

Command Reference

#AllowSameLineComments

Only for use in AutoIt2 (.aut scripts): Allows same-line comments.

#AllowSameLineComments

Parameters

None

Remarks

Specifying this directive at the top of any AutoIt2 (.aut) script will enable the use of same-line comments, which
are normally disabled for compatibility reasons. If not used at the top of the script, same-line comments are not
supported above the point where the directive occurs.

Related

None

Example

#AllowSameLineComments
Sleep, 1 ; This is a same-line comment.

Command Reference
#CommentFlag

Changes the script's comment symbol from semi-colon to some other string.

#CommentFlag NewString

Parameters
One or more characters that should be used as the new comment flag. Up to 15 characters may
NewString
be specified.

Remarks

The default comment flag is semi-colon (;).

The comment flag is used to indicate that text that follows it should not be acted upon by the script (comments
aren't even loaded into memory when a script is launched).

A comment flag that appears on the same line as a command is not considered to mark a comment unless it has at
least one space or tab to its left. For example:
MsgBox, Test1 ; This is a comment.
MsgBox, Test2; This is not a comment and will be displayed by MsgBox.

Related

#EscapeChar

Example

#CommentFlag // ; Change to C++ style of comments.

Command Reference
#EscapeChar (and discussion of escape sequences)

Changes the script's escape character (e.g. accent vs. backslash).

#EscapeChar NewChar

Parameters

NewChar Specifies a single character.

Remarks

The default escape character for AutoIt2 (.aut) scripts is backslash (\). For all other file extensions, including all
compiled scripts, the escape character defaults to accent (`). When a .aut script is auto-converted into a .ahk script,
the backslash escape character is globally replaced with the accent escape character.

The escape character is used to indicate that the character immediately following it should be interpreted
differently than it normally would.

Escape Sequences (when accent is the escape character)

Type This To Get This


, (literal comma) Note: Commas that appear within the last parameter of a command do not
`, need to be escaped because the program knows to treat them literally. The same is generally
true for all parameters of MsgBox because it has smart comma handling.
`% % (literal percent)
`n newline (linefeed/LF)
`r carriage return (CR)
`t tab (the more typical horizontal variety)
`v vertical tab
`b backspace
`a alert (bell)
`f formfeed
`` ` (accent, i.e. two consecutive escape characters result in a single literal character)

Related

The following rarely used directives also exist:


#DerefChar # ; Change it from its normal default, which is %
#Delimiter / ; Change it from its normal default, which is comma.

Example

#EscapeChar \ ; Change it to be the same as AutoIt2's.

Command Reference
#HotkeyInterval

Along with #MaxHotkeysPerInterval, specifies the rate of hotkey activations beyond which a warning dialog will
be displayed.

#HotkeyInterval Value

Parameters

Value The length of the interval in milliseconds.


Remarks

Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite
loop of keystrokes (via a Send command that accidentally triggers other hotkeys), your computer could become
unresponsive due to the rapid flood of keyboard events. An oversimplified example of an infinite loop of
keystrokes: ^c::Send, ^c

If you want to do something like the above without triggering an infinite loop, add a $ to the hotkey definition
(e.g. $#y::). The $ tells it to use the hook for the hotkey, which is smart enough to know not to treat our own
SEND commands as a source for hotkeys. Using the hook has at least a couple of disadvantages, which is why it
isn't always used: 1) Only one instance of the program at a time should have the hook installed; 2) It may very
slightly impact system performance.

If this directive is unspecified in the script, it will behave as though set to 2000.

Related

#MaxHotkeysPerInterval

Example

#HotkeyInterval 2000 ; This is the default value (milliseconds).


#MaxHotkeysPerInterval 50 ; This is the default value.

Command Reference
#HotkeyModifierTimeout

Affects the behavior of hotkey modifiers: CTRL, ALT, WIN, and SHIFT.

#HotkeyModifierTimeout Value

Parameters

The length of the interval in milliseconds. The value can be -1 so that it never times out, or 0 so
Value
that it always times out.

Remarks

This directive need not be used if the script has the keyboard hook installed (you can see if your script uses the
hook via the "View->Key history" menu item in the main window, or via the KeyHistory command). This is
because the hook can keep track of which modifier keys (ALT/CTRL/WIN/SHIFT) the user is physically holding
down and doesn't need to use the timeout.

To illustrate the effect of this directive, consider this example:


^!a::Send, abcdefg

When the Send command executes, the first thing it does is release the CTRL and ALT keys so that the characters
get sent properly. After sending the keys, the command doesn't know whether it can safely push back down CTRL
and ALT (to match whether the user is still holding them down). But if less than the specified number of
milliseconds have elapsed, it will assume that the user hasn't had a chance to release the keys yet and it will thus
push them back down to match their physical state. Otherwise, the modifier keys will not be restored and the user
will have to release and press them again to get them to modify the same or another key.

The timeout should be set to a value less than the amount of time that the user typically holds down a hotkey's
modifiers before releasing them. Otherwise, the modifiers may be restored to the down position (get stuck down)
even when the user isn't physically holding them down.

You can ensure that the keyboard hook is installed, thus making the use of this directive unnecessary, by adding
the line #InstallKeybdHook anywhere in the script. Alternatively, reducing KeyDelay via SetKeyDelay to 0 or -1
may also help if the keyboard hook isn't installed, since SEND will run more quickly.

If this is directive is unspecified in a script, it behaves as though set to 50.

Related

GetKeyState

Example

#HotkeyModifierTimeout 200

Command Reference
#Include / #IncludeAgain

Causes the script to behave as though the specified file's contents are present.

#Include FileName
#IncludeAgain FileName

Parameters

The name of the file to be included. It must not contain double quotes, variable references (e.g.
FileName %ProgramFiles%), wildcards, or escape sequences (which are not needed since all characters
are treated literally).
Remarks

#Include ensures that FileName is included only once, even if multiple inclusions are encountered for it. By
contrast, #IncludeAgain allows multiple inclusions of the same file, while being the same as #Include in all other
respects.

Lines displayed in the main window via ListLines or the menu View->Lines are always numbered according to
their physical order within their own files. In other words, including a new file will not change the line numbering
of the main script file.

Related

None.

Example

#Include C:\My Documents\Scripts\Utility Subroutines.ahk ; Don't enclose in double quotes.

Command Reference
#InstallKeybdHook

Forces the unconditional installation of the keyboard hook.

#InstallKeybdHook

Parameters

None

Remarks

The keyboard hook is currently not supported under Windows 95/98/ME because those operating systems require
a different type of hook that must reside in a DLL file. That hook has not yet been developed.

Specifying this directive anywhere in a script will force the keyboard hook to be installed.

Normally, the keyboard hook is installed only when the script contains one or more hotkeys that require that
method (most hotkeys don't). You can determine whether a script is using the hook via the KeyHistory command
or menu item.

Using the hook has at least a couple of disadvantages, which is why it isn't always used: 1) Only one instance of
the program at a time should have the hook installed; 2) It may very slightly impact system performance.
Related

#InstallMouseHook, #UseHook, KeyHistory

Example

#InstallKeybdHook

Command Reference
#InstallMouseHook

Forces the unconditional installation of the mouse hook.

#InstallMouseHook

Parameters

None

Remarks

The mouse hook is currently not supported under Windows 95/98/ME because those operating systems require a
different type of hook that must reside in a DLL file. That hook has not yet been developed.

Specifying this directive anywhere in a script will force the mouse hook to be installed.

Normally, the mouse hook is installed only when the script contains one or more mouse hotkeys. You can
determine whether a script is using the hook via the KeyHistory command or menu item.

Using the hook has at least a couple of disadvantages, which is why it isn't always used: 1) Only one instance of
the program at a time should have the hook installed; 2) It may very slightly impact system performance.

Related

#InstallKeybdHook, #UseHook, KeyHistory

Example
#InstallMouseHook

Command Reference
#MaxHotkeysPerInterval

Along with #HotkeyInterval, specifies the rate of hotkey activations beyond which a warning dialog will be
displayed.

#MaxHotkeysPerInterval Value

Parameters

The maximum number of hotkeys that can be pressed in the interval specified by
Value
#HotkeyInterval without triggering a warning dialog.

Remarks

Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite
loop of keystrokes (via a Send command that accidentally triggers other hotkeys), your computer could become
unresponsive due to the rapid flood of keyboard events. An oversimplified example of an infinite loop of
keystrokes: ^c::Send, ^c

If you want to do something like the above without triggering an infinite loop, add a $ to the hotkey definition
(e.g. $#y::). The $ tells it to use the hook for the hotkey, which is smart enough to know not to treat our own
SEND commands as a source for hotkeys. Using the hook has at least a couple of disadvantages, which is why it
isn't always used: 1) Only one instance of the program at a time should have the hook installed; 2) It may very
slightly impact system performance.

If this directive is unspecified in the script, it will behave as though set to 50.

Related

#HotkeyInterval

Example

#HotkeyInterval 2000 ; This is the default value (milliseconds).


#MaxHotkeysPerInterval 50 ; This is the default value.
Command Reference
#MaxThreads

Sets the maximum number of simultaneous threads.

#MaxThreads Value

Parameters

Value The maximum total number of threads that can exist simultaneously (limit 20).

Remarks

This setting is global, meaning that it should be specified only once (anywhere in the script) to affect the behavior
of the entire script.

Although a value of 1 is allowed, it is not recommended because it would prevent new hotkeys from launching
whenever the script is displaying a MsgBox or other dialog.

Any hotkey subroutine whose first line is ExitApp, Pause, Edit, Reload, KeyHistory, ListLines, ListVars, or
ListHotkeys will always run regardless of this setting.

If this setting is lower than #MaxThreadsPerHotkey, it effectively overrides that setting.

If this directive is unspecified in the script, it will behave as though set to 10.

Related

#MaxThreadsPerHotkey, Threads, #MaxHotkeysPerInterval, #HotkeyInterval, ListHotkeys

Example

#MaxThreads 2

Command Reference
#MaxThreadsBuffer

Causes some or all hotkeys to buffer rather than ignore keypresses when their #MaxThreadsPerHotkey limit has
been reached.
#MaxThreadsBuffer On|Off

Parameters

On: All hotkey subroutines between here and the next #MaxThreadsBuffer ON directive will
buffer rather than ignore presses of their hotkeys whenever their subroutines are at their
#MaxThreadsPerHotkey limit.
On|Off
Off: This is the default behavior. A hotkey press will be ignored whenever that hotkey is
already running its maximum number of threads (usually 1, but this can be changed with
#MaxThreadsPerHotkey).

Remarks

This directive is rarely used because this type of buffering, which is OFF by default, usually does more harm than
good. For example, if you accidentally press a hotkey twice, having this setting ON would cause that hotkey's
subroutine to automatically run a second time if its first thread takes less than 1 second to finish (this type of
buffer expires after 1 second, by design). Note that AutoHotkey buffers hotkeys in several other ways. It's just
that this particular way can be detrimental, thus it is OFF by default.

The main use for this directive is to increase the responsiveness of the keyboard's auto-repeat feature. For
example, when you hold down a hotkey whose #MaxThreadsPerHotkey setting is 1 (the default), incoming
keypresses are ignored if that hotkey subroutine is already running. Thus, when the subroutine finishes, it must
wait for the next auto-repeat keypress to come in, which might take 50ms or more due to being caught "in
between" keystrokes of the auto-repeat cycle. This 50ms delay can be avoided by enabling this directive for any
hotkey that needs the best possible response time while it is being auto-repeated.

As with all # directives, this one should not be positioned in the script as though it were a command (i.e. it is not
necessary to have it contained within a subroutine). Instead, position it immediately before or after the first hotkey
label you wish to have affected by it.

Related

#MaxThreads, #MaxThreadsPerHotkey, Threads, #MaxHotkeysPerInterval, #HotkeyInterval, ListHotkeys

Example

#MaxThreadsBuffer on
#x::MsgBox, This hotkey will use this type of buffering.
#y::MsgBox, And this one too.
#MaxThreadsBuffer off
#z::MsgBox, But not this one.

Command Reference
#MaxThreadsPerHotkey
Sets the maximum number of simultaneous threads per hotkey.

#MaxThreadsPerHotkey Value

Parameters

Value The maximum number of threads that can be launched for a given hotkey subroutine (limit 20).

Remarks

This setting is used to control how many "instances" of a given hotkey subroutine are allowed to exist
simultaneously. For example, if a hotkey has a max of 1 and it is pressed again while its subroutine is already
running, the press will be ignored. This is helpful to prevent accidental double-presses. However, if you wish
these keypresses to be buffered rather than ignored -- perhaps to increase the responsiveness of the keyboard's
auto-repeat feature -- use #MaxThreadsBuffer.

Unlike #MaxThreads, this setting is not global. Instead, position it before the first hotkey label you wish to have
affected by it, which will result in all subsequent hotkeys using that value until another instance of this directive is
encountered.

Any hotkey subroutine whose first line is ExitApp, Pause, Edit, Reload, KeyHistory, ListLines, ListVars, or
ListHotkeys will always run regardless of this setting.

The setting of #MaxThreads -- if lower than than this setting -- takes precedence.

If this directive is unspecified in the script, it will behave as though set to 1.

Related

#MaxThreads, #MaxThreadsBuffer, Threads, #MaxHotkeysPerInterval, #HotkeyInterval, ListHotkeys

Example

#MaxThreadsPerHotkey 3

Command Reference
#NoTrayIcon

Disables the showing of a tray icon.

#NoTrayIcon
Parameters

None

Remarks

Specifying this anywhere in a script will prevent the showing of a tray icon for that script when it is launched
(even if the script is compiled into an EXE).

If you use this for a script that has hotkeys, you might want to bind a hotkey to the ExitApp command. Otherwise,
there will be no easy way to exit the program (without restarting the computer or killing the process). Example:
#x::ExitApp

Related

ExitApp

Example

#NoTrayIcon

Command Reference
#SingleInstance

Prevents a second instance of a script from being launched.

#SingleInstance

Parameters

None

Remarks

Specifying this anywhere in a script will prevent new instances of that script from being launched once there is
already one running. Instead, you will be prompted for whether to keep the original (old) instance or replace it
with the new instance.
Related

None

Example

#SingleInstance

Command Reference
#UseHook

Forces the use of the hook to implement all or some hotkeys.

#UseHook [On|Off]

Parameters

On (default): The hook will be used to implement all hotkeys between here and the next
#UseHook OFF directive.
On|Off
Off: Hotkeys will be implemented using the default method (RegisterHotkey() if possible or
the hook otherwise).

Remarks

Normally, the windows API function RegisterHotkey() is used to implement a non-mouse hotkey whenever
possible. However, the responsiveness of hotkeys might be better under some OSes or under some conditions if
the hook is used instead.

Turning this directive ON is equivalent to using the $ prefix in the definition of each affected hotkey.

As with all # directives, this one should not be positioned in the script as though it were a command (i.e. it is not
necessary to have it contained within a subroutine). Instead, position it immediately before or after the first hotkey
label you wish to have affected by it.

If this directive does not appear in the script at all, it will behave as though set to OFF.

Related

#InstallKeybdHook, #InstallMouseHook, ListHotkeys


Example

#UseHook ; Force the use of the hook for hotkeys after this point.
#x::MsgBox, This hotkey will be implemented with the hook.
#y::MsgBox, And this one too.
#UseHook off
#z::MsgBox, But not this one.

Command Reference
#WinActivateForce

Skips the gentle method of of activating a window and goes straight to the forceful method.

#WinActivateForce

Parameters

None

Remarks

Specifying this anywhere in a script will cause commands that activate a window -- such as WinActivate,
WinActivateBottom, and GroupActivate -- to skip the "gentle" method of activating a window and go straight to
the more forceful methods.

Although this directive will usually not change how quickly or reliably a window is activated, it might prevent
task bar buttons from flashing when different windows are activated quickly one after the other.

Windows 95 and NT will probably never need this setting since they are more permissive about allowing
windows to be activated.

Related

WinActivate, WinActivateBottom, GroupActivate

Example

#WinActivateForce

You might also like