public static void Add(string name, User32.KeyModifiers keyModifier, Keys key, Control invokeHandleControl, HotKeyHandle hotKeyHandle) { HotKeyInfo hotKeyInfo = new HotKeyInfo(); hotKeyInfo.Name = name; hotKeyInfo.KeyModifiers = keyModifier; hotKeyInfo.Key = key; hotKeyInfo.InvokeHandleControl = invokeHandleControl; hotKeyInfo.HotKeyHandle = hotKeyHandle; Add(hotKeyInfo); }
/// <remarks> /// 如果名相同且和热键相同,则不会引发异常。 /// </remarks> public static void Change(HotKeyInfo hotKeyInfo) { var sameName = from val in hotKeys where val.Value.Name == hotKeyInfo.Name select val; if (sameName.Count() == 0) { throw new ArgumentException("The name doesn't exist.", "hotKeyInfo"); } else if (sameName.Count() > 1) { throw new Exception("A unknow error from HotKeyManager.Change."); } if (hotKeyInfo.InvokeHandleControl == null) { throw new ArgumentException("The invoke handle control can't be null.", "hotKeyInfo"); } if (hotKeyInfo.HotKeyHandle == null) { throw new ArgumentException("The hot key handle can't be null.", "hotKeyInfo"); } var sameHotKey = from val in hotKeys.Values where val.Key == hotKeyInfo.Key && val.KeyModifiers == hotKeyInfo.KeyModifiers select val; if (sameHotKey.Count() > 0) { throw new ArgumentException("The hot key has been existed.", "hotKeyInfo"); } if (hotKeyInfo.Key == Keys.None) { throw new ArgumentException("The key can't be none, if you want remove it, please call Change().", "hotKeyInfo"); } int newId; try { newId = Register(hotKeyInfo.KeyModifiers, hotKeyInfo.Key); } catch (Exception ex) { throw ex; } hotKeys.Remove(sameName.Single().Key); hotKeys.Add(newId, hotKeyInfo); }
private void button_SetSwitchHotKey_Click(object sender, EventArgs e) { var sameName = from val in HotKeyManager.HotKeyInfos where val.Name == HotKey_AbortClickThreadName select val; if (sameName.Count() > 0) { if (hotKey_SwitchClickThread == Keys.None) { HotKeyManager.Remove(HotKey_AbortClickThreadName); } else { try { HotKeyManager.Change(HotKey_AbortClickThreadName, hotKeyModifiers_SwitchClickThread, hotKey_SwitchClickThread, this, HotKey_SwitchClickThread); } catch (Exception) { HotKeyInfo last = sameName.Single(); textBox_SetSwitchHotKey.Text = GetHotKeyName(last.KeyModifiers, last.Key); ShowWarning("注册热键失败。"); return; } } } else { if (hotKey_SwitchClickThread != Keys.None) { try { HotKeyManager.Add(HotKey_AbortClickThreadName, hotKeyModifiers_SwitchClickThread, hotKey_SwitchClickThread, this, HotKey_SwitchClickThread); } catch (Exception) { CleanHotKeyInfo_SwitchClickThread(); ShowWarning("注册热键失败。"); return; } } } Change_textBox_SetSwitchHotKey_Text(GetHotKeyName(hotKeyModifiers_SwitchClickThread, hotKey_SwitchClickThread)); }