Skip to content

Validate hotkey when setting it #1770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ public class HotkeyModel
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<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
private static readonly Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
{
{Key.Space, "Space"},
{Key.Oem3, "~"}
Expand Down Expand Up @@ -86,7 +97,7 @@ private void Parse(string hotkeyString)
Ctrl = true;
keys.Remove("Ctrl");
}
if (keys.Count > 0)
if (keys.Count == 1)
{
string charKey = keys[0];
KeyValuePair<Key, string>? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
Expand All @@ -110,36 +121,61 @@ private void Parse(string hotkeyString)

public override string ToString()
{
string text = string.Empty;
List<string> keys = new List<string>();
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();
: CharKey.ToString());
}
return string.Join(" + ", keys);
}

private static bool ValidateHotkey(Key key)
{
HashSet<Key> 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 if (!string.IsNullOrEmpty(text))
else
{
text = text.Remove(text.Length - 3);
return false;
}
}

return text;
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
}
7 changes: 2 additions & 5 deletions Flow.Launcher/HotkeyControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Plugin;
using System.Threading;
using System.Windows.Interop;

namespace Flow.Launcher
{
Expand Down Expand Up @@ -55,9 +54,7 @@ private void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
specialKeyState.CtrlPressed,
key);

var hotkeyString = hotkeyModel.ToString();

if (hotkeyString == tbHotkey.Text)
if (hotkeyModel.Equals(CurrentHotkey))
{
return;
}
Expand All @@ -79,7 +76,7 @@ public async Task SetHotkeyAsync(HotkeyModel keyModel, bool triggerValidate = tr

if (triggerValidate)
{
CurrentHotkeyAvailable = CheckHotkeyAvailability();
CurrentHotkeyAvailable = CurrentHotkey.CharKey != Key.None && CheckHotkeyAvailability();
if (!CurrentHotkeyAvailable)
{
tbMsg.Foreground = new SolidColorBrush(Colors.Red);
Expand Down
4 changes: 2 additions & 2 deletions Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
tbMsgTextOriginal = HotkeyControl.tbMsg.Text;
tbMsgForegroundColorOriginal = HotkeyControl.tbMsg.Foreground;

HotkeyControl.SetHotkeyAsync(new Infrastructure.Hotkey.HotkeyModel(Settings.Hotkey), false);
HotkeyControl.SetHotkeyAsync(Settings.Hotkey, false);
}
private void HotkeyControl_OnGotFocus(object sender, RoutedEventArgs args)
{
Expand All @@ -49,4 +49,4 @@ private void HotkeyControl_OnLostFocus(object sender, RoutedEventArgs args)
HotkeyControl.tbMsg.Foreground = tbMsgForegroundColorOriginal;
}
}
}
}