Class GlobalKeyboardHook
- Namespace
- SunamoPInvoke.PInvoke
- Assembly
- SunamoPInvoke.dll
Provides a global keyboard hook for capturing keyboard events system-wide. Based on https://gist.github.com/Stasonix
public class GlobalKeyboardHook : W32Base, IDisposable
- Inheritance
-
GlobalKeyboardHook
- Implements
- Inherited Members
- Extension Methods
Constructors
GlobalKeyboardHook()
Initializes a new instance of the GlobalKeyboardHook class. Does not distinguish between uppercase and lowercase letters, even when caps lock is on.
public GlobalKeyboardHook()
Exceptions
- Win32Exception
Thrown when the hook installation fails.
Fields
LlkhfAltdown
Low-level keyboard hook flag indicating Alt key is down.
public const int LlkhfAltdown = 32
Field Value
VkSnapshot
Virtual key code for the Print Screen key.
public const int VkSnapshot = 44
Field Value
WH_KEYBOARD_LL
The identifier for low-level keyboard hook.
public const int WH_KEYBOARD_LL = 13
Field Value
Methods
Dispose()
Releases all resources used by the GlobalKeyboardHook.
public void Dispose()
Dispose(bool)
Releases the keyboard hook and frees the User32 library.
protected virtual void Dispose(bool isDisposing)
Parameters
isDisposingboolTrue if called from Dispose, false if called from finalizer.
~GlobalKeyboardHook()
Finalizer that releases unmanaged resources.
protected ~GlobalKeyboardHook()
LowLevelKeyboardProc2(int, nint, nint)
Callback for the low-level keyboard hook procedure.
public nint LowLevelKeyboardProc2(int nCode, nint wParam, nint lParam)
Parameters
nCodeintThe hook code passed to the hook procedure.
wParamnintThe identifier of the keyboard message.
lParamnintA pointer to a LowLevelKeyboardInputEvent structure.
Returns
- nint
A non-zero value to prevent the message from being passed to the target window.
Events
KeyboardPressed
Occurs when a keyboard key is pressed or released.
public event EventHandler<GlobalKeyboardHookEventArgs>? KeyboardPressed