Frequently Asked Questions (FAQ)

If your question is not in the list below, check the community-driven FAQ on autoHotkey.net.

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 variable names enclosed in percent signs?

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 traditional (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/backtick. 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

Where can I find the official build, or older releases?

See download page of AutoHotkey.

Why do some lines in my script never execute?

Any lines you want to execute immediately when the script starts should appear at the top of the script, prior to the first hotkey, hotstring, or Return. For details, see auto-execute section.

Also, a hotkey that executes more than one line must list its first line beneath the hotkey, not on the same line. For example:

#space::  ; Win+Spacebar
Run Notepad
WinWaitActive Untitled - Notepad
WinMaximize
return

Why does my script run fine in XP but not in Vista or Windows 7 or 8?

Since you've switched operating systems, it is likely that something else may have also changed which could be affecting your script. For instance, if you've got a new computer, it might differ in some way to the old computer. If you've also updated to a newer version of AutoHotkey, find out which version you had before and then check the changelog and compatibility notes.

A common problem when moving from XP to Vista or a later version of Windows is that programs which are "elevated" (run as administrator) block hotkeys or refuse to be automated. There are at least three workarounds:

SoundGet, SoundSet, SoundGetWaveVolume and SoundSetWaveVolume behave differently on Vista and later than on earlier versions of Windows. In particular, device numbers are different and some components may be unavailable. Behaviour depends on the audio drivers, which are necessarily different to the ones used on XP. The soundcard analysis script can be used to find the correct device numbers.

I can't edit my script via tray icon because it won't start due to an error. What do I do?

You need to fix the error in your script before you can get your tray icon back. But first, you need to find the script file.

Look for AutoHotkey.ahk in the following directories:

If you are running another AutoHotkey executable directly, the name of the script depends on the executable. For example, if you are running AutoHotkeyU32.exe, look for AutoHotkeyU32.ahk. Note that depending on your system settings the ".ahk" part may be hidden, but the file should have an icon like [H]

You can usually edit a script file by right clicking it and selecting Edit Script. If that doesn't work, you can open the file in Notepad or another editor.

If you launch AutoHotkey from the Start menu or by running AutoHotkey.exe directly (without command line parameters), it will look for a script in one of the locations shown above. Alternatively, you can create a script file (something.ahk) anywhere you like, and run the script file instead of running AutoHotkey.

See also Command Line Parameter "Script Filename" and Portability of AutoHotkey.exe.

How can I find and fix errors in my code?

For simple scripts, see Debugging a Script. To show contents of a variable, use MsgBox or ToolTip. For complex scripts, see Interactive Debugging.

Can I run AHK from a USB drive?

See Portability of AutoHotkey.exe. Note that if you use auto-included function libraries, AutoHotkey.exe and the Lib folder must be up one level from Ahk2Exe.exe (e.g. \AutoHotkey.exe vs \Compiler\Ahk2Exe.exe). Also note that Ahk2Exe saves settings to the following registry key: HKEY_CURRENT_USER\Software\AutoHotkey\Ahk2ExeCommand.

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, %A_ProgramFiles%\Some Application\App.exe, %A_ProgramFiles%\Some Application

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

Testing shows that due to file caching, a temporary file can be very fast for relatively small outputs. In fact, if the file is deleted immediately after use, it often does not actually get written to disk. For example:

RunWait %comspec% /c dir > C:\My Temp File.txt
FileRead, VarToContainContents, C:\My Temp File.txt
FileDelete, C:\My Temp File.txt

To avoid using a temporary file (especially if the output is large), consider using CmdRet or StdoutToVar.

How can a script close, pause, or suspend other script(s)?

First, here is an example that closes another script:

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).

To suspend or pause another script, replace the last line above with one of these:

PostMessage, 0x111, 65305,,, Script's File Name.ahk - AutoHotkey  ; Suspend.
PostMessage, 0x111, 65306,,, Script's File Name.ahk - AutoHotkey  ; Pause.

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 working example, which is a hotkey that 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 (change this hotkey to suit your preferences).
#MaxThreadsPerHotkey 1
if KeepWinZRunning  ; This means an underlying thread is already running the loop below.
{
    KeepWinZRunning := false  ; Signal that thread's loop to stop.
    return  ; End this thread so that the one underneath will resume and see the change made by the line above.
}
; Otherwise:
KeepWinZRunning := true
Loop
{
    ; The next four lines are the action you want to repeat (update them to suit your preferences):
    ToolTip, Press Win-Z again to stop this from flashing.
    Sleep 1000
    ToolTip
    Sleep 1000
    ; But leave the rest below unchanged.
    if not KeepWinZRunning  ; The user signaled the loop to stop by pressing Win-Z again.
        break  ; Break out of this loop.
}
KeepWinZRunning := false  ; Reset in preparation for the next press of this hotkey.
return

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

If a script's Hotkeys, Clicks, 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 to detect when a web page is finished loading?

With Internet Explorer, perhaps the most reliable method is to use DllCall and COM as demonstrated at www.autohotkey.com/forum/topic19256.html. On a related note, the contents of the address bar and status bar can be retrieved as demonstrated at www.autohotkey.com/forum/topic19255.html.

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

Run, www.yahoo.com
MouseMove, 0, 0  ; Prevents the status bar from showing a mouse-hover link instead of "Done".
WinWait, Yahoo! - 
WinActivate 
StatusBarWait, Done, 30
if ErrorLevel
   MsgBox The wait timed out or the window was closed. 
else 
   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.

How can I send the current Date and/or Time?

Use FormatTime or built-in variables for date and time.

How can I send text to a window which isn't active or isn't visible?

Use ControlSend.

Why do Hotstrings, Send, and Click have no effect in certain games?

Not all games allow AHK to send keys and clicks or receive pixel colors.

But there are some alternatives, try all the solutions mentioned below. If all these fail, it may not be possible for AHK to work with your game. Sometimes games have a hack and cheat prevention measure, such as GameGuard and Hackshield. If they do, there is a high chance that AutoHotkey will not work with that game.

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.

How can I change the default editor, which is accessible via context menu or tray icon?

In the example section of Edit you will find a script that allows you to change the default editor.

How can I save the contents of my GUI associated variables?

Use Gui Submit. For Example:

Gui, Add, Text,, Enter some Text and press Submit:
Gui, Add, Edit, vAssociatedVar
Gui, Add, Button,, Submit
Gui, Show
Return

ButtonSubmit:
Gui, Submit, NoHide
MsgBox, Content of the edit control: %AssociatedVar%
Return

Can I draw something with AHK?

See GDI+ standard library by tic. It's also possible with some rudimentary methods using Gui, but in a limited way.

How can I start an action when a window appears, closes or becomes [in]active?

Use WinWait, WinWaitClose or WinWait[Not]Active. See the community-driven FAQ (windows section) for more possibilities.

My antivirus program flagged AHK as malware. Does it really contain a virus?

Of course not. Antivirus programs may incorrectly recognize AHK as malware (called false positive), when the script is compiled with a compressor like UPX (default in AHK 1.0, removed in AHK 1.1) or MPRESS (optional in AHK 1.1). Compressors can reduce the size of compiled scripts. If you don't necessarily need this for your compiled executable, you should avoid using a compressor to prevent false alarms from the AV program for you and potential other users.

Hotkeys, Hotstrings, and Remapping

How do I put my hotkeys and hotstrings into effect automatically every time I start 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. Find the script file, select it, and press Control-C.
  2. Right-click the Start button (typically at the lower left corner of the screen) 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?

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 that you want to use -- 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+F1, you can then indirectly configure that button as a hotkey by using ^F1:: in a 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 do I detect the double press of a key or button?

Use built-in variables for hotkeys as follows:

~Ctrl::
    if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < 200)
        MsgBox double-press
return

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.

The preferred method is #IfWinActive. For example:

#IfWinActive, ahk_class Notepad
^a::MsgBox You pressed Control-A while Notepad is active.

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.

Can I use wildcards or regular expressions in Hotstrings?

Use the script by polyethene (examples are included).

How can I use a hotkey that is not in my keyboard layout?

See Special Keys.

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

Yes. 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.