Function Reference


_WinAPI_SetWindowsHookEx

Installs an application-defined hook procedure into a hook chain

#include <WinAPI.au3>
_WinAPI_SetWindowsHookEx ( $iHook, $pProc, $hDll [, $iThreadId = 0] )

Parameters

$iHook Specifies the type of hook procedure to be installed. This parameter can be one of the following values:
    $WH_CALLWNDPROC - Installs a hook procedure that monitors messages before the system sends them to the destination window procedure
    $WH_CALLWNDPROCRET - Installs a hook procedure that monitors messages after they have been processed by the destination window procedure
    $WH_CBT - Installs a hook procedure that receives notifications useful to a computer-based training (CBT) application
    $WH_DEBUG - Installs a hook procedure useful for debugging other hook procedures
    $WH_FOREGROUNDIDLE - Installs a hook procedure that will be called when the application's foreground thread is about to become idle
    $WH_GETMESSAGE - Installs a hook procedure that monitors messages posted to a message queue
    $WH_JOURNALPLAYBACK - Installs a hook procedure that posts messages previously recorded by a $WH_JOURNALRECORD hook procedure
    $WH_JOURNALRECORD - Installs a hook procedure that records input messages posted to the system message queue
    $WH_KEYBOARD - Installs a hook procedure that monitors keystroke messages
    $WH_KEYBOARD_LL - Installs a hook procedure that monitors low-level keyboard input events
    $WH_MOUSE - Installs a hook procedure that monitors mouse messages
    $WH_MOUSE_LL - Installs a hook procedure that monitors low-level mouse input events
    $WH_MSGFILTER - Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar
    $WH_SHELL - Installs a hook procedure that receives notifications useful to shell applications
    $WH_SYSMSGFILTER - Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar
$pProc Pointer to the hook procedure. If the $iThreadId parameter is zero or specifies the identifier of a thread created by a different process,
the $pProc parameter must point to a hook procedure in a DLL.
Otherwise, $pProc can point to a hook procedure in the code associated with the current process
$hDll Handle to the DLL containing the hook procedure pointed to by the $pProc parameter.
The $hMod parameter must be set to NULL if the $iThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process
$iThreadId [optional] Specifies the identifier of the thread with which the hook procedure is to be associated.
If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread

Return Value

Success: Handle to the hook procedure
Failure: 0, call _WinAPI_GetLastError() to get extended error informationt

Related

_WinAPI_CallNextHookEx, _WinAPI_UnhookWindowsHookEx

See Also

Search SetWindowsHookEx in MSDN Library.

Example

#include <MsgBoxConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $g_hHook, $g_hStub_KeyProc, $g_sBuffer = ""

Example()

Func Example()
    OnAutoItExitRegister("Cleanup")

    Local $hMod

    $g_hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hMod = _WinAPI_GetModuleHandle(0)
    $g_hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($g_hStub_KeyProc), $hMod)

    MsgBox($MB_SYSTEMMODAL, "", "Click OK, then in notepad type..." & _
            @CRLF & @CRLF & "Jon" & @CRLF & "AutoIt" & @CRLF & @CRLF & "Press Esc to exit script")

    Run("notepad.exe")
    WinWait("[CLASS:Notepad]")
    WinActivate("[CLASS:Notepad]")

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>Example

Func EvaluateKey($iKeycode)
    If (($iKeycode > 64) And ($iKeycode < 91)) _ ; a - z
            Or (($iKeycode > 96) And ($iKeycode < 123)) _ ; A - Z
            Or (($iKeycode > 47) And ($iKeycode < 58)) Then ; 0 - 9
        $g_sBuffer &= Chr($iKeycode)
        Switch $g_sBuffer
            Case "Jon"
                ToolTip("What can you say?")
            Case "AutoIt"
                ToolTip("AutoIt Rocks")
        EndSwitch
    ElseIf ($iKeycode > 159) And ($iKeycode < 164) Then
        Return
    ElseIf ($iKeycode = 27) Then ; esc key
        Exit
    Else
        $g_sBuffer = ""
    EndIf
EndFunc   ;==>EvaluateKey

; ===========================================================
; callback function
; ===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($g_hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN Then
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
    Else
        Local $iFlags = DllStructGetData($tKEYHOOKS, "flags")
        Switch $iFlags
            Case $LLKHF_ALTDOWN
                ConsoleWrite("$LLKHF_ALTDOWN" & @CRLF)
            Case $LLKHF_EXTENDED
                ConsoleWrite("$LLKHF_EXTENDED" & @CRLF)
            Case $LLKHF_INJECTED
                ConsoleWrite("$LLKHF_INJECTED" & @CRLF)
            Case $LLKHF_UP
                ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @CRLF)
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($g_hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($g_hHook)
    DllCallbackFree($g_hStub_KeyProc)
EndFunc   ;==>Cleanup