/// <summary> /// Deletes a Hotkey /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonDelete_Click(object sender, EventArgs e) { if (listBoxHotkeys.SelectedItem != null) { GlobalHotkey ghk = (GlobalHotkey)listBoxHotkeys.SelectedItem; ghk.Unregister(); hotkeys.Remove(ghk); } UpdateHotkeyList(); }
/// <summary> /// Loads all Hotkeys /// </summary> public void LoadHotkeys() { Properties.Settings.Default.Reload(); string[] str = Properties.Settings.Default.Hotkeys.Split('|'); foreach (var item in str) { GlobalHotkey ghk = GlobalHotkey.Deserialize(item, this.Handle); if (ghk != null) { hotkeys.Add(ghk); } } }
/// <summary> /// Deserialize a String to a GlobalHotkey /// </summary> /// <param name="serialized"></param> /// <param name="handle"></param> /// <returns></returns> public static GlobalHotkey Deserialize(string serialized, IntPtr handle) { string[] str = serialized.Split(':'); if (str.Length == 7) { Keys key = (Keys)Enum.Parse(typeof(Keys), str[4]); GlobalHotkey hk = new GlobalHotkey(key, handle, GetHotkeyFunctionInstanceByName(str[5])); hk.Ctrl = str[0] == "True" ? true : false; hk.Shift = str[1] == "True" ? true : false; hk.Alt = str[2] == "True" ? true : false; hk.Win = str[3] == "True" ? true : false; hk.HotkeyFunctionName = str[6]; return(hk); } return(null); }
/// <summary> /// Checks if the new Hotkey already exists /// </summary> /// <param name="ghk"></param> /// <returns></returns> private bool CheckIfHotkeyExists(GlobalHotkey ghk) { if (ghk != null) { if (hotkeys.Count > 0) { // Check if hotkey exists foreach (var item in hotkeys) { if (item != null) { if (item.Modifier == ghk.Modifier && item.Key == ghk.Key) { MessageBox.Show("Hotkey already exists!", "Adding Hotkey", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); return(true); } } } } return(false); } return(false); }
/// <summary> /// Adds Hotkey to Hotkeylist /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonAdd_Click(object sender, EventArgs e) { if (textBoxKey.Tag != null && textBoxKey.Tag.GetType() == typeof(Keys) && comboBoxHotkeyFunctions.SelectedItem != null) { if (typeof(IHotkeyFunction).IsAssignableFrom(comboBoxHotkeyFunctions.SelectedItem.GetType())) { // create new Hotkey GlobalHotkey ghk = new GlobalHotkey((Keys)textBoxKey.Tag, this.Handle, GlobalHotkey.GetHotkeyFunctionInstanceByName(comboBoxHotkeyFunctions.SelectedItem.GetType().Name)) { Ctrl = checkBoxCtrl.Checked, Shift = checkBoxShift.Checked, Alt = checkBoxAlt.Checked, Win = checkBoxWin.Checked, }; if (!CheckIfHotkeyExists(ghk)) { // Add and register Hotkey hotkeys.Add(ghk); UpdateHotkeyList(); SaveHotkeys(); ghk.Register(); } } // Reset controls checkBoxCtrl.Checked = false; checkBoxShift.Checked = false; checkBoxAlt.Checked = false; checkBoxWin.Checked = false; textBoxKey.Clear(); textBoxKey.Tag = null; } }
/// <summary> /// Deserialize a String to a GlobalHotkey /// </summary> /// <param name="serialized"></param> /// <param name="handle"></param> /// <returns></returns> public static GlobalHotkey Deserialize(string serialized, IntPtr handle) { string[] str = serialized.Split(':'); if (str.Length == 7) { Keys key = (Keys)Enum.Parse(typeof(Keys), str[4]); GlobalHotkey hk = new GlobalHotkey(key, handle, GetHotkeyFunctionInstanceByName(str[5])); hk.Ctrl = str[0] == "True" ? true : false; hk.Shift = str[1] == "True" ? true : false; hk.Alt = str[2] == "True" ? true : false; hk.Win = str[3] == "True" ? true : false; hk.HotkeyFunctionName = str[6]; return hk; } return null; }
/// <summary> /// Checks if the new Hotkey already exists /// </summary> /// <param name="ghk"></param> /// <returns></returns> private bool CheckIfHotkeyExists(GlobalHotkey ghk) { if (ghk != null) { if (hotkeys.Count > 0) { // Check if hotkey exists foreach (var item in hotkeys) { if (item != null) { if (item.Modifier == ghk.Modifier && item.Key == ghk.Key) { MessageBox.Show("Hotkey already exists!", "Adding Hotkey", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); return true; } } } } return false; } return false; }