From b463d04e9f1c7d236e6002bd34ce84face1de188 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 14 Dec 2017 23:11:26 -0600 Subject: [PATCH] Make KeyGestures with +,-,. in them match with both the non-numpad keys and the matching numpad keys. Fixes #225 --- src/Avalonia.Input/KeyGesture.cs | 17 ++++++- .../KeyGestureParseTests.cs | 28 ------------ .../KeyGestureTests.cs | 44 +++++++++++++++++++ 3 files changed, 60 insertions(+), 29 deletions(-) delete mode 100644 tests/Avalonia.Input.UnitTests/KeyGestureParseTests.cs create mode 100644 tests/Avalonia.Input.UnitTests/KeyGestureTests.cs diff --git a/src/Avalonia.Input/KeyGesture.cs b/src/Avalonia.Input/KeyGesture.cs index 88d3c99839..954333b80f 100644 --- a/src/Avalonia.Input/KeyGesture.cs +++ b/src/Avalonia.Input/KeyGesture.cs @@ -111,6 +111,21 @@ namespace Avalonia.Input return string.Join(" + ", parts); } - public bool Matches(KeyEventArgs keyEvent) => keyEvent.Key == Key && keyEvent.Modifiers == Modifiers; + public bool Matches(KeyEventArgs keyEvent) => ResolveNumPadOperationKey(keyEvent.Key) == Key && keyEvent.Modifiers == Modifiers; + + private Key ResolveNumPadOperationKey(Key key) + { + switch (key) + { + case Key.Add: + return Key.OemPlus; + case Key.Subtract: + return Key.OemMinus; + case Key.Decimal: + return Key.OemPeriod; + default: + return key; + } + } } } diff --git a/tests/Avalonia.Input.UnitTests/KeyGestureParseTests.cs b/tests/Avalonia.Input.UnitTests/KeyGestureParseTests.cs deleted file mode 100644 index d26b03ad80..0000000000 --- a/tests/Avalonia.Input.UnitTests/KeyGestureParseTests.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; - -namespace Avalonia.Input.UnitTests -{ - public class KeyGestureParseTests - { - private static readonly Dictionary SampleData = new Dictionary - { - {"Ctrl+A", new KeyGesture {Key = Key.A, Modifiers = InputModifiers.Control}}, - {" \tShift\t+Alt +B", new KeyGesture {Key = Key.B, Modifiers = InputModifiers.Shift|InputModifiers.Alt} }, - {"Control++", new KeyGesture {Key = Key.OemPlus, Modifiers = InputModifiers.Control} } - }; - - - - [Fact] - public void Key_Gesture_Is_Able_To_Parse_Sample_Data() - { - foreach (var d in SampleData) - Assert.Equal(d.Value, KeyGesture.Parse(d.Key)); - } - } -} diff --git a/tests/Avalonia.Input.UnitTests/KeyGestureTests.cs b/tests/Avalonia.Input.UnitTests/KeyGestureTests.cs new file mode 100644 index 0000000000..006ed1140e --- /dev/null +++ b/tests/Avalonia.Input.UnitTests/KeyGestureTests.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Avalonia.Input.UnitTests +{ + public class KeyGestureTests + { + public static readonly IEnumerable SampleData = new object[][] + { + new object[]{"Ctrl+A", new KeyGesture {Key = Key.A, Modifiers = InputModifiers.Control}}, + new object[]{" \tShift\t+Alt +B", new KeyGesture {Key = Key.B, Modifiers = InputModifiers.Shift|InputModifiers.Alt} }, + new object[]{"Control++", new KeyGesture {Key = Key.OemPlus, Modifiers = InputModifiers.Control} } + }; + + + + [Theory] + [MemberData(nameof(SampleData))] + public void Key_Gesture_Is_Able_To_Parse_Sample_Data(string text, KeyGesture gesture) + { + Assert.Equal(gesture, KeyGesture.Parse(text)); + } + + [Theory] + [InlineData(Key.OemMinus, Key.Subtract)] + [InlineData(Key.OemPlus, Key.Add)] + [InlineData(Key.OemPeriod, Key.Decimal)] + public void Key_Gesture_Matches_NumPad_To_Regular_Digit(Key gestureKey, Key pressedKey) + { + var keyGesture = new KeyGesture + { + Key = gestureKey + }; + Assert.True(keyGesture.Matches(new KeyEventArgs + { + Key = pressedKey + })); + } + } +}