📜 ⬆️ ⬇️

Library for registering and catching hotkeys

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:
 /// <summary> ///       /// </summary> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); /// <summary> ///      /// </summary> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId); /// <summary> ///     /// </summary> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] private static extern int UnhookWindowsHookEx(int idHook); 


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:


Developments:


HookManager class (static class)


Properties:


Developments:


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.

Source: https://habr.com/ru/post/140979/


All Articles