https://github.com/bracozs/globalhotkeysrx
Simple C# Library for creating global hotkeys.
https://github.com/bracozs/globalhotkeysrx
csharp desktop global hotkeys net8 windowless windows wpf
Last synced: 12 months ago
JSON representation
Simple C# Library for creating global hotkeys.
- Host: GitHub
- URL: https://github.com/bracozs/globalhotkeysrx
- Owner: BracoZS
- License: mit
- Created: 2024-07-27T03:34:52.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-05T18:58:20.000Z (over 1 year ago)
- Last Synced: 2025-04-07T04:28:43.158Z (about 1 year ago)
- Topics: csharp, desktop, global, hotkeys, net8, windowless, windows, wpf
- Language: C#
- Homepage: https://www.nuget.org/packages/GlobalHotkeysRX
- Size: 97.7 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GlobalHotKeysRX
Simple C# library for creating system-wide hotkeys. It allows you to implement keyboard shortcuts to perform specific actions in your application, regardless of the active window.
You can use it in background/windowless applications.
It supports combinations of:
-Modifier keys: Alt, Control, Shift, Win (up to 3 at a time).
-Keys, listed at [Virtual-Key Codes](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes).
## [Nuget Package](https://www.nuget.org/packages/GlobalHotkeysRX) 
## Usage
Import library
```
using GlobalHotKeysRX;
```
Initialization
```
// default constructor
GlobalHotkeys appHotkeys = new GlobalHotkeys();
// or you can call it with one or more 'Hotkey' parameters.
GlobalHotkeys appHotkeys = new GlobalHotkeys(
new Hotkey(Modifier.Shift, VKey.P, myFunction),
new Hotkey(Modifier.Alt, VKey.S, () => {/* code */}) // lambda expression
new Hotkey(Modifier.Control, VKey.Y)
);
```
> *Use the `|` operator to combine/add modifiers. For example, combine `Control` + `Alt` to get `AtlGr`
>
> `new Hotkey(Modifier.Control | Modifier.Alt, VKey.P, myFunction) // AltGr + P`
>
> *By using 'Modifiers.NoRepeat' with another modifier, the application will only receive another WM_HOTKEY message when the key is released and then pressed again while a modifier is held down.
>
> `new Hotkey(Modifier.Shift | Modifier.NoRepeat, VKey.P, myFunction)`
>
>
>### * If you are in a **Windows Form** proyect
>you need to specify the handle, either in constructor...
>
>`GlobalHotkeys appHotkeys = new GlobalHotkeys(this.Handle);`
>
>...or through the ClientHandle property
>
>`appHotkeys.ClientHandle = this.Handle;`
>
>Also you must add this to the form
>
>```
> protected override void WndProc(ref Message m)
>{
> if(m.Msg == WinApi.WM_HOTKEY)
> appHotkeys.OnWinFormMessage(m); // Replace by your own instance name
>
> // (Optional) Stops any default or additional processing of the message.
> m.Result = (IntPtr)1;
>
> base.WndProc(ref m);
>}
>```
Adding a hotkey after initialization
```
appHotkeys.Add(Modifier.Win, VKey.NUM_0);
// or
var htk = new Hotkey(Modifier.Win, VKey.NUM_0, myFuntion);
appHotkeys.Add(htk);
void myFunction()
{
// code when a hotkey occurs...
}
```
Removing
```
appHotkeys.Remove(Modifier.Shift, VKey.Z);
// or
appHotkeys.Remove(htk);
```
Subscribe to hotkeys
```
appHotkeys.HotkeyPressed += AppHotkeys_HotkeyPressed;
// ...
public void AppHotkeys_HotkeyPressed(object sender, HotkeyEventArgs e)
{
MessageBox.Show($"Hotkey {e.Modifier} + {e.Key} was pressed!");
}
```
Cleaning up resources
```
appHotkeys.Dispose();
```
Simplest way 🙂 (wpf)
```
GlobalHotkeys ghk = new GlobalHotkeys(new Hotkey(Modifier.Shift, VKey.V, () => { /*code*/ }));
```
### Known issues
This library uses Win32 API. Typically, [RegisterHotKey](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey) fails if the keystrokes specified for the hot key have already been registered for another hot key. However, some pre-existing, default hotkeys registered by the OS (such as PrintScreen, which launches the Snipping tool) may be overridden by another hot key registration when one of the app's windows is in the foreground.
[](https://www.patreon.com/Braco_ZS)
[](https://ko-fi.com/A0A4G6LKI)