A combination of keys means any number of keys pressed simultaneously, pressed in any order that your keyboard can allow. For the end user, however, do not exceed the number of more than five in one combination, because Not everyone has gaming keyboards.
Usage example
HotKeysManager manager = new HotKeysManager(); manager.AddHotKey(new HotKeyCombination(() => { MessageBox.Show(", !"); }) { Keys.LControlKey, Keys.H });
Another option to add, where the current keystrokes are taken as a combination, is convenient in the case when the user assigns a combination himself. In the demo there is an example of a similar recording of combinations.
manager.AddHotKey(new HotKeyCombination(HookManager.CurrentDownedKeys.ToArray(), () => { MessageBox.Show(", !"); }));
Now when you press the LeftCtrl + H (or H + LeftControl) combination, we will see a welcome message.
As done
')
Three WinAPI functions are used to intercept clicks globally:
When an event is received, it is checked whether the key was pressed or released, and a list of currently pressed keys is formed, after which a check of the combinations is called, and if there is a match an alert is sent.
Interception of clicks automatically turns on / off depending on the presence of combinations.
Functional
Class HotKeysManager
Methods:
- EnableHotKeys / DisableHotKeys - manual enable disabling key combinations checking
- AddHotKey (string, HotKeyCombination) - add a key combination to the collection, with the key indicated
- AddHotKey (HotKeyCombination) - adding a key combination with automatic key generation (the key will be generated as Key1 + Key2 + ... + Keyn)
- RemoveHotKey (string) - delete a combination with the key in the collection
- RemoveHotKey (HotKeyCombination) - removal of a combination, the key will be generated automatically (Key1 + Key2 + ... + Keyn), if there is no combination with such a key, an exception will not be thrown
Developments:
- NewCombination - notifies of the appearance of a new combination, a delegate of the form (string) {} is used, where Key1 + Key2 + ... + Keyn is passed as a parameter
HookManager class (static class)
Properties:
- CurrentDownedKeys - list of keystrokes (List <Keys>)
- CurrentDownedKeysDescription - a string with a description of the keys pressed (Key1 + Key2 + ... + Keyn)
Developments:
- ChangeKeyState - notifies about changing the set of keystrokes, the delegate without parameters
Download (source + bin_x86)When using, it is worth remembering that perekhat is global, and if you want to keep the action only with the active window of your program, you need to enter an additional check.