Skip to content

Latest commit

 

History

168 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Windows User Action Hook (EventHook)

CI NuGet

Code Quality

Quality Gate Coverage Lines of Code Bugs Vulnerabilities Code Smells Security Rating Reliability Rating Maintainability Rating Duplicated Lines Technical Debt

A .NET library to subscribe to Windows global user actions: keyboard, mouse, clipboard, application windows, print jobs, and hotkeys.

Requires: .NET 10 on Windows (net10.0-windows). AnyCPU — works on x86, x64, and ARM64 Windows when P/Invoke pointer sizes are correct (v2 fixes these).

Install

dotnet add package EventHook

Sample

using System;
using System.Windows.Forms;

using (var eventHookFactory = new EventHookFactory())
{
    var keyboardWatcher = eventHookFactory.GetKeyboardWatcher();
    keyboardWatcher.Start();
    keyboardWatcher.OnKeyInput += (s, e) =>
        Console.WriteLine($"Key {e.KeyData.EventType} of {e.KeyData.Keyname}");

    var mouseWatcher = eventHookFactory.GetMouseWatcher();
    mouseWatcher.IncludeMouseMove = false; // #28
    mouseWatcher.Start();
    mouseWatcher.OnMouseInput += (s, e) =>
        Console.WriteLine($"Mouse {e.Message} at {e.Point.x},{e.Point.y}");

    var clipboardWatcher = eventHookFactory.GetClipboardWatcher();
    clipboardWatcher.Start();
    clipboardWatcher.OnClipboardModified += (s, e) =>
        Console.WriteLine($"Clipboard {e.DataFormat}: {e.Data}");

    var applicationWatcher = eventHookFactory.GetApplicationWatcher();
    applicationWatcher.Start();
    applicationWatcher.OnApplicationWindowChange += (s, e) =>
        Console.WriteLine($"{e.ApplicationData.AppName} was {e.Event}");

    var printWatcher = eventHookFactory.GetPrintWatcher();
    printWatcher.Start();
    printWatcher.OnPrintEvent += (s, e) =>
        Console.WriteLine($"Printer {e.EventData.PrinterName} pages={e.EventData.Pages}");

    var hotkeyWatcher = eventHookFactory.GetHotkeyWatcher();
    hotkeyWatcher.Start();
    hotkeyWatcher.Register("demo", Keys.Control | Keys.Alt | Keys.H);
    hotkeyWatcher.OnHotkeyPressed += (s, e) =>
        Console.WriteLine($"Hotkey {e.Id} ({e.Keys})");

    Console.ReadLine();
}

Application window filter

EventHook.Helpers.AppWindowFilter.IncludeWindowsWithoutSysMenu = true; // games without WS_SYSMENU
EventHook.Helpers.AppWindowFilter.IncludeDialogs = true;               // MessageBox / #32770
EventHook.Helpers.AppWindowFilter.CustomFilter = hwnd => true;         // optional

Hosted apps (COM / Office add-ins)

Prefer constructing the factory on an STA UI thread, or pass an existing message-pump HWND:

using var factory = new EventHookFactory(hostMainWindowHandle);

VB.NET

See examples/EventHook.VB.Example. Context-menu paste is observed via the clipboard watcher (global); WM_PASTE itself is application-local.

Print to PDF

PrintWatcher enumerates local and connected queues, including virtual printers such as Microsoft Print to PDF. Print a document to that queue to verify OnPrintEvent.

Development

  • Visual Studio 2022 / .NET 10 SDK
  • dotnet build src/EventHook.sln -c Release
  • dotnet test tests/EventHook.Tests
  • dotnet test tests/EventHook.IntegrationTests
  • Docs: dotnet tool restore then docfx .github/docfx.json (writes API HTML under docs/, same as titanium-web-proxy)

Release branches

Branch NuGet Notes
develop (no publish) CI build + tests + DocFX
beta {VersionPrefix}-beta from EventHook.csproj Merge developbeta to publish prerelease
stable / tag v* {VersionPrefix} from csproj Stable release + GitHub Pages docs

Publishing uses NuGet Trusted Publishing (NUGET_USER on the nuget-publish environment), same pattern as titanium-web-proxy.

Version 2.0 notes

Breaking: targets net10.0-windows only (no longer .NET Framework 4.5).

Highlights: HotkeyWatcher, mouse-move filter, clipboard images/files, dialog/MsgBox tracking, PDF/virtual printers, x64/ARM64 P/Invoke fixes, reliable Stop/Dispose, GitHub Actions CI + DocFX.

About

A .NET library to subscribe for Windows operating system global user actions such mouse, keyboard, clipboard & print events

Topics

Resources

Security policy

Stars

289 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages