-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1defb6a
Refactor HotkeyModel
VictoriousRaptor 10fa2f7
Validate hotkey
VictoriousRaptor ff79651
Use Equals() for comparison
VictoriousRaptor cd056f6
Use overloaded version
VictoriousRaptor 0c58c2b
Ban single character key
VictoriousRaptor f6e2fe3
Reset message when got focus
VictoriousRaptor a399d24
Extract set message logic
VictoriousRaptor 72f8b9e
Refactor HotkeyModel validation logic
VictoriousRaptor c03a0b0
Validate hotkey before setting
VictoriousRaptor 3f6ab55
Use properties for hashcode
VictoriousRaptor 28c5031
Maintain focus when hotkey invalid
VictoriousRaptor 835370a
Set text to current hotkey when lost focus
VictoriousRaptor 203705b
Use compound assignment
VictoriousRaptor 3a8162b
Ban alphanumeric keys
VictoriousRaptor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Windows.Input; | ||
using System.Windows.Navigation; | ||
|
||
namespace Flow.Launcher.Infrastructure.Hotkey | ||
{ | ||
|
@@ -11,10 +12,10 @@ public class HotkeyModel | |
public bool Shift { get; set; } | ||
public bool Win { get; set; } | ||
public bool Ctrl { get; set; } | ||
public Key CharKey { get; set; } | ||
|
||
public Key CharKey { get; set; } = Key.None; | ||
|
||
Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string> | ||
private static readonly Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string> | ||
{ | ||
{Key.Space, "Space"}, | ||
{Key.Oem3, "~"} | ||
|
@@ -27,19 +28,19 @@ public ModifierKeys ModifierKeys | |
ModifierKeys modifierKeys = ModifierKeys.None; | ||
if (Alt) | ||
{ | ||
modifierKeys = ModifierKeys.Alt; | ||
modifierKeys |= ModifierKeys.Alt; | ||
} | ||
if (Shift) | ||
{ | ||
modifierKeys = modifierKeys | ModifierKeys.Shift; | ||
modifierKeys |= ModifierKeys.Shift; | ||
} | ||
if (Win) | ||
{ | ||
modifierKeys = modifierKeys | ModifierKeys.Windows; | ||
modifierKeys |= ModifierKeys.Windows; | ||
} | ||
if (Ctrl) | ||
{ | ||
modifierKeys = modifierKeys | ModifierKeys.Control; | ||
modifierKeys |= ModifierKeys.Control; | ||
} | ||
return modifierKeys; | ||
} | ||
|
@@ -86,7 +87,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); | ||
|
@@ -110,36 +111,75 @@ 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()); | ||
} | ||
else if (!string.IsNullOrEmpty(text)) | ||
return string.Join(" + ", keys); | ||
} | ||
|
||
public bool Validate() | ||
{ | ||
switch (CharKey) | ||
{ | ||
case Key.LeftAlt: | ||
case Key.RightAlt: | ||
case Key.LeftCtrl: | ||
case Key.RightCtrl: | ||
case Key.LeftShift: | ||
case Key.RightShift: | ||
case Key.LWin: | ||
case Key.RWin: | ||
return false; | ||
default: | ||
if (ModifierKeys == ModifierKeys.None) | ||
{ | ||
return !((CharKey >= Key.A && CharKey <= Key.Z) || | ||
(CharKey >= Key.D0 && CharKey <= Key.D9) || | ||
(CharKey >= Key.NumPad0 && CharKey <= Key.NumPad9)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking for all symbol keys are too difficult so I just ban alphanumeric keys. |
||
} | ||
else | ||
{ | ||
return CharKey != Key.None; | ||
} | ||
} | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is HotkeyModel other) | ||
{ | ||
text = text.Remove(text.Length - 3); | ||
return ModifierKeys == other.ModifierKeys && CharKey == other.CharKey; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return text; | ||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(ModifierKeys, CharKey); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.