11using System ;
2- using System . Collections . Generic ;
32using System . Diagnostics . CodeAnalysis ;
3+ using System . Globalization ;
44using System . Windows . Input ;
55
66namespace IPConfig . Models . Messages ;
77
88public class KeyPressMessage : ISender
99{
10- public KeyEventArgs ? Args { get ; }
10+ public KeyEventArgs Args { get ; }
1111
1212 public string Gesture { get ; private set ; }
1313
1414 public object Sender { get ; }
1515
16- public KeyPressMessage ( object sender , string gesture )
17- {
18- Sender = sender ;
19- Gesture = gesture ;
20- }
21-
2216 public KeyPressMessage ( object sender , KeyEventArgs args )
2317 {
2418 Sender = sender ;
@@ -27,40 +21,31 @@ public KeyPressMessage(object sender, KeyEventArgs args)
2721 GetGesture ( ) ;
2822 }
2923
30- [ MemberNotNull ( nameof ( Gesture ) ) ]
31- private void GetGesture ( )
24+ public bool GestureEquals ( string gestureString )
3225 {
33- var keys = new List < string > ( ) ;
26+ var gestureConverter = new KeyGestureConverter ( ) ;
27+ var keyGesture = gestureConverter . ConvertFromInvariantString ( gestureString ) as KeyGesture ;
28+ string ? normalizedGestureString = keyGesture ? . GetDisplayStringForCulture ( CultureInfo . InvariantCulture ) ;
3429
35- if ( Args ! . KeyboardDevice . Modifiers . HasFlag ( ModifierKeys . Windows ) )
30+ if ( normalizedGestureString is not null )
3631 {
37- keys . Add ( "Win" ) ;
32+ return normalizedGestureString . Equals ( Gesture , StringComparison . OrdinalIgnoreCase ) ;
3833 }
3934
40- if ( Args . KeyboardDevice . Modifiers . HasFlag ( ModifierKeys . Control ) )
41- {
42- keys . Add ( "Ctrl" ) ;
43- }
44-
45- if ( Args . KeyboardDevice . Modifiers . HasFlag ( ModifierKeys . Alt ) )
46- {
47- keys . Add ( "Alt" ) ;
48- }
49-
50- if ( Args . KeyboardDevice . Modifiers . HasFlag ( ModifierKeys . Shift ) )
51- {
52- keys . Add ( "Shift" ) ;
53- }
35+ return false ;
36+ }
5437
55- if ( Args . Key == Key . Escape )
38+ [ MemberNotNull ( nameof ( Gesture ) ) ]
39+ private void GetGesture ( )
40+ {
41+ try
5642 {
57- keys . Add ( "Esc" ) ;
43+ var gesture = new KeyGesture ( Args . Key , Args . KeyboardDevice . Modifiers ) ;
44+ Gesture = gesture . GetDisplayStringForCulture ( CultureInfo . InvariantCulture ) ;
5845 }
59- else
46+ catch ( NotSupportedException )
6047 {
61- keys . Add ( Args . Key . ToString ( ) ) ;
48+ Gesture = Args . Key . ToString ( ) ;
6249 }
63-
64- Gesture = String . Join ( '+' , keys ) ;
6550 }
6651}
0 commit comments