HotkeyModel.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Windows.Input; 9 10 namespace Wox.Infrastructure.Hotkey 11 { 12 public class HotkeyModel 13 { 14 public bool Alt { get; set; } 15 16 public bool Shift { get; set; } 17 18 public bool Win { get; set; } 19 20 public bool Ctrl { get; set; } 21 22 public Key CharKey { get; set; } 23 24 private readonly Dictionary<Key, string> _specialSymbolDictionary = new Dictionary<Key, string> 25 { 26 { Key.Space, "Space" }, 27 { Key.Oem3, "~" }, 28 }; 29 30 public ModifierKeys ModifierKeys 31 { 32 get 33 { 34 ModifierKeys modifierKeys = ModifierKeys.None; 35 36 if (Alt) 37 { 38 modifierKeys = ModifierKeys.Alt; 39 } 40 41 if (Shift) 42 { 43 modifierKeys |= ModifierKeys.Shift; 44 } 45 46 if (Win) 47 { 48 modifierKeys |= ModifierKeys.Windows; 49 } 50 51 if (Ctrl) 52 { 53 modifierKeys |= ModifierKeys.Control; 54 } 55 56 return modifierKeys; 57 } 58 } 59 60 public HotkeyModel() 61 { 62 } 63 64 public HotkeyModel(string hotkeyString) 65 { 66 Parse(hotkeyString); 67 } 68 69 public HotkeyModel(bool alt, bool shift, bool win, bool ctrl, Key key) 70 { 71 Alt = alt; 72 Shift = shift; 73 Win = win; 74 Ctrl = ctrl; 75 CharKey = key; 76 } 77 78 private void Parse(string hotkeyString) 79 { 80 if (string.IsNullOrEmpty(hotkeyString)) 81 { 82 return; 83 } 84 85 // Using InvariantCulture since this is internal and involves key codes 86 List<string> keys = hotkeyString.Replace(" ", string.Empty, StringComparison.InvariantCulture).Split('+').ToList(); 87 if (keys.Contains("Alt")) 88 { 89 Alt = true; 90 keys.Remove("Alt"); 91 } 92 93 if (keys.Contains("Shift")) 94 { 95 Shift = true; 96 keys.Remove("Shift"); 97 } 98 99 if (keys.Contains("Win")) 100 { 101 Win = true; 102 keys.Remove("Win"); 103 } 104 105 if (keys.Contains("Ctrl")) 106 { 107 Ctrl = true; 108 keys.Remove("Ctrl"); 109 } 110 111 if (keys.Count > 0) 112 { 113 string charKey = keys[0]; 114 KeyValuePair<Key, string>? specialSymbolPair = _specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey); 115 if (specialSymbolPair.Value.Value != null) 116 { 117 CharKey = specialSymbolPair.Value.Key; 118 } 119 else 120 { 121 try 122 { 123 CharKey = (Key)Enum.Parse(typeof(Key), charKey); 124 } 125 catch (ArgumentException) 126 { 127 } 128 } 129 } 130 } 131 132 public override string ToString() 133 { 134 string text = string.Empty; 135 if (Ctrl) 136 { 137 text += "Ctrl + "; 138 } 139 140 if (Alt) 141 { 142 text += "Alt + "; 143 } 144 145 if (Shift) 146 { 147 text += "Shift + "; 148 } 149 150 if (Win) 151 { 152 text += "Win + "; 153 } 154 155 if (CharKey != Key.None) 156 { 157 text += _specialSymbolDictionary.TryGetValue(CharKey, out string value) ? value : CharKey.ToString(); 158 } 159 else if (!string.IsNullOrEmpty(text)) 160 { 161 text = text.Remove(text.Length - 3); 162 } 163 164 return text; 165 } 166 } 167 }