Advanced Usage
This page goes more in depth on some of the features and inner workings of the HotkeyUtility package.
Binding
One of the more convenient features of HotkeyUtility is that its objects and controls support binding to its properties.
The Combination Property
Both HotkeyBinding and VisualHotkey contain a dependency property called Combination, which is a KeyBinding. This means that if you bind a KeyBinding to the Combination property, the hotkey(s) in your application can be changed at runtime.
Info
The Combination property is really just a KeyBinding in disguise. This provides a significant advantage over the Gesture property of a KeyBinding because the Gesture property doesn't allow certain combinations like Shift+M or Shift+1.
Using HotkeyUtility Programmatically
So far, you've really only seen HotkeyUtility used in XAML but what if you're more of a codebehind kind of person? Well, don't worry because you can simply use the HotkeyManager class to add, remove, or replace hotkeys.
The HotkeyManager class exposes the following methods that you can use:
GetHotkeyManagerTryAddHotkeyTryRemoveHotkeyTryReplaceHotkeyGetHotkeys
Getting Started
In order to get an instance of the HotkeyManager class, you need to use its GetHotkeyManager method. The HotkeyManager class uses the singleton design pattern which means that as long as you access your HotkeyManager from the same thread that created it in the first place, you'll always have access to your hotkeys.
Here's a simple example of how we can go about creating an instance of HotkeyManager for the first time in our application:
ShellViewModel.cs | |
|---|---|
Now, let's say that we've added some hotkeys to our HotkeyManager (which we'll get to soon in another section) and we want to interact with them in another file. Well, we can do exactly that by calling the same method:
OtherViewModel.cs | |
|---|---|
Adding Hotkeys
If you want to programmatically add a hotkey to your application, you'll need to:
- Create a
Hotkeyobject - Use the
TryAddHotkeymethod onHotkeyManagerto add it
The Hotkey class has the following signature for its constructor:
You'll need to pass it a Key, a ModifierKeys, and an event handler that will be subscribed to the Pressed event that will trigger when the hotkey is pressed.
Once you've created your Hotkey, pass it to the TryAddHotkey method of an instance of the HotkeyManager class:
The TryAddHotkey method returns a bool indicating whether or not the operation was successful.
Removing Hotkeys
If you want to remove a hotkey from your application, you can do so by passing the same Hotkey object to the TryRemoveHotkey method of HotkeyManager:
Now, Ctrl+Shift+A will be unregistered from your application.
The TryRemoveHotkey method returns a bool indicating whether or not the operation was successful.
Replacing Hotkeys
The TryReplaceHotkey method works by unregistering an existing Hotkey and reregistering it with a new Key, a new ModifierKeys, or both and returns a boolean indicating whether the operation was successful. This method consists of two overloaded methods. Here are both of their signatures:
First signature | |
|---|---|
Second signature | |
|---|---|
First Overloaded Method
For the first signature, all you need are the Key and ModifierKeys of an existing Hotkey object for the first two parameters. These will be used to iterate through the current hotkeys until it finds a matching Key and ModifierKeys. The newKey and newModifiers parameters will then be used to replace the binding of the existing Hotkey.
Second Overloaded Method
As for the second signature, the first parameter must be filled by the Id of an existing Hotkey. If the Id is a match, the newKey and newModifiers parameters will be used to replace the binding of the existing Hotkey.
Viewing Your Hotkeys
You can iterate over all of your current hotkeys by calling the GetHotkeys method. This will return a ValueCollection of Hotkey objects that you can then iterate over:
Additionally, HotkeyUtility provides an extension method called Find that you can use alongside the GetHotkeys method to quickly and compactly find a single Hotkey in the ValueCollection. Pass the method a Key and ModifierKeys to find and match the existing Hotkey. If a match was found, the expected Hotkey will be returned; otherwise, null will be returned.