private void RefreshHotkeys() { // Unregister all hotkeys. foreach (var hotkey in _hotkeys) { hotkey.Dispose(); } _hotkeys.Clear(); // Remove any hotkeys for profiles that no longer exist (but keep hotkeys for system commands). if (SettingsFiles.ApplicationSettings.Hotkeys.RemoveAll(x => !SystemCommands.IsSystemCommand(x.Id) && !_profiles.ContainsKey(x.Id)) != 0) { SettingsFiles.SaveApplicationSettings(); } // Register hotkeys. foreach (var hotkeySetting in SettingsFiles.ApplicationSettings.Hotkeys) { var id = hotkeySetting.Id; try { _hotkeys.Add(new WinFormsHotkey(hotkeySetting.Hotkey, true, () => ExecuteSystemCommandOrLoadProfile(hotkeySetting.Id))); } catch (Exception ex) { BalloonTip("Could not bind hotkey " + WinFormsHotkey.HotkeyString(hotkeySetting.Hotkey) + " to " + SystemCommands.GetTitle(id), ex.Message, ToolTipIcon.Warning); } } }
public SetHotkeyDialog() { InitializeComponent(); hotkeyTextBox.KeyDown += WinFormsHotkey.CreateKeyDownHandler(x => { keys = x; EnableDisableButtons(); }); hotkeyTextBox.KeyUp += (_, __) => hotkeyTextBox.Text = WinFormsHotkey.HotkeyString(keys); }
/// <summary> /// Executes the dialog for a specific profile name and existing hotkey. Returns <c>null</c> if the user cancelled, <c>Keys.None</c> to remove a hotkey; otherwise, returns the new hotkey. /// </summary> /// <param name="profileName">The name of the profile.</param> /// <param name="existingHotkey">The existing hotkey for that profile.</param> public static Keys?ExecuteDialog(string profileName, Keys existingHotkey) { using (var dialog = new SetHotkeyDialog()) { dialog.Text += profileName; dialog.originalKeys = dialog.keys = existingHotkey; dialog.hotkeyTextBox.Text = WinFormsHotkey.HotkeyString(existingHotkey); dialog.EnableDisableButtons(); var result = dialog.ShowDialog(); if (result != DialogResult.OK) { return(null); } return(dialog.keys); } }