Function Reference


_WinAPI_RegisterHotKey

Defines a system-wide hot key

#include <WinAPISys.au3>
_WinAPI_RegisterHotKey ( $hWnd, $iID, $iModifiers, $vKey )

Parameters

$hWnd Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter
is 0, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in
the message loop.
$iID Specifies the identifier of the hot key. An application must specify an id value in the range
0x0000 through 0xBFFF.
$iModifiers Specifies keys that must be pressed in combination with the key specified by the $vKey parameter
in order to generate the WM_HOTKEY message.
The $iModifiers parameter can be a combination of the following values.
    $MOD_ALT
    $MOD_CONTROL
    $MOD_SHIFT
    $MOD_WIN

Windows 7 or later
    $MOD_NOREPEAT
$vKey Specifies the virtual-key code of the hot key ($VK_*).

Return Value

Success: True.
Failure: False, call _WinAPI_GetLastError() to get extended error information.

Remarks

When a key is pressed, the system looks for a match against all hot keys. Upon finding a match, the system posts
the WM_HOTKEY message to the message queue of the window with which the hot key is associated. If the hot key is
not associated with a window, then the WM_HOTKEY message is posted to the thread associated with the hot key.

_WinAPI_RegisterHotKey() fails if the keystrokes specified for the hot key have already been registered by
another hot key.

In Windows XP, if a hot key already exists with the same $hWnd and $iID parameters,
it is replaced by the new hot key.

In Windows Vista and subsequent versions of Windows, if a hot key already exists with the same $hWnd and $iID
parameters, it is maintained along with the new hot key. In these versions of Windows, the application must
explicitly call _WinAPI_UnregisterHotKey() to unregister the old hot key.

Related

_WinAPI_UnregisterHotKey

See Also

Search RegisterHotKey in MSDN Library.

Example

#include <APISysConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIConv.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

Opt('TrayAutoPause', 0)

OnAutoItExitRegister('OnAutoItExit')

Local $hWnd = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'))
GUIRegisterMsg($WM_HOTKEY, 'WM_HOTKEY')

; Set ALT-D
_WinAPI_RegisterHotKey($hWnd, 0x0144, $MOD_ALT, 0x44)
; Set ESC
_WinAPI_RegisterHotKey($hWnd, 0x011B, 0, 0x1B)

While 1
        Sleep(1000)
WEnd

Func WM_HOTKEY($hWnd, $iMsg, $wParam, $lParam)
        #forceref $hWnd, $iMsg, $wParam

        Switch _WinAPI_HiWord($lParam)
                Case 0x44
                        MsgBox($MB_SYSTEMMODAL, '', 'You pressed ALT-D')
                Case 0x1B
                        MsgBox($MB_SYSTEMMODAL, '', 'You pressed ESC')
                        Exit
        EndSwitch
EndFunc   ;==>WM_HOTKEY

Func OnAutoItExit()
        _WinAPI_UnregisterHotKey($hWnd, 0x0144)
        _WinAPI_UnregisterHotKey($hWnd, 0x011B)
EndFunc   ;==>OnAutoItExit