Class KeyboardHook
Provides a mechanism for hooking all keyboard events within the operating system.
public static class KeyboardHook
- Inheritance
-
KeyboardHook
- Inherited Members
Properties
IsInstalled
Indicates whether KeyboardHook has been installed and is capturing keyboard events.
public static bool IsInstalled { get; }
Property Value
Methods
Install()
Installs the keyboard hook, capturing all global keyboard events.
public static void Install()
Remarks
This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.
The hook procedure should process a message in less time than the data entry specified in the LowLevelHooksTimeout
value in the following registry key:
HKEY_CURRENT_USER\Control Panel\Desktop
The value is in milliseconds. If the hook procedure times out, the system passes the message to the next hook. However, on Windows 7 and later, the hook is silently removed without being called. There is no way for the application to know whether the hook is removed.
Windows 10 version 1709 and later The maximum timeout value the system allows is 1000 milliseconds (1 second). The system will default to using a 1000 millisecond timeout if the LowLevelHooksTimeout
value is set to a value larger than 1000.
Exceptions
- Win32Exception
The hook could not be installed.
TryInstall()
Attempts to install the keyboard hook.
public static bool TryInstall()
Returns
TryUninstall()
Attempts to uninstall the keyboard hook.
public static bool TryUninstall()
Returns
Uninstall()
Uninstalls the keyboard hook, and stops further keyboard events from being captured.
public static void Uninstall()
Exceptions
- Win32Exception
The hook could not be uninstalled.
Events
KeyDown
Occurs when a key is pressed.
public static event KeyEventHandler? KeyDown
Event Type
Remarks
Event handlers should aim to be as fast as possible, otherwise keyboard performance may be impacted, or the hook removed by the operating system. See Install() for more information.
KeyUp
Occurs a key is released.
public static event KeyEventHandler? KeyUp
Event Type
Remarks
Event handlers should aim to be as fast as possible, otherwise mouse performance may be impacted, or the hook removed by the operating system. See Install() for more information.