Skip to content

arcanexhoax/GlobalKeyInterceptor

Repository files navigation

GlobalKeyInterceptor

NET Version Downloads License

It's a simple to use library that allows you to intercept keystrokes or shortcuts in the system.

Features

  • Intercept specified keys and shortcuts or intercept every keystroke
  • Possibility to "eat" pressed key
  • Works on any Windows application platforms (WPF/WinForms/Console)

Installation

Add the following string to the .csproj file:

<PackageReference Include="GlobalKeyInterceptor" Version="1.3.0" />

Example

Add namespace

using GlobalKeyInterceptor;

Intercept everything

var interceptor = new KeyInterceptor();

interceptor.ShortcutPressed += (_, e) =>
{
    Console.WriteLine(e.Shortcut);
};

Intercept only specified keystrokes/shortcuts

s_interceptor.RegisterShortcut(new Shortcut(Key.R, state: KeyState.Down), () => Console.WriteLine("R is down"));
s_interceptor.RegisterShortcut(new Shortcut(Key.Alt, KeyModifier.Ctrl), () => Console.WriteLine("Modifier + Modifier as a simple key"));
s_interceptor.RegisterShortcut(new Shortcut(Key.D, KeyModifier.Ctrl | KeyModifier.Alt | KeyModifier.Shift | KeyModifier.Win), () =>
{
    Console.WriteLine("Every modifier + D");

    // Return true if you want to "eat" the pressed key
    return true;
});

Console application

To allow the console application to intercept keystrokes, you need to add a message loop at the end of the Main() method:

interceptor.RunMessageLoop();

Full example you can find here

About

A simple to use library that allows you to intercept keystrokes or shortcuts in the system.

Topics

Resources

License

Stars

Watchers

Forks

Languages