//======================================================================================== // Methods //======================================================================================== private void DoOK(object sender, RoutedEventArgs e) { manager.IsEnabled = true; manager.Update(map); manager.Save(); map = manager.KeyMap; Close(); }
/// <summary> /// Preferred constructor, initializes a new instance using the given key manager. /// </summary> /// <param name="manager">The KeyManager to edit.</param> public HotKeyEditor(KeyManager manager) : this() { this.manager = manager; this.manager.IsEnabled = false; this.map = this.manager.KeyMap; this.trapper = new KeyTrapper(); this.trapper.KeyPressed += new HotKeyHandler(DoKeyPressed); this.editor.DataContext = this; this.editor.SelectedIndex = 0; }
/// <summary> /// Unregisters the existing hot keys and replaces them with the given /// hot key keymap. /// </summary> /// <param name="map">The map of new hot keys to register.</param> public void Update(HotKeyCollection map) { HotKeyCollection hotkeys = KeyMap; foreach (var hotKey in hotkeys) { var key = (HotKey)hotKey; UnregisterHotKey(key.Code, key.Modifier); } foreach (var hotKey in map) { var key = (HotKey)hotKey; RegisterHotKey(key.Code, key.Modifier, key.Action); } }
/// <summary> /// Loads preserved hot keys or set default hot keys. /// </summary> private void Load() { HotKeyCollection map = null; if (File.Exists(dataPath)) { try { using (XmlReader reader = XmlReader.Create(dataPath)) { DataContractSerializer serializer = new DataContractSerializer(typeof(HotKeyCollection)); map = (HotKeyCollection)serializer.ReadObject(reader); reader.Close(); } } catch { map = null; } } if (map == null) { map = new HotKeyCollection(); } KeyModifier mod = KeyModifier.Alt | KeyModifier.Win; MergeMap(map, HotKeyAction.PlayPause, Keys.Space, mod); MergeMap(map, HotKeyAction.NextTrack, Keys.Oemplus, mod); MergeMap(map, HotKeyAction.PrevTrack, Keys.OemMinus, mod); MergeMap(map, HotKeyAction.VolumeDown, Keys.Oemcomma, mod); MergeMap(map, HotKeyAction.VolumeUp, Keys.OemPeriod, mod); MergeMap(map, HotKeyAction.Mute, Keys.M, mod); MergeMap(map, HotKeyAction.ShowLyrics, Keys.L, mod); MergeMap(map, HotKeyAction.ShowiTunes, Keys.I, mod); MergeMap(map, HotKeyAction.ShowiTuner, Keys.T, mod); foreach (var hotKey in map) { var key = (HotKey)hotKey; RegisterHotKey(key.Code, key.Modifier, key.Action); } }
/// <summary> /// Ensure a HotKey exists in the given map for the specified action. /// </summary> /// <param name="map">The key map to scan.</param> /// <param name="action">The action to define.</param> /// <param name="keys">The primary key to define.</param> /// <param name="modifier">The secondary key modifiers to define.</param> private void MergeMap( HotKeyCollection map, HotKeyAction action, Keys keys, KeyModifier modifier) { if (!map.Contains(action)) { if (map.Contains(keys, modifier)) { // can't have two actions with same key sequence so we need to randomize // this sequence. This would only occur when a new version of iTuner // introduces a new action whose default sequence matches the user-defined // sequence of an existing action. keys = Keys.None; modifier = KeyModifier.None; } // Add will add the HotKey in the order prescribed by the HotKeyAction enum map.Add(new HotKey(action, keys, modifier)); } }
/// <summary> /// Save all currently registered hot key sequences and their associated actions /// to the user's local application data directory. /// </summary> public void Save() { var dirPath = Path.GetDirectoryName(dataPath); if (dirPath == null) { MessageWindow.Show($"Cannot find save path: {dataPath}"); return; } if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var map = new HotKeyCollection(keys.Values); var settings = new XmlWriterSettings { Indent = true }; try { using (XmlWriter writer = XmlWriter.Create(dataPath, settings)) { var serializer = new DataContractSerializer(typeof(HotKeyCollection)); serializer.WriteObject(writer, map); writer.Flush(); writer.Close(); } } catch (Exception exc) { MessageWindow.Show(string.Format(Resx.HotKeysNotSaved, exc.Message)); } }
private void DoCancel(object sender, RoutedEventArgs e) { manager.IsEnabled = true; map = manager.KeyMap; }