Table of Contents

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

int

VkSnapshot

Virtual key code for the Print Screen key.

public const int VkSnapshot = 44

Field Value

int

WH_KEYBOARD_LL

The identifier for low-level keyboard hook.

public const int WH_KEYBOARD_LL = 13

Field Value

int

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

isDisposing bool

True 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

nCode int

The hook code passed to the hook procedure.

wParam nint

The identifier of the keyboard message.

lParam nint

A 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

Event Type

EventHandler<GlobalKeyboardHookEventArgs>