Class WindowsHook
Wraps API SetWindowsHookEx.
public sealed class WindowsHook : IDisposable
Remarks
Hooks are used to receive notifications about various system events. Keyboard and mouse input, window messages, various window events.
Threads that use hooks must process Windows messages. For example have a window/dialog/messagebox, or use a "wait-for" function that dispatches messages or has such option (see Seconds.DoEvents).
important
The variable should be disposed when don't need, or at least unhooked, either explicitly (call Dispose or Unhook in same thread) or with using
. Can do it in hook procedure.
warning
Avoid many hooks. Each low-level keyboard or mouse hook makes the computer slower, even if the hook procedure is fast. On each input event (key down, key up, mouse move, click, wheel) Windows sends a message to your thread.
To receive hook events is used a callback function, aka hook procedure. Hook procedures of some hook types can block some events (call BlockEvent or return true
). Blocked events are not sent to apps and older hooks.
Delegates of hook procedures are protected from GC until called Dispose or until the thread ends, even of unreferenced WindowsHook variables.
UI element functions may fail in hook procedures of low-level keyboard and mouse hooks. Workarounds exist.
Exists an alternative way to monitor keyboard or mouse events - raw input API. Good: less overhead; can detect from which device the input event came. Bad: cannot block events; incompatible with low-level keyboard hooks. This library does not have functions to make the API easier to use.
Namespace: Au.More
Assembly: Au.dll
Properties
Name | Description |
---|---|
IsSet | Returns |
LowLevelHooksTimeout | Gets the max time in milliseconds allowed by Windows for low-level keyboard and mouse hook procedures. |
Methods
Name | Description |
---|---|
Dispose() | Calls WindowsHook.Unhook and disposes this object. |
Hook(int) | Sets the hook. |
Keyboard(Action<Keyboard>, bool, bool) | Sets a low-level keyboard hook (WH_KEYBOARD_LL). See API SetWindowsHookEx. |
Mouse(Action<Mouse>, bool, bool) | Sets a low-level mouse hook (WH_MOUSE_LL). See API SetWindowsHookEx. |
Restore() | Rehooks this low-level keyboard or mouse hook. |
ThreadCallWndProc(Action<ThreadCallWndProc>, int, bool) | Sets a WH_CALLWNDPROC hook for a thread of this process. See API SetWindowsHookEx. |
ThreadCallWndProcRet(Action<ThreadCallWndProcRet>, int, bool) | Sets a WH_CALLWNDPROCRET hook for a thread of this process. See API SetWindowsHookEx. |
ThreadCbt(Func<ThreadCbt, bool>, int, bool) | Sets a WH_CBT hook for a thread of this process. See API SetWindowsHookEx. |
ThreadGetMessage(Action<ThreadGetMessage>, int, bool) | Sets a WH_GETMESSAGE hook for a thread of this process. See API SetWindowsHookEx. |
ThreadKeyboard(Func<ThreadKeyboard, bool>, int, bool) | Sets a WH_GETMESSAGE hook for a thread of this process. See API SetWindowsHookEx. |
ThreadMouse(Func<ThreadMouse, bool>, int, bool) | Sets a WH_MOUSE hook for a thread of this process. See API SetWindowsHookEx. |
Unhook() | Removes the hook. |