Creates, modifies, enables, or disables a hotkey while the script is running.
Hotkey, KeyName [, Label, Options]
Hotkey, IfWinActive/Exist [, WinTitle, WinText]
Name of the hotkey's activation key, including any modifier symbols. For example, specify #c for the Win+C hotkey.
If KeyName already exists as a hotkey, that hotkey will be updated with the values of the command's other parameters.
KeyName can also be the name of an existing hotkey label (i.e. a double-colon label), which will cause that hotkey to be updated with the values of the command's other parameters.
When specifying an existing hotkey, KeyName is not case sensitive. However, the names of keys must be spelled the same as in the existing hotkey (e.g. Esc is not the same as Escape for this purpose). Also, the order of modifier symbols such as ^!+# does not matter.
When a hotkey is first created -- either by the Hotkey command or a double-colon label in the script -- its key name and the ordering of its modifier symbols becomes the permanent name of that hotkey as reflected by A_ThisHotkey. This name does not change even if the Hotkey command later accesses the hotkey with a different symbol ordering.
The label name whose contents will be executed (as a new thread) when the hotkey is pressed. Both normal labels and hotkey/hotstring labels can be used. The trailing colon(s) should not be included. If Label is dynamic (e.g. %VarContainingLabelName%), IsLabel(VarContainingLabelName) may be called beforehand to verify that the label exists.
This parameter can be left blank if KeyName already exists as a hotkey, in which case its label will not be changed. This is useful to change only the hotkey's Options.
If the label is specified but the hotkey is disabled from a previous use of this command, the hotkey will remain disabled. To prevent this, include the word ON in Options.
This parameter can also be one of the following special values:
On: The hotkey becomes enabled. No action is taken if the hotkey is already On.
Off: The hotkey becomes disabled. No action is taken if the hotkey is already Off.
Toggle: The hotkey is set to the opposite state (enabled or disabled).
AltTab (and others): These are special Alt-Tab hotkey actions that are described here.
A string of zero or more of the following letters with optional spaces in between. For example: UseErrorLevel B0
On: Enables the hotkey if it is currently disabled.
Off: Disables the hotkey if it is currently enabled. This is typically used to create a hotkey in an initially-disabled state.
B or B0: Specify the letter B to buffer the hotkey as described in #MaxThreadsBuffer. Specify B0 (B with the number 0) to disable this type of buffering.
Pn: Specify the letter P followed by the hotkey's thread priority. If the P option is omitted when creating a hotkey, 0 will be used.
Tn: Specify the letter T followed by a the number of threads to allow for this hotkey as described in #MaxThreadsPerHotkey. For example: T5
If either or both of the B and T option letters are omitted and the hotkey already exists, those options will not be changed. But if the hotkey does not yet exist -- that is, it is about to be created by this command -- the options will default to those most recently in effect. For example, the instance of #MaxThreadsBuffer that occurs closest to the bottom of the script will be used. If #MaxThreadsBuffer does not appear in the script, its default setting (OFF in this case) will be used. This behavior also applies to #IfWin: the bottommost occurrence applies to newly created hotkeys unless "Hotkey IfWin" has executed since the script started.
|(IfWinNotActive and IfWinNotExist are also supported). These sub-commands make all subsequently-created hotkeys context sensitive. See below for details.|
Within these parameters, any variable reference such as %var% becomes permanent the moment the command finishes. In other words, subsequent changes to the contents of the variable are not seen by existing IfWin hotkeys.
ErrorLevel is changed only when: 1) the first parameter is IfWin[Not]Active/Exist, in which case it is set to 1 if there was a problem or 0 otherwise; or 2) the word UseErrorLevel is present in the Options parameter.
|1||The Label parameter specifies a nonexistent label name.|
|2||The KeyName parameter specifies one or more keys that are either not recognized or not supported by the current keyboard layout/language.|
|3||Unsupported prefix key. For example, using the mouse wheel as a prefix in a hotkey such as WheelDown & Enter is not supported.|
|4||The KeyName parameter is not suitable for use with the AltTab or ShiftAltTab actions. A combination of two keys is required. For example: RControl & RShift::AltTab|
|5||The command attempted to modify a nonexistent hotkey.|
|6||The command attempted to modify a nonexistent variant of an existing hotkey. To solve this, use "Hotkey IfWin" to set the criteria to match those of the hotkey to be modified.|
|50||Windows 95/98/Me: The command completed successfully but the operating system refused to activate the hotkey. This is usually caused by the hotkey being "in use" by some other script or application (or the OS itself). This occurs only on Windows 95/98/Me because on other operating systems, the program will resort to the keyboard hook to override the refusal.|
|51||Windows 95/98/Me: The command completed successfully but the hotkey is not supported on Windows 95/98/Me. For example, mouse hotkeys and prefix hotkeys such as "a & b" are not supported.|
|98||Creating this hotkey would exceed the 1000-hotkey-per-script limit (however, each hotkey can have an unlimited number of variants, and there is no limit to the number of hotstrings).|
|99||Out of memory. This is very rare and usually happens only when the operating system has become unstable.|
Tip: The UseErrorLevel option can be used to test for the existence of a hotkey variant. For example:
Hotkey, ^!p,, UseErrorLevel if ErrorLevel in 5,6 MsgBox The hotkey does not exist or it has no variant for the current IfWin criteria.
If the goal is to disable selected hotkeys or hotstrings automatically based on the type of window that is active, Hotkey, ^!c, Off is usually less convenient than using #IfWinActive/Exist (or their dynamic counterparts "Hotkey IfWinActive/Exist" below).
Creating hotkeys via double-colon labels performs better than using the Hotkey command because the hotkeys can all be enabled as a batch when the script starts (rather than one by one). Therefore, it is best to use this command to create only those hotkeys whose key names are not known until after the script has started running. One such case is when a script's hotkeys for various actions are configurable via an INI file.
A given label can be the target of more than one hotkey. If it is known that a label was called by a hotkey, you can determine which hotkey by checking the built-in variable A_ThisHotkey.
If the script is suspended, newly added/enabled hotkeys will also be suspended until the suspension is turned off (unless they are exempt as described in the Suspend section).
The keyboard and/or mouse hooks will be installed or removed if justified by the changes made by this command.
Although the Hotkey command cannot directly enable or disable hotkeys in scripts other than its own, in most cases it can override them by creating or enabling the same hotkeys. Whether this works depends on a combination of factors: 1) Whether the hotkey to be overridden is a hook hotkey in the other script (non-hook hotkeys can always be overridden except on Win9x); 2) The fact that the most recently started script's hotkeys generally take precedence over those in other scripts (therefore, if the script intending to override was started most recently, its override should always succeed); 3) Whether the enabling or creating of this hotkey will newly activate the keyboard or mouse hook (if so, the override will always succeed).
Once a script has at least one hotkey, it becomes persistent, meaning that ExitApp rather than Exit should be used to terminate it. Hotkey scripts are also automatically #SingleInstance unless #SingleInstance Off has been specified.
The "Hotkey IfWin" commands allow context-sensitive hotkeys to be created and modified while the script is running (by contrast, the #IfWinActive/Exist directives are positional and take effect before the script begins executing). For example:
Hotkey, IfWinActive, ahk_class Notepad Hotkey, ^!e, MyLabel ; Creates a hotkey that works only in Notepad.
Using "Hotkey IfWin" puts context sensitivity into effect for all subsequently created or modified hotkeys. In addition, each IfWin sub-command is mutually exclusive; that is, only the most recent one will be in effect.
To turn off context sensitivity (that is, to make subsequently-created hotkeys work in all windows), specify any IfWin sub-command but omit the WinTitle/Text parameters. For example: Hotkey, IfWinActive
If "Hotkey IfWin" is never used by a script, the bottommost use of the #IfWin directive (if any) will be in effect for the Hotkey command.
When a mouse or keyboard hotkey is disabled via IfWin, it performs its native function; that is, it passes through to the active window as though there is no such hotkey. There are two exceptions: 1) Windows 95/98/Me: pressing an IfWin-disabled hotkey has no effect (not even its native function); and 2) Joystick hotkeys: although IfWin works, it never prevents other programs from seeing the press of a button.
A particular hotkey can be created more than once if each definition has different IfWin criteria. These are known as hotkey variants. For example:
Hotkey, IfWinActive, ahk_class Notepad Hotkey, ^!c, MyLabelForNotepad Hotkey, IfWinActive, ahk_class WordPadClass Hotkey, ^!c, MyLabelForWordPad Hotkey, IfWinActive Hotkey, ^!c, MyLabelForAllOtherWindows
If more than one variant of a hotkey is eligible to fire, only the one created earliest will fire. The exception to this is the global variant (the one with no IfWin criteria): It always has the lowest precedence, and thus will fire only if no other variant is eligible.
When creating duplicate hotkeys, the order of modifier symbols such as ^!+# does not matter. For example, ^!c is the same as !^c. However, keys must be spelled consistently. For example, Esc is not the same as Escape for this purpose (though the case does not matter). Finally, any hotkey with a wildcard prefix (*) is entirely separate from a non-wildcard one; for example, *F1 and F1 would each have their own set of variants.
For more information about IfWin hotkeys, see #IfWin's General Remarks.
Hotkey Symbols, #IfWinActive/Exist, #MaxThreadsBuffer, #MaxThreadsPerHotkey, Suspend, IsLabel(), Threads, Thread, Critical, Gosub, Return, Menu, SetTimer
Hotkey, ^!z, MyLabel return MyLabel: MsgBox You pressed %A_ThisHotkey%. return ; Other examples: Hotkey, RCtrl & RShift, AltTab ; Makes RCtrl & RShift operate like Alt-Tab. Hotkey, #c, On ; Re-enables the Win-C hotkey. Hotkey, $+#c, Off ; Disables the Shift-Win-C hotkey. Hotkey, ^!a, , T5 ; Changes the hotkey to allow 5 threads. Hotkey, IfWinActive, ahk_class Notepad Hotkey, ^!c, MyLabelForNotepad ; Creates Ctrl-Alt-C as a hotkey that works only in Notepad.
Homepage | Command List