@@ -202,6 +202,8 @@ private bool handleRepeat(InputState state)
202
202
return drawables . FirstOrDefault ( d => triggerKeyBindingEvent ( d , pressEvent ) ) != null ;
203
203
}
204
204
205
+ private readonly List < IKeyBinding > newlyPressed = new List < IKeyBinding > ( ) ;
206
+
205
207
private bool handleNewPressed ( InputState state , InputKey newKey , Vector2 ? scrollDelta = null , bool isPrecise = false )
206
208
{
207
209
pressedInputKeys . Add ( newKey ) ;
@@ -210,21 +212,36 @@ private bool handleNewPressed(InputState state, InputKey newKey, Vector2? scroll
210
212
var pressedCombination = new KeyCombination ( pressedInputKeys ) ;
211
213
212
214
bool handled = false ;
213
- var bindings = KeyBindings ? . Except ( pressedBindings ) ?? Enumerable . Empty < IKeyBinding > ( ) ;
214
- var newlyPressed = bindings . Where ( m =>
215
- m . KeyCombination . IsPressed ( pressedCombination , matchingMode ) ) ;
215
+
216
+ newlyPressed . Clear ( ) ;
217
+
218
+ if ( KeyBindings != null )
219
+ {
220
+ foreach ( IKeyBinding binding in KeyBindings )
221
+ {
222
+ if ( pressedBindings . Contains ( binding ) )
223
+ continue ;
224
+
225
+ if ( binding . KeyCombination . IsPressed ( pressedCombination , matchingMode ) )
226
+ newlyPressed . Add ( binding ) ;
227
+ }
228
+ }
216
229
217
230
if ( KeyCombination . IsModifierKey ( newKey ) )
218
231
{
219
232
// if the current key pressed was a modifier, only handle modifier-only bindings.
220
233
// lambda expression is used so that the delegate is cached (see: https://github.com/dotnet/roslyn/issues/5835)
221
234
// TODO: remove when we switch to .NET 7.
222
235
// ReSharper disable once ConvertClosureToMethodGroup
223
- newlyPressed = newlyPressed . Where ( b => b . KeyCombination . Keys . All ( key => KeyCombination . IsModifierKey ( key ) ) ) ;
236
+ for ( int i = 0 ; i < newlyPressed . Count ; i ++ )
237
+ {
238
+ if ( ! newlyPressed [ i ] . KeyCombination . Keys . All ( key => KeyCombination . IsModifierKey ( key ) ) )
239
+ newlyPressed . RemoveAt ( i -- ) ;
240
+ }
224
241
}
225
242
226
243
// we want to always handle bindings with more keys before bindings with less.
227
- newlyPressed = newlyPressed . OrderByDescending ( b => b . KeyCombination . Keys . Length ) . ToList ( ) ;
244
+ newlyPressed . Sort ( static ( a , b ) => b . KeyCombination . Keys . Length . CompareTo ( a . KeyCombination . Keys . Length ) ) ;
228
245
229
246
pressedBindings . AddRange ( newlyPressed ) ;
230
247
@@ -342,16 +359,16 @@ private void handleNewReleased(InputState state, InputKey releasedKey)
342
359
// we don't want to consider exact matching here as we are dealing with bindings, not actions.
343
360
var pressedCombination = new KeyCombination ( pressedInputKeys ) ;
344
361
345
- var newlyReleased = pressedInputKeys . Count == 0
346
- ? pressedBindings . ToList ( )
347
- : pressedBindings . Where ( b => ! b . KeyCombination . IsPressed ( pressedCombination , KeyCombinationMatchingMode . Any ) ) . ToList ( ) ;
348
-
349
- foreach ( var binding in newlyReleased )
362
+ for ( int i = 0 ; i < pressedBindings . Count ; i ++ )
350
363
{
351
- pressedBindings . Remove ( binding ) ;
364
+ var binding = pressedBindings [ i ] ;
352
365
353
- PropagateReleased ( getInputQueue ( binding ) . Where ( d => d . IsRootedAt ( this ) ) , state , binding . GetAction < T > ( ) ) ;
354
- keyBindingQueues [ binding ] . Clear ( ) ;
366
+ if ( pressedInputKeys . Count == 0 || ! binding . KeyCombination . IsPressed ( pressedCombination , KeyCombinationMatchingMode . Any ) )
367
+ {
368
+ pressedBindings . RemoveAt ( i -- ) ;
369
+ PropagateReleased ( getInputQueue ( binding ) . Where ( d => d . IsRootedAt ( this ) ) , state , binding . GetAction < T > ( ) ) ;
370
+ keyBindingQueues [ binding ] . Clear ( ) ;
371
+ }
355
372
}
356
373
}
357
374
0 commit comments