Function Reference


Sets a hotkey that calls a user function.

HotKeySet ( "key" [, "function"] )


key The key combination to use as the hotkey. Same format as Send().
function [optional] The name of the function to call when the key is pressed. Not specifying this parameter will unset a previous hotkey.

Return Value

Success: 1.
Failure: 0.


It is recommended to use lower-case keys/characters (e.g. "b" and not "B") when setting hotkeys to avoid errors as with some keyboard layouts upper and lower case keys may be mapped differently.

If two AutoIt scripts set the same hotkeys, you should avoid running those scripts simultaneously as the second script cannot capture the hotkey unless the first script terminates or unregisters the key prior to the second script setting the hotkey. If the scripts use GUIs, then consider using GUISetAccelerators as these keys are only active when the parent GUI is active.

A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes or is interrupted. Exceptions are as follows:
1) If the current function is a "blocking" function, then the key-presses are buffered and execute as soon as the blocking function completes. MsgBox() and FileSelectFolder() are examples of blocking functions. Try the behavior of Shift-Alt-d in the Example.
2) If you have paused the script by clicking on the AutoIt Tray icon, any hotkeys pressed during this paused state are ignored.

The following hotkeys cannot be set:

Ctrl+Alt+Delete It is reserved by Windows
F12 It is also reserved by Windows, according to its API.
NumPad's Enter Key Instead, use {Enter} which captures both Enter keys on the keyboard.
Win+B,D,E,F,L,M,R,U; and Win+Shift+M These are built-in Windows shortcuts. Note: Win+B and Win+L might only be reserved on Windows XP and above.
Alt, Ctrl, Shift, Win These are the modifier keys themselves!
Other Any global hotkeys a user has defined using third-party software, any combos of two or more "base keys" such as '{F1}{F2}', and any keys of the form '{LALT}' or '{ALTDOWN}'.

When you set a hotkey, AutoIt captures the key-press and does not pass it on to the active application, with one exception: the Lock keys (NumLock, CapsLock, and ScrollLock) still toggle their respective state!
To Send() a key combination which will trigger a HotKeySet() event, either use ControlSend() or unregister the HotKeySet() event, otherwise, the Send() event may trigger an infinite loop.

; capture and pass along a keypress
HotKeySet("{Esc}", "captureEsc")
Func captureEsc()
    ; ... can do stuff here
    HotKeySet("{Esc}", "captureEsc")

The called function can not be given parameters. They will be ignored.

@HotKeyPressed macro can be used inside the function to handle several keys in the same function.


Send, GUISetAccelerators


#include <MsgBoxConstants.au3>

; Press Esc to terminate script, Pause/Break to "pause"

Global $fPaused = False

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage") ; Shift-Alt-d

While 1

Func TogglePause()
    $fPaused = Not $fPaused
    While $fPaused
        ToolTip('Script is "Paused"', 0, 0)
EndFunc   ;==>TogglePause

Func Terminate()
EndFunc   ;==>Terminate

Func ShowMessage()
    MsgBox($MB_SYSTEMMODAL, "", "This is a message.")
EndFunc   ;==>ShowMessage