diff --git a/Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs b/Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs index 5bd97714c..e4a020cb7 100644 --- a/Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs +++ b/Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs @@ -11,10 +11,21 @@ namespace Flow.Launcher.Infrastructure.Hotkey public bool Shift { get; set; } public bool Win { get; set; } public bool Ctrl { get; set; } - public Key CharKey { get; set; } + private Key charKey = Key.None; + public Key CharKey + { + get => charKey; + set + { + if (ValidateHotkey(value)) + { + charKey = value; + } + } + } - Dictionary specialSymbolDictionary = new Dictionary + private static readonly Dictionary specialSymbolDictionary = new Dictionary { {Key.Space, "Space"}, {Key.Oem3, "~"} @@ -86,7 +97,7 @@ namespace Flow.Launcher.Infrastructure.Hotkey Ctrl = true; keys.Remove("Ctrl"); } - if (keys.Count > 0) + if (keys.Count == 1) { string charKey = keys[0]; KeyValuePair? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey); @@ -110,36 +121,61 @@ namespace Flow.Launcher.Infrastructure.Hotkey public override string ToString() { - string text = string.Empty; + List keys = new List(); if (Ctrl) { - text += "Ctrl + "; + keys.Add("Ctrl"); } if (Alt) { - text += "Alt + "; + keys.Add("Alt"); } if (Shift) { - text += "Shift + "; + keys.Add("Shift"); } if (Win) { - text += "Win + "; + keys.Add("Win"); } if (CharKey != Key.None) { - text += specialSymbolDictionary.ContainsKey(CharKey) + keys.Add(specialSymbolDictionary.ContainsKey(CharKey) ? specialSymbolDictionary[CharKey] - : CharKey.ToString(); - } - else if (!string.IsNullOrEmpty(text)) - { - text = text.Remove(text.Length - 3); + : CharKey.ToString()); } + return string.Join(" + ", keys); + } - return text; + private static bool ValidateHotkey(Key key) + { + HashSet invalidKeys = new() + { + Key.LeftAlt, Key.RightAlt, + Key.LeftCtrl, Key.RightCtrl, + Key.LeftShift, Key.RightShift, + Key.LWin, Key.RWin, + }; + + return !invalidKeys.Contains(key); + } + + public override bool Equals(object obj) + { + if (obj is HotkeyModel other) + { + return ModifierKeys == other.ModifierKeys && CharKey == other.charKey; + } + else + { + return false; + } + } + + public override int GetHashCode() + { + return this.ToString().GetHashCode(); } } }