SharpHotHook 0.5.0

dotnet add package SharpHotHook --version 0.5.0                
NuGet\Install-Package SharpHotHook -Version 0.5.0                
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="SharpHotHook" Version="0.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpHotHook --version 0.5.0                
#r "nuget: SharpHotHook, 0.5.0"                
#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 SharpHotHook as a Cake Addin
#addin nuget:?package=SharpHotHook&version=0.5.0

// Install SharpHotHook as a Cake Tool
#tool nuget:?package=SharpHotHook&version=0.5.0                

SharpHotHook

A simple library for handling global hotkey combinations. It utilizes the SharpHook library, which allows global key detection. This library simplifies the process by combining key detection and triggering events.


How to Use

The following classes are used for operation:

  • HotkeyManager: The main class that holds a list of hotkeys and activates them.
  • KeyReaderManager: A standalone class that simply reads the current key.
  • IHotkey: An interface defining the structure of a hotkey. Its main features include a list of keys and a method that gets called upon activation.

Basic Hotkey Usage

You can use the predefined Hotkey class from SharpHotHook.Defaults, which implements IHotkey. The HotkeyManager class is used to manage and trigger hotkeys.

static void Example1()
{
    var manager = new HotkeyManager();

    // Initializing a hotkey via properties
    manager.Add(new Hotkey()
    {
        KeyCodes = [KeyCode.Vc1],
        OnHotkey = () => Console.WriteLine("\n\tKey vc1 pressed")
    });

    // Initializing a hotkey via constructor
    manager.Add(new Hotkey(
        keyCodes: [KeyCode.Vc1, KeyCode.VcOpenBracket],
        action: () => Console.WriteLine("\n\tKey 1 + [ pressed")
    ));

    // Use Stop to stop the manager, Start to run it
    manager.Add(new Hotkey()
    {
        KeyCodes = [KeyCode.VcEscape],
        OnHotkey = manager.Stop
    });

    manager.Start();
}

static void Main(string[] args)
{
    Example1();
    Console.WriteLine("\nWaiting for keys");

    // Due to the inner workings of SharpHook's reader, the program will not terminate and will keep listening for key presses.
}

Console output (symbols entered on the left, output displayed on the right):

Waiting for keys
1
Key vc1 pressed
Key vc1 pressed
1
Key vc1 pressed
13
Key vc1 pressed
1ххх
Key vc1 pressed
1
Key 1 + [ pressed
х
Key vc1 pressed
1
Key 1 + [ pressed
[
хх
Key 1 + [ pressed
1х
Key 1 + [ pressed
1[[Key 1 + [ pressed
1
Key vc1 pressed
1
Key 1 + [ pressed
[

Key-Only Reading

This feature can be useful if you only want to monitor which keys are being pressed. The KeyReaderManager class is designed for this purpose. Here’s an example:

class KeyReader : KeyReaderManager
{
    private KeyCode _key;
    public Action OnKeyChanged { get; set; } = Console.WriteLine;

    public override KeyCode CurrentKey
    {
        get => _key;
        set
        {
            _key = value;
            if (_key == KeyCode.VcEscape)
            {
                Stop();
                return;
            }

            Console.Write("Key is changed on: ");
            OnKeyChanged();
        }
    }
}

static void Example2()
{
    var manager = new KeyReader();
    manager.OnKeyChanged = () => Console.WriteLine(manager.CurrentKey);
    manager.Start();
}

Creating Custom Classes

The library is flexible. You can inherit from KeyReaderBase and its inherits, or implement IKeyReader, IHotkeyContainer, and IHotkey. The HotkeyMethods extension methods are available for working with IHotkey.

When implementing IHotkey, be sure to handle the IsActivated boolean array. Its size must match the size of the key list.


License

Note: The use of the library code, the library itself, and the texts of the README are subject to the NIN license (available in the project's repository). Acceptance of the license is mandatory.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
0.5.0 85 12/3/2024
0.3.2 108 9/25/2024
0.3.1 95 9/24/2024
0.3.0 99 9/24/2024
0.2.0 113 9/9/2024
0.1.0 118 9/9/2024