InputInterceptor 2.2.1

dotnet add package InputInterceptor --version 2.2.1                
NuGet\Install-Package InputInterceptor -Version 2.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InputInterceptor" Version="2.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InputInterceptor --version 2.2.1                
#r "nuget: InputInterceptor, 2.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InputInterceptor as a Cake Addin
#addin nuget:?package=InputInterceptor&version=2.2.1

// Install InputInterceptor as a Cake Tool
#tool nuget:?package=InputInterceptor&version=2.2.1                

InputInterceptor

Library for keyboard and mouse input interception and simulation. Based on interception driver. Library will work only if the driver is intalled, however library contains installer and the driver can be installed by InputInterceptor.InstallDriver() call (InputInterceptor.UninstallDriver() for uninstall). Available as NuGet package.

Compatible with .NET Standard 1.3 and higher!

Usage

You are able to use static InputInterceptor class to interact with the driver in the same way as shown in the driver examples.

Library also contains Hook derived classes — KeyboardHook and MouseHook, which will do most of the work for you. They will intercept input and have methods to simulate input. Note, that input simulation might not work right if device wasn't captured (random keyboard/mouse device will be used). Even if hook doesn't recieve callback it will capture device with first input by this device.

MouseHook

// Constructors
MouseHook(MouseFilter filter = MouseFilter.All, CallbackAction callback = null);
MouseHook(CallbackAction callback);
// Base methods
Boolean SetMouseState(MouseState state, Int16 rolling = 0);
Win32Point GetCursorPosition();
Boolean SetCursorPosition(Win32Point point, Boolean useWinAPI = false);
Boolean SetCursorPosition(Int32 x, Int32 y, Boolean useWinAPI = false);
Boolean MoveCursorBy(Int32 dX, Int32 dY, Boolean useWinAPI = false);
// Simulation methods
Boolean SimulateLeftButtonDown();
Boolean SimulateLeftButtonUp();
Boolean SimulateLeftButtonClick(Int32 releaseDelay = 50);
Boolean SimulateMiddleButtonDown();
Boolean SimulateMiddleButtonUp();
Boolean SimulateMiddleButtonClick(Int32 releaseDelay = 50);
Boolean SimulateRightButtonDown();
Boolean SimulateRightButtonUp();
Boolean SimulateRightButtonClick(Int32 releaseDelay = 50);
Boolean SimulateScrollDown(Int16 rolling = 120);
Boolean SimulateScrollUp(Int16 rolling = 120);
Boolean SimulateMoveTo(Win32Point point, Int32 speed = 15, Boolean useWinAPI = false);
Boolean SimulateMoveTo(Int32 x, Int32 y, Int32 speed = 15, Boolean useWinAPI = false);
Boolean SimulateMoveBy(Int32 dX, Int32 dY, Int32 speed = 15, Boolean useWinAPI = false);

KeyboardHook

// Constructors
KeyboardHook(KeyboardFilter filter = KeyboardFilter.All, CallbackAction callback = null);
KeyboardHook(CallbackAction callback);
// Base methods
Boolean SetKeyState(KeyCode code, KeyState state);
// Simulation methods
Boolean SimulateKeyDown(KeyCode code);
Boolean SimulateKeyUp(KeyCode code);
Boolean SimulateKeyPress(KeyCode code, Int32 releaseDelay = 75);
Boolean SimulateInput(String text, Int32 delayBetweenKeyPresses = 50, Int32 releaseDelay = 75);

* SimulateInput method works with ANSI compatible string with english letters only (special chars are supported)

Hook

// Base properties
Context Context { get; private set; }
Device Device { get; private set; }
Device RandomDevice { get; private set; }
Filter FilterMode { get; private set; }
Predicate Predicate { get; private set; }
CallbackAction Callback { get; private set; }
Exception Exception { get; private set; }
Boolean Active { get; private set; }
Thread Thread { get; private set; }
// Usefull getter properties
Boolean IsInitialized => this.Context != Context.Zero && this.Device != -1;
Boolean CanSimulateInput => this.Context != Context.Zero && (this.RandomDevice != -1 || this.Device != -1);
Boolean HasException => this.Exception != null;

Example Application

using InputInterceptorNS;

if (InitializeDriver()) {
    MouseHook mouseHook = new MouseHook(MouseCallback);
    KeyboardHook keyboardHook = new KeyboardHook(KeyboardCallback);
    Console.WriteLine("Hooks enabled. Press any key to release.");
    Console.ReadKey();
    keyboardHook.Dispose();
    mouseHook.Dispose();
} else {
    InstallDriver();
}

Console.WriteLine("End of program. Press any key.");
Console.ReadKey();

void MouseCallback(ref MouseStroke mouseStroke) {
    Console.WriteLine($"{mouseStroke.X} {mouseStroke.Y} {mouseStroke.Flags} {mouseStroke.State} {mouseStroke.Information}"); // Mouse XY coordinates are raw
    // Invert mouse X
    //mouseStroke.X = -mouseStroke.X;
    // Invert mouse Y
    //mouseStroke.Y = -mouseStroke.Y;
}

void KeyboardCallback(ref KeyStroke keyStroke) {
    Console.WriteLine($"{keyStroke.Code} {keyStroke.State} {keyStroke.Information}");
    // Button swap
    //keyStroke.Code = keyStroke.Code switch {
    //    KeyCode.A => KeyCode.B,
    //    KeyCode.B => KeyCode.A,
    //    _ => keyStroke.Code,
    //};
}

Boolean InitializeDriver() {
    if (InputInterceptor.CheckDriverInstalled()) {
        Console.WriteLine("Input interceptor seems to be installed.");
        if (InputInterceptor.Initialize()) {
            Console.WriteLine("Input interceptor successfully initialized.");
            return true;
        }
    }
    Console.WriteLine("Input interceptor initialization failed.");
    return false;
}

void InstallDriver() {
    Console.WriteLine("Input interceptor not installed.");
    if (InputInterceptor.CheckAdministratorRights()) {
        Console.WriteLine("Installing...");
        if (InputInterceptor.InstallDriver()) {
            Console.WriteLine("Done! Restart your computer.");
        } else {
            Console.WriteLine("Something... gone... wrong... :(");
        }
    } else {
        Console.WriteLine("Restart program with administrator rights so it will be installed.");
    }
}

Warning

You may lose keyboard and mouse control if your program freezes (due to an exception in main thread or something like that) when intercepting input. The hook classes are wrapped with try catch, however, for example, this will not save you from blocking the thread.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net46 was computed.  net461 was computed.  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 tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.1 589 3/24/2024
2.2.0 2,277 5/20/2022
2.1.0 476 3/30/2022
2.0.2 435 10/20/2021
2.0.1 308 10/17/2021
2.0.0 304 10/17/2021
1.0.7 719 10/8/2019
1.0.3 494 10/7/2019
1.0.2 500 10/6/2019
1.0.1 476 10/6/2019