![]() |
VOOZH | about |
dotnet add package SharpHook --version 7.1.2
NuGet\Install-Package SharpHook -Version 7.1.2
<PackageReference Include="SharpHook" Version="7.1.2" />
<PackageVersion Include="SharpHook" Version="7.1.2" />Directory.Packages.props
<PackageReference Include="SharpHook" />Project file
paket add SharpHook --version 7.1.2
#r "nuget: SharpHook, 7.1.2"
#:package SharpHook@7.1.2
#addin nuget:?package=SharpHook&version=7.1.2Install as a Cake Addin
#tool nuget:?package=SharpHook&version=7.1.2Install as a Cake Tool
SharpHook provides a cross-platform global keyboard and mouse hook, event simulation, and text entry simulation for .NET. It is a wrapper of libuiohook and provides direct access to its features as well as higher-level types to work with it.
SharpHook exposes the functions of libuiohook in the SharpHook.Native.UioHook class. The SharpHook.Data namespace
contains types which represent the data used by libuiohook.
In general, you don't need to use the native methods directly. Instead, use the higher-level interfaces and classes provided by SharpHook. However, you should still read this section to know how the high-level features work under the hood.
If you want to use the low-level functionality, you don't need to use the UioHook class directly. Instead you can use
interfaces in the SharpHook.Providers namespace. The methods in those interfaces are the same as in the UioHook
class. SharpHook.Providers.UioHookProvider implements all of these interfaces and simply calls the corresponding
methods in UioHook. This should be done to decouple your code from UioHook and make testing easier.
UioHook contains the following methods for working with the global hook:
SetDispatchProc – sets the function which will be called when an event is raised by libuiohook.Run – creates a keyboard and mouse global hook and runs it on the current thread, blocking it until Stop is
called.RunKeyboard – creates a keyboard-only global hook and runs it on the current thread, blocking it until Stop is
called.RunMouse – creates a mouse-only global hook and runs it on the current thread, blocking it until Stop is called.Stop – stops the global hook.Important: You have to remember that only one global hook can exist at a time since calling
SetDispatchProcwill override the previously set one.
Additionally, UioHook contains the PostEvent method for simulating input events.
UioHook also contains the PostText method which simulates text entry. The text to simulate doesn't depend on the
current keyboard layout. The full range of UTF-16 (including surrogate pairs, e.g. emojis) is supported.
libuiohook also provides functions to get various system properties. The corresponding methods are also present in
UioHook.
SharpHook provides the IGlobalHook interface along with three default implementations which you can use to control the
hook and subscribe to its events. Here's a basic usage example:
using SharpHook;
using SharpHook.Providers;
// KeyTyped events may cause system-wide side effects, so they should be disabled if unused.
UioHookProvider.Instance.KeyTypedEnabled = false; // or true
var hook = new EventLoopGlobalHook();
hook.HookEnabled += OnHookEnabled; // EventHandler<HookEventArgs>
hook.HookDisabled += OnHookDisabled; // EventHandler<HookEventArgs>
hook.KeyTyped += OnKeyTyped; // EventHandler<KeyboardHookEventArgs>
hook.KeyPressed += OnKeyPressed; // EventHandler<KeyboardHookEventArgs>
hook.KeyReleased += OnKeyReleased; // EventHandler<KeyboardHookEventArgs>
hook.MouseClicked += OnMouseClicked; // EventHandler<MouseHookEventArgs>
hook.MousePressed += OnMousePressed; // EventHandler<MouseHookEventArgs>
hook.MouseReleased += OnMouseReleased; // EventHandler<MouseHookEventArgs>
hook.MouseMoved += OnMouseMoved; // EventHandler<MouseHookEventArgs>
hook.MouseDragged += OnMouseDragged; // EventHandler<MouseHookEventArgs>
hook.MouseWheel += OnMouseWheel; // EventHandler<MouseWheelHookEventArgs>
hook.Run();
// or
await hook.RunAsync();
First, you create the hook, then subscribe to its events, and then run it. The Run method runs the hook on the current
thread, blocking it. The RunAsync() method runs the hook on a separate thread and returns a Task which is finished
when the hook is stopped. You can subscribe to events after the hook is started.
IGlobalHook contains the Stop method to stop the global hook. After stopping, the global hook can be started again
by calling the Run or RunAsync method. Calling Stop when the hook is not running won't do anything.
IGlobalHook extends IDisposable. When you call the Dispose method on a hook, it's disposed and stopped if it was
running. Once a hook has been disposed, it cannot be started again – you'll have to create a new instance. Calling
Dispose when the hook is not running won't do anything other than marking the instance as disposed.
Hook events are of type HookEventArgs or a derived type which contains more info. It's possible to suppress event
propagation by setting the SuppressEvent property to true inside the event handler. This must be done synchronously
and is only supported on Windows and macOS. You can check the event time and whether the event is real or simulated with
the EventTime and IsEventSimulated properties respectively.
Important: Always use one instance of
IGlobalHookat a time in the entire application since they all must use the same static method to set the hook callback for libuiohook, so there may only be one callback at a time. Running a global hook when another global hook is already running will corrupt the internal global state of libuiohook.
You can create a keyboard-only or a mouse-only hook by passing a GlobalHookType to the hook's constructor. This makes
a real difference only on Windows where there are two different global hooks – a keyboard hook and a mouse hook. On
macOS and Linux, there is one hook for all events, and this simply enables filtering keyboard or mouse events out on
these OSes.
SharpHook provides three implementations of IGlobalHook:
SharpHook.SimpleGlobalHook runs all of its event handlers on the same thread on which the hook itself runs. This
means that the handlers should generally be fast since they will block the hook from handling the events that follow if
they run for too long.
SharpHook.EventLoopGlobalHook runs all of its event handlers on a separate dedicated thread. On backpressure it will
queue the remaining events which means that the hook will be able to process all events. This implementation should be
preferred to SimpleGlobalHook except for very simple use-cases. But it has a downside – suppressing event propagation
will be ignored since event handlers are run on another thread.
SharpHook.TaskPoolGlobalHook runs all of its event handlers on other threads inside the default thread pool for
tasks. The parallelism level of the handlers can be configured. On backpressure it will queue the remaining events which
means that the hook will be able to process all events. This implementation should be preferred to SimpleGlobalHook
except for very simple use-cases. But it has a downside – suppressing event propagation will be ignored since event
handlers are run on other threads. In general, EventLoopGlobalHook should be preferred instead, as this class provides
benefits only if events should be processed in parallel, which is rarely the case.
The library also provides the SharpHook.GlobalHookBase class which you can extend to create your own implementation
of the global hook. It calls the appropriate event handlers, and you only need to implement a strategy for dispatching
the events. It also keeps a reference to a running global hook so that it's not garbage-collected.
Use the SharpHook.Reactive package for reactive global hooks with Rx.NET integration.
Use the SharpHook.R3 package for reactive global hooks with R3 integration.
SharpHook provides the ability to simulate keyboard and mouse events in a cross-platform way as well. Here's a quick example:
using SharpHook;
using SharpHook.Data;
// ...
var simulator = new EventSimulator();
// Press Ctrl+C
simulator.SimulateKeyPress(KeyCode.VcLeftControl);
simulator.SimulateKeyPress(KeyCode.VcC);
// Release Ctrl+C
simulator.SimulateKeyRelease(KeyCode.VcC);
simulator.SimulateKeyRelease(KeyCode.VcLeftControl);
// Press the left mouse button
simulator.SimulateMousePress(MouseButton.Button1);
// Release the left mouse button
simulator.SimulateMouseRelease(MouseButton.Button1);
// Press the left mouse button at (0, 0)
simulator.SimulateMousePress(0, 0, MouseButton.Button1);
// Release the left mouse button at (0, 0)
simulator.SimulateMouseRelease(0, 0, MouseButton.Button1);
// Move the mouse pointer to (0, 0)
simulator.SimulateMouseMovement(0, 0);
// Move the mouse pointer 50 pixels to the right and 100 pixels down
simulator.SimulateMouseMovementRelative(50, 100);
// Scroll the mouse wheel
simulator.SimulateMouseWheel(
rotation: -120,
direction: MouseWheelScrollDirection.Vertical, // Vertical by default
type: MouseWheelScrollType.UnitScroll); // UnitScroll by default
SharpHook provides the IEventSimulator interface, and the default implementation, EventSimulator, which by default
calls UioHook.PostEvent to simulate the events.
SharpHook also provides text entry simulation. IEventSimulator contains the SimulateTextEntry method which accepts
a string. The text to simulate doesn't depend on the current keyboard layout. The full range of UTF-16 (including
surrogate pairs, e.g., emojis) is supported.
libuiohook can log messages throughout its execution. By default the messages are not logged anywhere, but you can get
these logs by using the ILogSource interface and its default implementation, LogSource:
using SharpHook.Logging;
// ...
var logSource = LogSource.RegisterOrGet(minLevel: LogLevel.Info);
logSource.MessageLogged += this.OnMessageLogged;
private void OnMessageLogged(object? sender, LogEventArgs e) =>
this.logger.Log(this.AdaptLogLevel(e.LogEntry.Level), e.LogEntry.FullText);
ILogSource extends IDisposable – you can dispose of a log source to stop receiving libuiohook messages.
An EmptyLogSource class is also available – this class doesn't listen to the libuiohook logs and can be used instead
of LogSource in release builds.
SharpHook provides two classes which make testing easier. They aren't required since mocks can be used instead, but unlike mocks, no setup is required to use these classes.
SharpHook.Testing.TestGlobalHook provides an implementation of IGlobalHook and IEventSimulator which can be used
for testing. When the Run or RunAsync method is called, it will dispatch events using the various Simulate methods
from IEventSimulator.
If this class is used as an IEventSimulator in the tested code, then the SimulatedEvents property can be checked to
see which events were simulated using the test instance.
If the low-level functionality of SharpHook should be mocked, or mocking should be pushed as far away as possible,
then SharpHook.Testing.TestProvider can be used. It implements every interface in the SharpHook.Providers namespace
and as such it can be used instead of a normal low-level functionality provider.
Like TestGlobalHook, this class can post events using the PostEvent method and dispatch them if Run was called.
It also contains the PostedEvents property.
Icon made by Freepik from www.flaticon.com.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-maccatalyst18.0 net9.0-maccatalyst18.0 is compatible. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-maccatalyst26.0 net10.0-maccatalyst26.0 is compatible. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 is compatible. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on SharpHook:
| Package | Downloads |
|---|---|
|
GetIt
.NET library to help introduce programming in a funnier way. Inspired by Scratch and Turtle graphics. |
|
|
SharpHook.Reactive
SharpHook provides a cross-platform global keyboard and mouse hook, event simulation, and text entry simulation for .NET. |
|
|
VL.IO.MouseKeyGlobal
Global mouse and keyboard hook for vvvv gamma based on Sharphook by Tolik Pylypchuk |
|
|
CrossSharp.Application
Cross-platform UI c# library supporting Linux, Windows and MacOs |
|
|
Agibuild.Fulora.Avalonia
Cross-platform WebView control for Avalonia UI with native platform adapters (WKWebView on macOS/iOS, WebView2 on Windows, WebKitGTK on Linux, Android WebView). Provides embedded WebView, popup dialog, and OAuth authentication flows. |
Showing the top 19 popular GitHub repositories that depend on SharpHook:
| Repository | Stars |
|---|---|
|
timschneeb/GalaxyBudsClient
Unofficial Galaxy Buds Manager for Windows, macOS, Linux, and Android
|
|
|
Jeric-X/SyncClipboard
跨平台剪贴板同步、历史记录管理工具 / Cross-platform cipboard syncing, history management tool
|
|
|
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
|
|
|
CXWorld/CapFrameX
Frametime capture and analysis tool
|
|
|
flyingpie/windows-terminal-quake
Enable Quake-style dropdown for (almost) any application.
|
|
|
Sidekick-Poe/Sidekick
The main repository for the Sidekick project, a companion trade tool for Path of Exile and Path of Exile 2.
|
|
|
verybadcat/CSharpMath
LaTeX. in C#. (ported from the wonderful iosMath project).
|
|
|
awaescher/StageManager
🖥️ Stage Manager for Microsoft Windows (feasibility study)
|
|
|
MaaXYZ/MFAAvalonia
基于 Avalonia 的 MAAFramework 通用 GUI 项目 | A universal GUI project for MAAFramework based on Avalonia
|
|
|
ShareX/XerahS
XerahS is a cross platform reimagining of the ShareX user interface built with Avalonia. It targets modern UX modular architecture and long term maintainability while preserving core workflows speed and reliability. It provides a foundation for future desktop experiences on Windows, macOS, and Linux with consistent accessible design.
|
|
|
lulzsun/RePlays
Open-source game recording management software
|
|
|
Particle1904/DatasetHelpers
Dataset Helper program to automatically select, re scale and tag Datasets (composed of image and text) for Machine Learning training.
|
|
|
tghamm/Anthropic.SDK
An unofficial C#/.NET SDK for accessing the Anthropic Claude API. This package is not affiliated with, endorsed by, or sponsored by Anthropic. Anthropic and Claude are trademarks of Anthropic, PBC.
|
|
|
PizzaLovers007/AdofaiTweaks
Helpful tweaks for A Dance of Fire and Ice
|
|
|
AvionBlock/VoiceCraft
VoiceCraft software to implement proximity VC for Minecraft Bedrock Edition
|
|
|
jooapa/jammer
light-weight CLI music player with Soundcloud, Youtube, Rss, Midi Support for Win & Linux
|
|
|
dan0v/AmplitudeSoundboard
A sleek, cross-platform soundboard, available for Windows, MacOS, and Linux
|
|
|
AlizerUncaged/waifu-desktop
🎂 your waifu, right on your desktop!
|
|
|
deanthecoder/ZXSpeculator
Cross-platform ZX Spectrum emulator written in C#
|
| Version | Downloads | Last Updated |
|---|---|---|
| 7.1.2 | 2,128 | 5/25/2026 |
| 7.1.1 | 33,826 | 12/30/2025 |
| 7.1.0 | 19,284 | 11/16/2025 |
| 7.0.3 | 7,072 | 10/5/2025 |
| 7.0.2 | 3,036 | 9/21/2025 |
| 7.0.1 | 10,669 | 8/17/2025 |
| 7.0.0 | 1,670 | 8/10/2025 |
| 6.2.0 | 2,172 | 7/19/2025 |
| 6.1.2 | 3,287 | 6/22/2025 |
| 6.1.1 | 1,809 | 6/3/2025 |
| 6.1.0 | 1,060 | 5/23/2025 |
| 6.0.0 | 1,300 | 5/18/2025 |
| 5.3.9 | 21,354 | 3/29/2025 |
| 5.3.8 | 30,633 | 9/27/2024 |
| 5.3.7 | 16,906 | 7/9/2024 |
| 5.3.6 | 4,049 | 5/22/2024 |
| 5.3.5 | 6,781 | 5/20/2024 |
| 5.3.4 | 1,290 | 5/5/2024 |
| 5.3.3 | 1,013 | 4/28/2024 |
| 5.3.2 | 4,969 | 4/2/2024 |