Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mrousavy/hotkeys

:abc: A small C# (.NET) Library which allows binding of global HotKeys to any Application's Windows (including Windows Apps such as explorer.exe), even in Background. (using P/Invokes)
https://github.com/mrousavy/hotkeys

bindings dotnet global-hotkeys hotkeys key library windows

Last synced: 2 days ago
JSON representation

:abc: A small C# (.NET) Library which allows binding of global HotKeys to any Application's Windows (including Windows Apps such as explorer.exe), even in Background. (using P/Invokes)

Awesome Lists containing this project

README

        

# Hotkeys
A small C# (.NET) Library which allows binding of global HotKeys to any Application's Windows (including Windows Apps such as `explorer.exe`), even in Background. (using P/Invokes)

# Install

Install from [NuGet](https://www.nuget.org/packages/GlobalHotkeys)

```
Install-Package GlobalHotkeys
```
[![NuGet](https://img.shields.io/nuget/dt/GlobalHotkeys.svg)](https://www.nuget.org/packages/GlobalHotkeys/)

> _(or [download the Library (.dll)](https://raw.githubusercontent.com/mrousavy/Hotkeys/master/Downloads/Hotkeys.dll) manually)_

Buy Me a Coffee at ko-fi.com

# Usage

Example _(C#, in a WPF Application)_:
```cs
using mrousavy;
// ...
var key = new HotKey(
(ModifierKeys.Control | ModifierKeys.Alt),
Key.S,
this,
(hotkey) => {
MessageBox.Show("Ctrl + Alt + S was pressed!");
}
);
// ...
key.Dispose();
```

> Note #1: Since `HotKey` implements the [`IDisposable` interface](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable?view=netcore-3.1), you must call `Dispose()` to unregister the Hotkey, e.g. when your Window closes. Keep in mind that when you're using `using(...) { ... }`, the HotKey gets unregistered and disposed once you exit the using-statement's scope.

> Note #2: Use the binary **or** operator (`|`) to combine key combinations for the constructor.

> Note #3: Use a Window Reference as the third Argument. This may be a [WPF `Window`](https://docs.microsoft.com/en-us/dotnet/api/system.windows.window?view=netcore-3.1), a [`WindowInteropHelper`](https://docs.microsoft.com/en-us/dotnet/api/system.windows.interop.windowinterophelper?view=netcore-3.1), or a Window Handle ([`IntPtr`](https://stackoverflow.com/questions/1953582/how-to-i-get-the-window-handle-by-giving-the-process-name-that-is-running)). So you can use your own WPF/WinForms Window, as well as another process's Window using it's Handle. Keep in mind that this is sketchy behaviour and not the intention of this library.