SharpHook 1.0.0-preview.1
See the version list below for details.
dotnet add package SharpHook --version 1.0.0-preview.1
NuGet\Install-Package SharpHook -Version 1.0.0-preview.1
<PackageReference Include="SharpHook" Version="1.0.0-preview.1" />
paket add SharpHook --version 1.0.0-preview.1
#r "nuget: SharpHook, 1.0.0-preview.1"
// Install SharpHook as a Cake Addin #addin nuget:?package=SharpHook&version=1.0.0-preview.1&prerelease // Install SharpHook as a Cake Tool #tool nuget:?package=SharpHook&version=1.0.0-preview.1&prerelease
SharpHook
SharpHook is a library which provides a cross-platform global keyboard and mouse hook for .NET. It is a thin wrapper of libuiohook and provides direct access to its features as well as a higher-level interface and classes to work with it.
Supported Platforms
<table> <tr> <th></th> <th>Windows</th> <th>macOS</th> <th>Linux</th> </tr> <tr> <th>x86</th> <td>Yes</td> <td>N/A</td> <td>No<sup>1</sup></td> </tr> <tr> <th>x64</th> <td>Yes</td> <td>Yes</td> <td>Yes</td> </tr> <tr> <th>Arm32</th> <td>Yes<sup>2</sup></td> <td>N/A</td> <td>Yes</td> </tr> <tr> <th>Arm64</th> <td>No<sup>3</sup></td> <td>Yes</td> <td>Yes</td> </tr> </table>
[1] - Linux on x86 is not supported by .NET itself.
[2] - Windows Arm32 support was dropped in .NET 5 so it will most probably be dropped by this library in a future version as well.
[3] - Windows on Arm64 is not yet supported by libuiohook.
libuiohook only supports X11 on Linux. Wayland support may be coming, but it's not yet here.
Usage
Native Methods of libuiohook
SharpHook exposes the methods of libuiohook in the SharpHook.Native.UioHook
class. The SharpHook.Native
namespace also contains structs and enums which represent the data returned by libuiohook.
Note: In general, you shouldn't use native methods directly. Instead, use the higher-level interface and classes provided by SharpHook.
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 global hook and runs it on the current thread, blocking it untilStop
is called.Stop
- destroys the global hook.
libuiohook also provides functions to get various system properties. The corresponding methods in UioHook
are listed
below:
CreateScreenInfo
- gets the information about the screens and returns it as an array. There's also a version of this function which returns an unmanaged array and that version shouldn't be used directly.GetAutoRepeatRate
GetAutoRepeatDelay
GetPointerAccelerationMultiplier
GetPointerAccelerationThreshold
GetPointerSensitivity
GetMultiClickTime
Default Global Hooks
SharpHook provides the IGlobalHook
interface along with two default implementations which you can use to control the
hook and subscribe to its events. Here's a basic usage example:
using SharpHook;
// ...
var hook = new TaskPoolGlobalHook();
hook.HookEnabled += OnHookEnabled;
hook.HookDisabled += OnHookDisabled;
hook.KeyTyped += OnKeyTyped;
hook.KeyPressed += OnKeyPressed;
hook.KeyReleased += OnKeyReleased;
hook.MouseClicked += OnMouseClicked;
hook.MousePressed += OnMousePressed;
hook.MouseReleased += OnMouseReleased;
hook.MouseMoved += OnMouseMoved;
hook.MouseDragged += OnMouseDragged;
hook.MouseWheel += OnMouseWheel;
await hook.Start();
First, you create the hook, then subscribe to its events, and then start it. The Start()
method returns a Task
which is finished when the hook is destroyed, so if you await
it, you block the current async context until it stops.
You can subscribe to events after the hook is started.
IGlobalHook
extends IDisposable
. When you call the Dispose
method on a hook, it's destroyed. The contract of
the interface is that once a hook has been destroyed, it cannot be started again - you'll have to create a new instance.
Important: Always use one instance of IGlobalHook
at a time in the entire application since they all must use
the same static method to set the hook callback for libuiohook, and there may only be one callback at a time.
SharpHook provides two implementations of IGlobalHook
:
SimpleGlobalHook
runs the hook on a separate thread, and runs all of its event handlers on that same thread. 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.TaskPoolGlobalHook
runs the hook on a separate thread, and 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 handlers. This means that the hook will be able to process all events. This implementation should be preferred toSimpleGlobalHook
except for very simple use-cases.
The library also provides the GlobalHookBase
class which you can extend to create your own implementation of the
global hook. It runs the hook on a separate thread and calls appropriate event handlers. You only need to implement a
strategy for dispatching the events.
Reactive Global Hooks
Use the SharpHook.Reactive
package for reactive global hooks.
Limitations
You have to remember that libuiohook binaries should be present in the curent working directory. This is how P/Invoke works, and it can cause the 'library not found' issues when running your client app from a different directory.
Another thing is that libuiohook supports hooking into its logging capabilities so that you can get its logs. This
library doesn't support this. The reason is that you should call hook_set_logger_proc
and pass your callback for
logging. This is similar to hook_set_dispatch_proc
, but this time the callback should accept a variable number of
arguments (using C's ...
syntax) and the client decides how to format the log message. Supporting native
variable arguments in callbacks is next to impossible in C#, and the payoff is not worth spending a lot of effort to
implement this feature.
Icon
Icon made by Freepik from www.flaticon.com.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.6.1
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (6)
Showing the top 5 NuGet packages that depend on SharpHook:
Package | Downloads |
---|---|
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 |
|
Frank.GameEngine.Input
Package Description |
|
SharpHotHook
Read global hotkeys on C#. // Зчитування глобальних гарячих клавіш |
|
Frank.GameEngine.Rendering.MonoGame
Package Description |
GitHub repositories (7)
Showing the top 5 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 Solution
|
|
Sidekick-Poe/Sidekick
The main repository for the Sidekick project, a companion trade tool for Path of Exile and Path of Exile 2.
|
|
awaescher/StageManager
🖥️ Stage Manager for Microsoft Windows (feasibility study)
|
|
lulzsun/RePlays
Open-source game recording management software
|
Version | Downloads | Last updated |
---|---|---|
5.3.8 | 8,449 | 9/27/2024 |
5.3.7 | 8,544 | 7/9/2024 |
5.3.6 | 3,192 | 5/22/2024 |
5.3.5 | 3,848 | 5/20/2024 |
5.3.4 | 944 | 5/5/2024 |
5.3.3 | 733 | 4/28/2024 |
5.3.2 | 3,710 | 4/2/2024 |
5.3.1 | 15,446 | 3/3/2024 |
5.3.0 | 1,676 | 2/24/2024 |
5.2.3 | 5,358 | 2/1/2024 |
5.2.2 | 1,290 | 1/13/2024 |
5.2.1 | 1,510 | 1/3/2024 |
5.2.0 | 1,246 | 12/16/2023 |
5.1.2 | 1,903 | 11/25/2023 |
5.1.1 | 6,477 | 10/13/2023 |
5.1.0 | 575 | 10/8/2023 |
5.0.0 | 6,531 | 8/10/2023 |
4.2.1 | 3,036 | 6/18/2023 |
4.2.0 | 4,578 | 4/8/2023 |
4.1.0 | 618 | 3/26/2023 |
4.0.1 | 598 | 3/12/2023 |
4.0.0 | 4,521 | 11/9/2022 |
3.1.3 | 1,208 | 10/27/2022 |
3.1.2 | 1,406 | 10/19/2022 |
3.1.1 | 3,134 | 8/5/2022 |
3.1.0 | 1,166 | 7/30/2022 |
3.0.2 | 7,564 | 7/1/2022 |
3.0.1 | 734 | 6/25/2022 |
3.0.0 | 1,129 | 3/27/2022 |
2.0.0 | 1,164 | 2/4/2022 |
1.1.0 | 1,048 | 12/4/2021 |
1.0.1 | 807 | 11/21/2021 |
1.0.0 | 631 | 11/8/2021 |
1.0.0-preview.4 | 162 | 11/6/2021 |
1.0.0-preview.3 | 180 | 11/1/2021 |
1.0.0-preview.2 | 159 | 10/26/2021 |
1.0.0-preview.1 | 161 | 10/26/2021 |