Mouse and keyboard macros and hotkeys.

Home | Download | Documentation | Changelog | Support | Forum

Frequently Asked Questions (FAQ)

Language Syntax

Common Tasks

Hotkeys, Hotstrings, and Remapping


Language Syntax

When are quotation marks used with commands and their parameters?

Double quotes (") have special meaning only within expressions. In all other places, they are treated literally as if they were normal characters. However, when a script launches a program or document, the operating system usually requires quotes around any command-line parameter that contains spaces, such as in this example: Run, Notepad.exe "C:\My Documents\Address List.txt"

When exactly are percent signs used around variable names?

Variable names are always enclosed in percent signs except in cases illustrated in bold below:

1) In parameters that are input or output variables: StringLen, OutputVar, InputVar
2) On the left side of an assignment: Var = 123abc
3) On the left side of non-expression if-statements: If Var1 < %Var2%
4) Everywhere in expressions. For example:
If (Var1 <> Var2)
Var1 := Var2
+ 100

When should percent signs and commas be escaped?

Literal percent signs must be escaped by preceding them with an accent. For example: MsgBox The current percentage is 25`%. Literal commas must also be escaped (`,) except when used in MsgBox or the last parameter of any command (in which case the accent is permitted but not necessary).

When commas or percent signs are enclosed in quotes within an expression, the accent is permitted but not necessary. For example: Var := "15%"


Common Tasks

Why is the Run command unable to launch my game or program?

Some programs need to be started in their own directories (when in doubt, it is usually best to do so). For example:
Run, %ProgramFiles%\Some Application\App.exe, %ProgramFiles%\Some Application

How can the output of a command line operation be retrieved?

A small freeware utility (4 KB) captures up to 512 KB of output from a command or program. The text is captured to the clipboard, which a script can access via the clipboard variable. For example:
RunWait %comspec% /c dir | cb.exe
MsgBox %clipboard%

How can a script close other script(s)?

Here is an example:

DetectHiddenWindows On  ; Allows a script's hidden main window to be detected.
SetTitleMatchMode 2  ; Avoids the need to specify the full path of the file below.
WinClose Script's File Name.ahk - AutoHotkey  ; Update this to reflect the script's name (case sensitive).

How can a repeating action be stopped without exiting the script?

To pause or resume the entire script at the press of a key, assign a hotkey to the Pause command as in this example:
^!p::Pause ; Press Ctrl+Alt+P to pause. Press it again to resume.

To stop an action that is repeating inside a Loop, consider the following example hotkey, which both starts and stops its own repeating action. In other words, pressing the hotkey once will start the Loop. Pressing the same hotkey again will stop it.

#MaxThreadsPerHotkey 3
#z::  ; Win+Z hotkey.
#MaxThreadsPerHotkey 1
if KeepWinZRunning = y  ; This means an underlying thread is already running the loop below.
	KeepWinZRunning =  ; Make it blank to signal that thread's loop to stop.
	return  ; End this thread so that the one underneath will resume and see the change.
; Otherwise:
KeepWinZRunning = y
	ToolTip, Press Win-Z again to stop this from flashing.
	Sleep, 1000
	if KeepWinZRunning =  ; The user signaled the loop to stop by pressing Win-Z again.
		break  ; Break out of this loop.
	Sleep, 1000
KeepWinZRunning =  ; Reset in preparation for the next press of this hotkey.

How can performance be improved for games or at other times when the CPU is under heavy load?

If a script's Hotkeys, MouseClicks, or Sends are noticeably slower than normal while the CPU is under heavy load, raising the script's process-priority may help. To do this, include the following line near the top of the script:
Process, Priority, , High

How can context sensitive help for AutoHotkey commands be used in any editor?

Rajat created this script.

How can a script detect when a web page is finished loading?

The technique in the following example will work with MS Internet Explorer for most pages. A similar technique might work in other browsers:

MouseMove, 0, 0  ; Prevents the status bar from showing a mouse-hover link instead of "Done".
WinWait, Yahoo! - Microsoft Internet Explorer 
StatusBarWait, Done, 30
if ErrorLevel <> 0 
   MsgBox, The wait timed out or the window was closed. 
   MsgBox, The page is done loading. 

How can dates and times be compared or manipulated?

The EnvAdd command can add or subtract a quantity of days, hours, minutes, or seconds to a time-string that is in the YYYYMMDDHH24MISS format. The following example subtracts 7 days from the specified time:
EnvAdd, VarContainingTimestamp, -7, days

To determine the amount of time between two dates or times, see EnvSub, which gives an example. Also, the built-in variable A_Now contains the current local time. Finally, there are several built-in date/time variables, as well as the FormatTime command to create a custom date/time string.

Why don't Hotstrings, Send, and MouseClick work in certain games?

Some games use DirectInput exclusively. As a side-effect, they might ignore all simulated keystrokes and mouse clicks. In such cases, increasing SetKeyDelay might help with the Send command. Try one of these:
SetKeyDelay, 10, 10
SetKeyDelay, 0, 10

In addition, ControlSend might work in cases where Send fails. Finally, it has been observed that the following products are able to send keystrokes and mouse clicks to a wider variety of games than other macro programs: Journal Macro, QuicKeys, KeyGO, OnHand, and Toggle Keyboard.

I use multiple languages or keyboard layouts on my system. Why do Send and Hotstrings sometimes send the wrong characters?

This can happen whenever the script's language or keyboard layout does not match that of the active window. To fix it, open the script's main window via its tray icon. While the main window is active, use the language bar (or a language hotkey such as LeftAlt+Shift) to change the script's language/layout to match that of the window you are currently typing in. Switching the script's language can be automated with the following example hotkey:

#l::   ; Win+L hotkey.
ListLines  ; Show the script's main window.
WinWaitActive ahk_class AutoHotkey
Send {LAlt down}{Shift}{LAlt up}  ; Switch to alternate language (keys must be in this format).
WinMinimize  ; Minimize the window found by WinWaitActive above.

More info: Like all applications, each script starts off using your default language. If the default does not match that of the active window (where the keystrokes are sent), the difference in keyboard layouts might cause keystrokes sent by the script to be translated into something unexpected.

How can Winamp be controlled even when it isn't active?

See Automating Winamp.

How can MsgBox's button names be changed?

Here is an example.


Hotkeys, Hotstrings, and Remapping

How do I put my hotkeys and hotstrings into effect automatically every time I restart my PC?

There is a folder in the Start Menu called Startup.  If you put a shortcut to your script in that folder, the script will launch automatically every time you start your PC. To create a shortcut:

1) Select the script file in Explorer and press Control-C (but if your script is is a .ini file, select AutoHotkey.exe instead).
2) Right-click the Start button and choose "Explore All Users".
3) Navigate to the Startup folder inside the Programs folder.
4) From the menu bar, choose Edit > Paste Shortcut.  The shortcut to the script should now be in the Startup folder.

I'm having trouble getting my mouse buttons working as hotkeys. Any advice?

Note that mouse hotkeys are not currently possible on Windows 95/98/Me. On other operating systems, the left and right mouse buttons should be assignable normally (for example, "#LButton::" is the Win+LeftButton hotkey). Similarly, the middle button and the turning of the mouse wheel should be assignable normally except on mice whose drivers directly control those buttons.

The fourth button (XButton1) and the fifth button (XButton2) might be assignable if your mouse driver allows their clicks to be seen by the system. If they cannot be seen -- or if your mouse has more than five buttons -- you can try configuring the software that came with the mouse (sometimes accessible in the Control Panel or Start Menu) to send a keystroke whenever you press one of these buttons. Such a keystroke can then be defined as a hotkey in a script. For example, if you configure the fourth button to send Control+F12, you can then indirectly configure that button as a hotkey by using ^F12:: in the script.

If you have a five-button mouse whose fourth and fifth buttons cannot be seen, you can try changing your mouse driver to the default driver included with the OS. This assumes there is such a driver for your particular mouse and that you can live without the features provided by your mouse's custom software.

How can tab and space be defined as hotkeys?

Use the names of the keys (Tab and Space) rather than their characters. For example, #Space is Win+Space and ^!Tab is Control+Alt+Tab.

How can keys or mouse buttons be remapped so that they become different keys?

This is described on the remapping page.

How can a hotkey or hotstring be made exclusive to certain program(s)? In other words, I want a certain key to act as it normally does except when a specific window is active.

In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":

IfWinNotActive, CAD Editor
	Send, {NumpadEnter}
; Otherwise, the desired application is active, so do a custom action:
Send, abc

This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.

IfWinNotActive, CAD Editor
; Otherwise, the desired application is active, so do a custom action:
Send, abc

How can a prefix key be made to perform its native function rather than doing nothing?

Consider the following example, which makes Numpad0 into a prefix key:
Numpad0 & Numpad1::MsgBox, You pressed Numpad1 while holding down Numpad0.

Now, to make Numpad0 send a real Numpad0 keystroke whenever it wasn't used to launch a hotkey such as the above, add the following hotkey:
$Numpad0::Send, {Numpad0}

The $ prefix is needed to prevent a warning dialog about an infinite loop (since the hotkey "sends itself"). In addition, the above action occurs at the time the key is released.

How can the built-in Windows shortcut keys, such as Win+U (Utility Manager) and Win+R (Run), be changed or disabled?

Here are some examples.

My keypad has a special 000 key. Is it possible to turn it into a hotkey?

You can, but only if you're running Windows NT, 2000, XP, or beyond. This example script makes the 000 key into an equals key. You can change the action by replacing the "Send, =" line with line(s) of your choice.

Home | Download | Documentation | Changelog | Support | Forum