Suspending/Resuming Hotkeys

Suspending and resuming hotkeys at runtime

Suspending hotkeys simply refers to disabling or deactivating the hotkeys while resuming refers to enabling or reactivating the hotkeys for continued use.

These two methods are very applicable in scenarios where a user would prefer to change a specific hotkey while its currently active.

A classic example.

We'll let us imagine we have two hotkeys Control+Shift+E and Alt+X, but the user prefers to change Control+Shift+E to Alt+X and Alt+X to something else. In this case, we cannot simply change one hotkey to another if the hotkeys are currently active. So what do we do? We first of all need to suspend the currently active hotkeys to prevent them from being detected or listened to, change the respective hotkeys, then resume listening to the hotkeys having made the necessary changes.

Here's an example:

Hotkey hotkey1 = new Hotkey(Keys.Control | Keys.Shift, Keys.E);
Hotkey hotkey2 = new Hotkey(Keys.Alt, Keys.X);

// Suspend all registered hotkeys.
hkl.Suspend();

// Update hotkeys with newer keys.
hkl.Update(ref hotkey1, new Hotkey(Keys.Alt, Keys.X));
hkl.Update(ref hotkey2, new Hotkey(Keys.Shift, Keys.PrintScreen));

// Resume listening to the hotkeys.
hkl.Resume();

Hope that's much clearer now...

Now, let's find out more on how to work with this feature using one very important class that comes with Hotkey Listener - the HotkeySelector class.

Last updated