@@ -11,10 +11,21 @@ public class HotkeyModel
11
11
public bool Shift { get ; set ; }
12
12
public bool Win { get ; set ; }
13
13
public bool Ctrl { get ; set ; }
14
- public Key CharKey { get ; set ; }
15
14
15
+ private Key charKey = Key . None ;
16
+ public Key CharKey
17
+ {
18
+ get => charKey ;
19
+ set
20
+ {
21
+ if ( ValidateHotkey ( value ) )
22
+ {
23
+ charKey = value ;
24
+ }
25
+ }
26
+ }
16
27
17
- Dictionary < Key , string > specialSymbolDictionary = new Dictionary < Key , string >
28
+ private static readonly Dictionary < Key , string > specialSymbolDictionary = new Dictionary < Key , string >
18
29
{
19
30
{ Key . Space , "Space" } ,
20
31
{ Key . Oem3 , "~" }
@@ -86,7 +97,7 @@ private void Parse(string hotkeyString)
86
97
Ctrl = true ;
87
98
keys . Remove ( "Ctrl" ) ;
88
99
}
89
- if ( keys . Count > 0 )
100
+ if ( keys . Count == 1 )
90
101
{
91
102
string charKey = keys [ 0 ] ;
92
103
KeyValuePair < Key , string > ? specialSymbolPair = specialSymbolDictionary . FirstOrDefault ( pair => pair . Value == charKey ) ;
@@ -110,36 +121,61 @@ private void Parse(string hotkeyString)
110
121
111
122
public override string ToString ( )
112
123
{
113
- string text = string . Empty ;
124
+ List < string > keys = new List < string > ( ) ;
114
125
if ( Ctrl )
115
126
{
116
- text += "Ctrl + " ;
127
+ keys . Add ( "Ctrl" ) ;
117
128
}
118
129
if ( Alt )
119
130
{
120
- text += "Alt + " ;
131
+ keys . Add ( "Alt" ) ;
121
132
}
122
133
if ( Shift )
123
134
{
124
- text += "Shift + " ;
135
+ keys . Add ( "Shift" ) ;
125
136
}
126
137
if ( Win )
127
138
{
128
- text += "Win + " ;
139
+ keys . Add ( "Win" ) ;
129
140
}
130
141
131
142
if ( CharKey != Key . None )
132
143
{
133
- text += specialSymbolDictionary . ContainsKey ( CharKey )
144
+ keys . Add ( specialSymbolDictionary . ContainsKey ( CharKey )
134
145
? specialSymbolDictionary [ CharKey ]
135
- : CharKey . ToString ( ) ;
146
+ : CharKey . ToString ( ) ) ;
147
+ }
148
+ return string . Join ( " + " , keys ) ;
149
+ }
150
+
151
+ private static bool ValidateHotkey ( Key key )
152
+ {
153
+ HashSet < Key > invalidKeys = new ( )
154
+ {
155
+ Key . LeftAlt , Key . RightAlt ,
156
+ Key . LeftCtrl , Key . RightCtrl ,
157
+ Key . LeftShift , Key . RightShift ,
158
+ Key . LWin , Key . RWin ,
159
+ } ;
160
+
161
+ return ! invalidKeys . Contains ( key ) ;
162
+ }
163
+
164
+ public override bool Equals ( object obj )
165
+ {
166
+ if ( obj is HotkeyModel other )
167
+ {
168
+ return ModifierKeys == other . ModifierKeys && CharKey == other . charKey ;
136
169
}
137
- else if ( ! string . IsNullOrEmpty ( text ) )
170
+ else
138
171
{
139
- text = text . Remove ( text . Length - 3 ) ;
172
+ return false ;
140
173
}
174
+ }
141
175
142
- return text ;
176
+ public override int GetHashCode ( )
177
+ {
178
+ return this . ToString ( ) . GetHashCode ( ) ;
143
179
}
144
180
}
145
181
}
0 commit comments