# Function Reference

# HotKeySet

Sets a hotkey that calls a user function.

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

## Parameters

key | The key combination to use as the hotkey. Same format as Send(). See remarks.
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.

## Remarks

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.
Keyboard with 102 keys as the Hungarian keyboard need to use "{OEM_102}" to capture the "í" key.
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:

ASC nnnn | Cannot be used as in Send(). It will use "A" first char as Send() does when the "{...}" is invalid.
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}")
Send("{Esc}")
HotKeySet("{Esc}", "captureEsc")
EndFunc
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.

## Related

GUISetAccelerators, Send

## Example

### Example 1

    #include <MsgBoxConstants.au3>

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

    Global $g_bPaused = False

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

    While 1
        Sleep(100)
    WEnd

    Func TogglePause()
        $g_bPaused = Not $g_bPaused
        While $g_bPaused
            Sleep(100)
            ToolTip('Script is "Paused"', 0, 0)
        WEnd
        ToolTip("")
    EndFunc   ;==>TogglePause

    Func Terminate()
        Exit
    EndFunc   ;==>Terminate

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

### Example 2, @HotKeyPressed usage

    #include <MsgBoxConstants.au3>

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

    Global $g_bPaused = False

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

    While 1
        Sleep(100)
    WEnd

    Func HotKeyPressed()
        Switch @HotKeyPressed ; The last hotkey pressed.
            Case "{PAUSE}" ; String is the {PAUSE} hotkey.
                $g_bPaused = Not $g_bPaused
                While $g_bPaused
                    Sleep(100)
                    ToolTip('Script is "Paused"', 0, 0)
                WEnd
                ToolTip("")

            Case "{ESC}" ; String is the {ESC} hotkey.
                Exit

            Case "+!d" ; String is the Shift-Alt-d hotkey.
                MsgBox($MB_SYSTEMMODAL, "", "This is a message.")

        EndSwitch
    EndFunc   ;==>HotKeyPressed