66 * PROGRAMER: Peter Geinitz (Wayfarer)
77 */
88
9+ using System ;
910using System . Collections . Generic ;
1011using System . Windows ;
1112using System . Windows . Controls ;
@@ -26,16 +27,14 @@ public static class GlobalKeyHandler
2627 ComCtlResources . GlobalKeyAttach , typeof ( bool ) , typeof ( GlobalKeyHandler ) ,
2728 new PropertyMetadata ( false , OnAttachChanged ) ) ;
2829
29-
3030 /// <summary>
31- /// DependencyProperty to store a dictionary of key-command pairs for a UIElement.
31+ /// DependencyProperty to store a dictionary of modifier/ key-command pairs for a UIElement.
3232 /// </summary>
3333 public static readonly DependencyProperty CommandBindingsProperty =
3434 DependencyProperty . RegisterAttached (
35- ComCtlResources . GlobalKeyCommandBindings , typeof ( Dictionary < Key , ICommand > ) , typeof ( GlobalKeyHandler ) ,
35+ ComCtlResources . GlobalKeyCommandBindings , typeof ( Dictionary < Tuple < ModifierKeys , Key > , ICommand > ) , typeof ( GlobalKeyHandler ) ,
3636 new PropertyMetadata ( null ) ) ;
3737
38-
3938 /// <summary>
4039 /// Skip text controls property
4140 /// </summary>
@@ -82,21 +81,20 @@ public static bool GetAttach(UIElement element)
8281 }
8382
8483 /// <summary>
85- /// Sets the dictionary of key-command bindings for the specified UIElement.
86- /// This dictionary maps specific keys to ICommand instances, allowing the element to handle
87- /// those keys by executing the associated commands.
84+ /// Sets the dictionary of modifier/key-command bindings for the specified UIElement.
85+ /// This dictionary maps specific keys combinations to ICommand instances.
8886 /// </summary>
89- public static void SetCommandBindings ( UIElement element , Dictionary < Key , ICommand > value )
87+ public static void SetCommandBindings ( UIElement element , Dictionary < Tuple < ModifierKeys , Key > , ICommand > value )
9088 {
9189 element . SetValue ( CommandBindingsProperty , value ) ;
9290 }
9391
9492 /// <summary>
9593 /// Gets the dictionary of key-command bindings associated with the specified UIElement.
9694 /// </summary>
97- public static Dictionary < Key , ICommand > GetCommandBindings ( UIElement element )
95+ public static Dictionary < Tuple < ModifierKeys , Key > , ICommand > GetCommandBindings ( UIElement element )
9896 {
99- return ( Dictionary < Key , ICommand > ) element . GetValue ( CommandBindingsProperty ) ;
97+ return ( Dictionary < Tuple < ModifierKeys , Key > , ICommand > ) element . GetValue ( CommandBindingsProperty ) ;
10098 }
10199
102100 /// <summary>
@@ -121,7 +119,7 @@ private static void OnAttachChanged(DependencyObject d, DependencyPropertyChange
121119
122120 /// <summary>
123121 /// Handles the PreviewKeyDown event on the attached UIElement.
124- /// This method checks if there is a command bound to the pressed key and executes it if allowed.
122+ /// This method checks if there is a command bound to the pressed key/modifier combination and executes it if allowed.
125123 /// </summary>
126124 private static void OnPreviewKeyDown ( object sender , KeyEventArgs e )
127125 {
@@ -147,8 +145,19 @@ private static void OnPreviewKeyDown(object sender, KeyEventArgs e)
147145 // Retrieve the dictionary of key-command bindings for this element
148146 var bindings = GetCommandBindings ( element ) ;
149147
150- // Check if a command is bound to the pressed key and if it can execute
151- if ( bindings == null || ! bindings . TryGetValue ( e . Key , out var command ) || ! command . CanExecute ( null ) )
148+ if ( bindings == null )
149+ {
150+ return ;
151+ }
152+
153+ // 1. Capture the currently pressed modifier keys (Ctrl, Alt, Shift, or None)
154+ ModifierKeys currentModifiers = Keyboard . Modifiers ;
155+
156+ // 2. Create the lookup tuple based on the current modifiers and the pressed key
157+ var keyCombination = new Tuple < ModifierKeys , Key > ( currentModifiers , e . Key ) ;
158+
159+ // 3. Check if a command is bound to this combination and if it can execute
160+ if ( ! bindings . TryGetValue ( keyCombination , out var command ) || ! command . CanExecute ( null ) )
152161 {
153162 return ;
154163 }
@@ -158,4 +167,4 @@ private static void OnPreviewKeyDown(object sender, KeyEventArgs e)
158167 e . Handled = true ;
159168 }
160169 }
161- }
170+ }
0 commit comments