using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace NumpadMonitor { internal class KeyHandler { [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); [DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); public KeyInfo KeyInfo { get; private set; } private readonly IntPtr hWnd; private readonly int id; public void SetKeyCode(IntPtr keyCode) { KeyInfo.SetKeyCode(keyCode); } public KeyHandler(Keys key, Form form) { KeyInfo = new KeyInfo(key); hWnd = form.Handle; id = GetHashCode(); } public KeyHandler(KeyInfo keyInfo, Form form) { KeyInfo = keyInfo; hWnd = form.Handle; id = GetHashCode(); } public override int GetHashCode() { int hash = 17; hash = hash * 23 + KeyInfo.GetHashCode(); hash = hash * 23 + hWnd.ToInt32(); return hash; } public bool Register() { return RegisterHotKey(hWnd, id, KeyInfo.FsModifiers, (int)KeyInfo.Key); } public bool Unregiser() { return UnregisterHotKey(hWnd, id); } } }