Skip to content

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

License

Notifications You must be signed in to change notification settings

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.2.0" />

Example

Add namespace

using GlobalKeyInterceptor;

Intercept everything

var interceptor = new KeyInterceptor();

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

Intercept only specified keystrokes/shortcuts

Shortcut[] shortcuts =
[
    // You can specify a key, up to 4 modifiers, key state and a name
    new Shortcut(Key.R, state: KeyState.Down, name: "R (key is down)"),
    new Shortcut(Key.Alt, KeyModifier.Ctrl, name: "Modifier + Modifier as a simple key"),
    new Shortcut(Key.D, KeyModifier.Ctrl | KeyModifier.Shift | KeyModifier.Alt | KeyModifier.Win, 
        name: "Every modifier + D"),
];

var interceptor = new KeyInterceptor(shortcuts);

interceptor.ShortcutPressed += (_, e) =>
{
    // Specify a name of the shortcut to easy check if it pressed
    switch (e.Shortcut.Name)
    {
        case "R (key is down)":
            // Set e.IsHandled to true if you want to "eat" the pressed key
            e.IsHandled = true;

            // some logic
            break;
        case "Modifier + Modifier as a simple key":
            // some logic 2
            break;
        case "Every modifier + D":
            // some logic 3
            break;
    }
};

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