Hotkeys, Hotstrings, and Remapping
If AutoHotkey cannot be installed the normal way, see How to Install AutoHotkey for more help.
Normally if AutoHotkey is installed, right-clicking an AutoHotkey script (.ahk) file should give the following options:
Note: On Windows 11, some of these options are usually relegated to a submenu that can be accessed by selecting "Show more options".
Sometimes these options are overridden by settings in the current user's profile, such as if Open With has been used to change the default program for opening .ahk files. This can be fixed by deleting the following registry key:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ahk\UserChoice
This can be done by applying this registry patch or by running UX\reset-assoc.ahk
from the AutoHotkey installation directory.
It may also be necessary to repair the default registry values, either by reinstalling AutoHotkey or by running UX\install.ahk
from the AutoHotkey installation directory.
There are many variations of this problem, such as:
If you've switched operating systems, it is likely that something else has also changed and may be affecting your script. For instance, if you've got a new computer, it might have different drivers or other software installed. 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.
Also refer to the following question:
By default, User Account Control (UAC) protects "elevated" programs (that is, programs which are running as admin) from being automated by non-elevated programs, since that would allow them to bypass security restrictions. Hotkeys are also blocked, so for instance, a non-elevated program cannot spy on input intended for an elevated program.
UAC may also prevent SendPlay and BlockInput from working.
Common workarounds are as follows:
Run '*UIAccess "Your script.ahk"'
."AutoHotkey32_UIA.exe" "Your script.ahk"
(but include full paths where necessary).You need to fix the error in your script before you can get your tray icon back. This will require locating the script and opening it in an editor by some other means than the tray icon.
If you are running an AutoHotkey executable file directly, the name of the script depends on the executable. For example, if you are running AutoHotkey32.exe, look for AutoHotkey32.ahk in the same directory. Note that depending on your system settings the ".ahk" part may be hidden, but the file should have an icon like
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.
Normally, you should create a script file (something.ahk) anywhere you like, and run that script file instead of running AutoHotkey.
See also Command Line Parameter "Script Filename" and Portability of AutoHotkey.exe.
For simple scripts, see Debugging a Script. To show contents of a variable, use MsgBox or ToolTip. For complex scripts, see Interactive Debugging.
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"
If the program you are trying to start is in A_WinDir "\System32"
and you are using AutoHotkey 32-bit on a 64-bit system, the File System Redirector may be interfering. To work around this, use A_WinDir "\SysNative"
instead; this is a virtual directory only visible to 32-bit programs running on 64-bit systems.
Short answer: Save the script as UTF-8, preferably with BOM.
Non-ASCII characters are represented by different binary values depending on the chosen encoding. In order for such characters to be interpreted correctly, your text editor and AutoHotkey must be on the same codepage, so to speak. AutoHotkey v2 defaults to UTF-8 for all script files, although this can be overridden with the /CP command line switch.
It is recommended to save the script as UTF-8 with BOM (byte order mark) to ensure that editors (and maybe other applications) can determine with certainty that the file is indeed UTF-8. Without BOM, the editor has to guess the encoding of the file, which can sometimes be wrong.
To save as UTF-8 in Notepad, select UTF-8 or UTF-8 with BOM from the Encoding drop-down in the Save As dialog. Note that Notepad in Windows 10 v1903 and later defaults to UTF-8 (without BOM).
To read other UTF-8 files which lack a byte order mark (BOM), use FileEncoding "UTF-8-RAW"
, the *P65001
option with FileRead, or "UTF-8-RAW"
for the third parameter of FileOpen. The -RAW
suffix can be omitted, but in that case any newly created files will have a BOM.
Note that INI files accessed with the standard INI functions do not support UTF-8; they must be saved as ANSI or UTF-16.
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.
Use SendPlay via the SendPlay function, SendMode Play and/or the hotstring option SP.
SendPlay "abc"
SendMode "Play" Send "abc"
:SP:btw::by the way ; or #Hotstring SP ::btw::by the way
Note: SendPlay may have no effect at all if User Account Control is enabled, even if the script is running as an administrator.
Increase SetKeyDelay. For example:
SetKeyDelay 0, 50 SetKeyDelay 0, 50, "Play"
Try ControlSend, which might work in cases where the other Send modes fail:
ControlSend "abc",, GameTitle
Try the down and up event of a key with the various send methods:
Send "{KEY Down}{KEY Up}"
Try the down and up event of a key with a Sleep between them:
Send "{KEY down}" Sleep 10 ; Try various milliseconds. Send "{KEY up}"
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:
ProcessSetPriority "High"
Although it is certainly possible that the file has been infected, most often these alerts are false positives, meaning that the antivirus program is mistaken. One common suggestion is to upload the file to an online service such as virustotal or Jotti and see what other antivirus programs have to say. If in doubt, you could send the file to the vendor of your antivirus software for confirmation. This might also help us and other AutoHotkey users, as the vendor may confirm it is a false positive and fix their product to play nice with AutoHotkey.
False positives might be more common for compiled scripts which have been compressed, such as with UPX (default for AutoHotkey 1.0 but not 1.1) or MPRESS (optional for AutoHotkey 1.1). As the default AutoHotkey installation does not include a compressor, compiled scripts are not compressed by default.
See download page of AutoHotkey.
See Portability of AutoHotkey.exe.
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 A_ComSpec ' /c dir > C:\My Temp File.txt' VarToContainContents := FileRead("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 the Shell.Exec() method as shown in the examples for the Run function.
First, here is an example that closes another script:
DetectHiddenWindows True ; 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 "ScriptFileName.ahk - AutoHotkey" ; Update this to reflect the script's name (case-sensitive).
To suspend, pause or reload another script, replace the last line above with one of these:
PostMessage 0x0111, 65305,,, "ScriptFileName.ahk - AutoHotkey" ; Suspend. PostMessage 0x0111, 65306,,, "ScriptFileName.ahk - AutoHotkey" ; Pause. PostMessage 0x0111, 65303,,, "ScriptFileName.ahk - AutoHotkey" ; Reload.
To pause or resume the entire script at the press of a key, assign a hotkey to the Pause function 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). { static KeepWinZRunning := false 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. } #MaxThreadsPerHotkey 1
Rajat created this script.
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 if StatusBarWait("Done", 30) MsgBox "The page is done loading." else MsgBox "The wait timed out or the window was closed."
The DateAdd function 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:
Result := DateAdd(VarContainingTimestamp, -7, "days")
To determine the amount of time between two dates or times, see DateDiff, 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 function to create a custom date/time string.
Use FormatTime or built-in variables for date and time.
Use ControlSend.
See Automating Winamp.
Here is an example.
In the example section of Edit you will find a script that allows you to change the default editor.
Use Gui.Submit. For Example:
MyGui := Gui() MyGui.Add("Text",, "Enter some Text and press Submit:") MyGui.Add("Edit", "vMyEdit") MyGui.Add("Button",, "Submit").OnEvent("Click", Submit) MyGui.Show Submit(*) { Saved := MyGui.Submit(false) MsgBox "Content of the edit control: " Saved.MyEdit }
See GDI+ standard library by tic. It's also possible with some rudimentary methods using Gui, but in a limited way.
Use WinWait, WinWaitClose or WinWait[Not]Active.
There are also user-created solutions such as OnWin.ahk and [How to] Hook on to Shell to receive its messages.
There are several ways to make a script (or any program) launch automatically every time you start your PC. The easiest is to place a shortcut to the script in the Startup folder:
shell:startup
and click OK or Enter. This will open the Startup folder for the current user. To instead open the folder for all users, enter shell:common startup
(however, in that case you must be an administrator to proceed).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 Ctrl+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.
Use the names of the keys (Tab and Space) rather than their characters. For example, #Space
is Win+Space and ^!Tab
is Ctrl+Alt+Tab.
This is described on the remapping page.
Use built-in variables for hotkeys as follows:
~Ctrl:: { if (ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < 200) MsgBox "double-press" }
The preferred method is #HotIf. For example:
#HotIf WinActive("ahk_class Notepad") ^a::MsgBox "You pressed Control-A while Notepad is active."
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.
Here are some examples.
Use the script by polyethene (examples are included).
See Special Keys.
You can. This example script makes 000 into an equals key. You can change the action by replacing the Send "="
line with line(s) of your choice.