public HotkeyStatus RegisterHotkey(Keys hotkey, Action hotkeyPress = null, string tag = "") { KeyInfo keyInfo = new KeyInfo(hotkey); if (keyInfo.KeyCode == Keys.None) { return HotkeyStatus.NotConfigured; } if (IsHotkeyExist(hotkey)) { HotkeyInfo hki = HotkeyList.FirstOrDefault(x => x.Tag == tag); if (hki != null) { hki.HotkeyPress = hotkeyPress; return HotkeyStatus.Registered; } else { log.Debug("Hotkey already exist: " + keyInfo); return HotkeyStatus.Failed; } } string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + (int)hotkey; ushort id = NativeMethods.GlobalAddAtom(atomName); if (id == 0) { DebugHelper.WriteLine("Unable to generate unique hotkey ID. Error code: " + Marshal.GetLastWin32Error()); return HotkeyStatus.Failed; } if (!NativeMethods.RegisterHotKey(Handle, (int)id, (uint)keyInfo.ModifiersEnum, (uint)keyInfo.KeyCode)) { NativeMethods.GlobalDeleteAtom(id); DebugHelper.WriteLine("Unable to register hotkey: {0}\r\nError code: {1}", keyInfo, Marshal.GetLastWin32Error()); return HotkeyStatus.Failed; } HotkeyInfo hotkeyInfo = new HotkeyInfo(id, hotkey, hotkeyPress, tag); HotkeyList.Add(hotkeyInfo); return HotkeyStatus.Registered; }
public bool UnregisterHotkey(HotkeyInfo hotkeyInfo) { if (hotkeyInfo != null) { bool result = UnregisterHotkey(hotkeyInfo.ID); HotkeyList.Remove(hotkeyInfo); return result; } return false; }