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; }
private void SetKey(Keys key) { KeyInfo keyInfo = new KeyInfo(key); cbControl.Checked = keyInfo.Control; cbShift.Checked = keyInfo.Shift; cbAlt.Checked = keyInfo.Alt; Keys vk = keyInfo.KeyCode; for (int i = 0; i < keys.Count; i++) { if (keys[i].Key == vk) { cbKeys.SelectedIndex = i; return; } } cbKeys.SelectedIndex = 0; }