浏览代码

Switched key events to use KeyModifiers

Nikita Tsukanov 6 年之前
父节点
当前提交
3d4a2781b0

+ 10 - 6
src/Avalonia.Controls/Platform/InProcessDragSource.cs

@@ -57,11 +57,15 @@ namespace Avalonia.Platform
         }
 
 
-        private DragDropEffects RaiseEventAndUpdateCursor(RawDragEventType type, IInputElement root, Point pt, InputModifiers modifiers)
+        private DragDropEffects RaiseEventAndUpdateCursor(RawDragEventType type, IInputElement root, Point pt,
+            InputModifiers modifiers)
+            => RaiseEventAndUpdateCursor(type, root, pt, (RawInputModifiers)modifiers);
+        
+        private DragDropEffects RaiseEventAndUpdateCursor(RawDragEventType type, IInputElement root, Point pt, RawInputModifiers modifiers)
         {
             _lastPosition = pt;
 
-            RawDragEvent rawEvent = new RawDragEvent(_dragDrop, type, root, pt, _draggedData, _allowedEffects, modifiers);
+            RawDragEvent rawEvent = new RawDragEvent(_dragDrop, type, root, pt, _draggedData, _allowedEffects, (InputModifiers)modifiers);
             var tl = root.GetSelfAndVisualAncestors().OfType<TopLevel>().FirstOrDefault();
             tl.PlatformImpl?.Input(rawEvent);
 
@@ -70,13 +74,13 @@ namespace Avalonia.Platform
             return effect;
         }
 
-        private DragDropEffects GetPreferredEffect(DragDropEffects effect, InputModifiers modifiers)
+        private DragDropEffects GetPreferredEffect(DragDropEffects effect, RawInputModifiers modifiers)
         {
             if (effect == DragDropEffects.Copy || effect == DragDropEffects.Move || effect == DragDropEffects.Link || effect == DragDropEffects.None)
                 return effect; // No need to check for the modifiers.
-            if (effect.HasFlag(DragDropEffects.Link) && modifiers.HasFlag(InputModifiers.Alt))
+            if (effect.HasFlag(DragDropEffects.Link) && modifiers.HasFlag(RawInputModifiers.Alt))
                 return DragDropEffects.Link;
-            if (effect.HasFlag(DragDropEffects.Copy) && modifiers.HasFlag(InputModifiers.Control))
+            if (effect.HasFlag(DragDropEffects.Copy) && modifiers.HasFlag(RawInputModifiers.Control))
                 return DragDropEffects.Copy;
             return DragDropEffects.Move;
         }
@@ -132,7 +136,7 @@ namespace Avalonia.Platform
         private void CancelDragging()
         {
             if (_lastRoot != null)
-                RaiseEventAndUpdateCursor(RawDragEventType.DragLeave, _lastRoot, _lastPosition, InputModifiers.None);
+                RaiseEventAndUpdateCursor(RawDragEventType.DragLeave, _lastRoot, _lastPosition, RawInputModifiers.None);
             UpdateCursor(null, DragDropEffects.None);
             _result.OnNext(DragDropEffects.None);
         }

+ 5 - 1
src/Avalonia.Controls/Remote/Server/RemoteServerTopLevelImpl.cs

@@ -57,6 +57,10 @@ namespace Avalonia.Controls.Remote.Server
             }
         }
 
+        private static RawInputModifiers GetAvaloniaRawInputModifiers(
+            Avalonia.Remote.Protocol.Input.InputModifiers[] modifiers)
+            => (RawInputModifiers)GetAvaloniaInputModifiers(modifiers);
+        
         private static InputModifiers GetAvaloniaInputModifiers (Avalonia.Remote.Protocol.Input.InputModifiers[] modifiers)
         {
             var result = InputModifiers.None;
@@ -225,7 +229,7 @@ namespace Avalonia.Controls.Remote.Server
                             0,
                             key.IsDown ? RawKeyEventType.KeyDown : RawKeyEventType.KeyUp,
                             (Key)key.Key,
-                            GetAvaloniaInputModifiers(key.Modifiers)));
+                            GetAvaloniaRawInputModifiers(key.Modifiers)));
                     }, DispatcherPriority.Input);
                 }
                 if(obj is TextInputEventMessage text)

+ 2 - 2
src/Avalonia.Diagnostics/DevTools.xaml.cs

@@ -116,9 +116,9 @@ namespace Avalonia.Diagnostics
 
         private void RawKeyDown(RawKeyEventArgs e)
         {
-            const InputModifiers modifiers = InputModifiers.Control | InputModifiers.Shift;
+            const RawInputModifiers modifiers = RawInputModifiers.Control | RawInputModifiers.Shift;
 
-            if ((e.Modifiers) == modifiers)
+            if (e.Modifiers == modifiers)
             {
                 var point = (Root.VisualRoot as IInputRoot)?.MouseDevice?.GetPosition(Root) ?? default(Point);
                 var control = Root.GetVisualsAt(point, x => (!(x is AdornerLayer) && x.IsVisible))

+ 30 - 1
src/Avalonia.Input/IKeyboardDevice.cs

@@ -6,7 +6,7 @@ using System.ComponentModel;
 
 namespace Avalonia.Input
 {
-    [Flags]
+    [Flags, Obsolete("Use KeyModifiers and PointerPointProperties")]
     public enum InputModifiers
     {
         None = 0,
@@ -19,6 +19,16 @@ namespace Avalonia.Input
         MiddleMouseButton = 64
     }
 
+    [Flags]
+    public enum KeyModifiers
+    {
+        None = 0,
+        Alt = 1,
+        Control = 2,
+        Shift = 4,
+        Meta = 8,
+    }
+
     [Flags]
     public enum KeyStates
     {
@@ -27,6 +37,25 @@ namespace Avalonia.Input
         Toggled = 2,
     }
 
+    public enum RawInputModifiers
+    {
+        None = 0,
+        Alt = 1,
+        Control = 2,
+        Shift = 4,
+        Meta = 8,
+        LeftMouseButton = 16,
+        RightMouseButton = 32,
+        MiddleMouseButton = 64,
+        KeyboardMask = Alt | Control | Shift | Meta
+    }
+
+    internal static class KeyModifiersUtils
+    {
+        public static KeyModifiers ConvertToKey(RawInputModifiers modifiers) =>
+            (KeyModifiers)(modifiers & RawInputModifiers.KeyboardMask);
+    }
+
     public interface IKeyboardDevice : IInputDevice, INotifyPropertyChanged
     {
         IInputElement FocusedElement { get; }

+ 4 - 1
src/Avalonia.Input/KeyEventArgs.cs

@@ -1,6 +1,7 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
+using System;
 using Avalonia.Interactivity;
 
 namespace Avalonia.Input
@@ -11,6 +12,8 @@ namespace Avalonia.Input
 
         public Key Key { get; set; }
 
-        public InputModifiers Modifiers { get; set; }
+        [Obsolete("Use KeyModifiers")]
+        public InputModifiers Modifiers => (InputModifiers)KeyModifiers;
+        public KeyModifiers KeyModifiers { get; set; }
     }
 }

+ 8 - 1
src/Avalonia.Input/KeyGesture.cs

@@ -51,7 +51,14 @@ namespace Avalonia.Input
 
         public Key Key { get; set; }
 
-        public InputModifiers Modifiers { get; set; }
+        [Obsolete("Use KeyModifiers")]
+        public InputModifiers Modifiers
+        {
+            get => (InputModifiers)KeyModifiers;
+            set => KeyModifiers = (KeyModifiers)(((int)value) & 0xf);
+        }
+        
+        public KeyModifiers KeyModifiers { get; set; }
 
         
         static readonly Dictionary<string, Key> KeySynonyms = new Dictionary<string, Key>

+ 1 - 1
src/Avalonia.Input/KeyboardDevice.cs

@@ -91,7 +91,7 @@ namespace Avalonia.Input
                                 RoutedEvent = routedEvent,
                                 Device = this,
                                 Key = keyInput.Key,
-                                Modifiers = keyInput.Modifiers,
+                                KeyModifiers = KeyModifiersUtils.ConvertToKey(keyInput.Modifiers),
                                 Source = element,
                             };
 

+ 2 - 2
src/Avalonia.Input/Raw/RawKeyEventArgs.cs

@@ -15,7 +15,7 @@ namespace Avalonia.Input.Raw
             IKeyboardDevice device,
             ulong timestamp,
             RawKeyEventType type,
-            Key key, InputModifiers modifiers)
+            Key key, RawInputModifiers modifiers)
             : base(device, timestamp)
         {
             Key = key;
@@ -25,7 +25,7 @@ namespace Avalonia.Input.Raw
 
         public Key Key { get; set; }
 
-        public InputModifiers Modifiers { get; set; }
+        public RawInputModifiers Modifiers { get; set; }
 
         public RawKeyEventType Type { get; set; }
     }

+ 1 - 1
src/Avalonia.Native/WindowImplBase.cs

@@ -209,7 +209,7 @@ namespace Avalonia.Native
         {
             Dispatcher.UIThread.RunJobs(DispatcherPriority.Input + 1);
 
-            var args = new RawKeyEventArgs(_keyboard, timeStamp, (RawKeyEventType)type, (Key)key, (InputModifiers)modifiers);
+            var args = new RawKeyEventArgs(_keyboard, timeStamp, (RawKeyEventType)type, (Key)key, (RawInputModifiers)modifiers);
 
             Input?.Invoke(args);
 

+ 2 - 1
src/Avalonia.X11/X11Window.cs

@@ -438,7 +438,7 @@ namespace Avalonia.X11
                 
                 ScheduleInput(new RawKeyEventArgs(_keyboard, (ulong)ev.KeyEvent.time.ToInt64(),
                     ev.type == XEventName.KeyPress ? RawKeyEventType.KeyDown : RawKeyEventType.KeyUp,
-                    X11KeyTransform.ConvertKey(key), TranslateModifiers(ev.KeyEvent.state)), ref ev);
+                    X11KeyTransform.ConvertKey(key), TranslateRawModifiers(ev.KeyEvent.state)), ref ev);
 
                 if (ev.type == XEventName.KeyPress)
                 {
@@ -559,6 +559,7 @@ namespace Avalonia.X11
         }
 
 
+        RawInputModifiers TranslateRawModifiers(XModifierMask state) => (RawInputModifiers)TranslateModifiers(state);
         InputModifiers TranslateModifiers(XModifierMask state)
         {
             var rv = default(InputModifiers);

+ 1 - 0
src/Windows/Avalonia.Win32/Input/WindowsKeyboardDevice.cs

@@ -14,6 +14,7 @@ namespace Avalonia.Win32.Input
 
         public new static WindowsKeyboardDevice Instance { get; } = new WindowsKeyboardDevice();
 
+        public RawInputModifiers RawModifiers => (RawInputModifiers)Modifiers;
         public InputModifiers Modifiers
         {
             get

+ 2 - 2
src/Windows/Avalonia.Win32/WindowImpl.cs

@@ -510,7 +510,7 @@ namespace Avalonia.Win32
                             WindowsKeyboardDevice.Instance,
                             timestamp,
                             RawKeyEventType.KeyDown,
-                            KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
+                            KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.RawModifiers);
                     break;
 
                 case UnmanagedMethods.WindowsMessage.WM_MENUCHAR:
@@ -523,7 +523,7 @@ namespace Avalonia.Win32
                             WindowsKeyboardDevice.Instance,
                             timestamp,
                             RawKeyEventType.KeyUp,
-                            KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
+                            KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.RawModifiers);
                     break;
                 case UnmanagedMethods.WindowsMessage.WM_CHAR:
                     // Ignore control chars

+ 15 - 15
tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

@@ -55,7 +55,7 @@ namespace Avalonia.Controls.UnitTests
                     Text = "1234"
                 };
 
-                RaiseKeyEvent(target, Key.A, InputModifiers.Control);
+                RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
 
                 Assert.Equal(0, target.SelectionStart);
                 Assert.Equal(4, target.SelectionEnd);
@@ -72,7 +72,7 @@ namespace Avalonia.Controls.UnitTests
                     Template = CreateTemplate()
                 };
 
-                RaiseKeyEvent(target, Key.A, InputModifiers.Control);
+                RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
 
                 Assert.Equal(0, target.SelectionStart);
                 Assert.Equal(0, target.SelectionEnd);
@@ -90,7 +90,7 @@ namespace Avalonia.Controls.UnitTests
                     Text = "1234"
                 };
 
-                RaiseKeyEvent(target, Key.Z, InputModifiers.Control);
+                RaiseKeyEvent(target, Key.Z, KeyModifiers.Control);
 
                 Assert.Equal("1234", target.Text);
             }
@@ -136,29 +136,29 @@ namespace Avalonia.Controls.UnitTests
                 };
 
                 // (First| Second Third Fourth)
-                RaiseKeyEvent(textBox, Key.Back, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                 Assert.Equal(" Second Third Fourth", textBox.Text);
 
                 // ( Second |Third Fourth)
                 textBox.CaretIndex = 8;
-                RaiseKeyEvent(textBox, Key.Back, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                 Assert.Equal(" Third Fourth", textBox.Text);
 
                 // ( Thi|rd Fourth)
                 textBox.CaretIndex = 4;
-                RaiseKeyEvent(textBox, Key.Back, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                 Assert.Equal(" rd Fourth", textBox.Text);
 
                 // ( rd F[ou]rth)
                 textBox.SelectionStart = 5;
                 textBox.SelectionEnd = 7;
 
-                RaiseKeyEvent(textBox, Key.Back, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                 Assert.Equal(" rd Frth", textBox.Text);
 
                 // ( |rd Frth)
                 textBox.CaretIndex = 1;
-                RaiseKeyEvent(textBox, Key.Back, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                 Assert.Equal("rd Frth", textBox.Text);
             }
         }
@@ -175,30 +175,30 @@ namespace Avalonia.Controls.UnitTests
                 };
 
                 // (First Second Third |Fourth)
-                RaiseKeyEvent(textBox, Key.Delete, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                 Assert.Equal("First Second Third ", textBox.Text);
 
                 // (First Second |Third )
                 textBox.CaretIndex = 13;
-                RaiseKeyEvent(textBox, Key.Delete, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                 Assert.Equal("First Second ", textBox.Text);
 
                 // (First Sec|ond )
                 textBox.CaretIndex = 9;
-                RaiseKeyEvent(textBox, Key.Delete, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                 Assert.Equal("First Sec", textBox.Text);
 
                 // (Fi[rs]t Sec )
                 textBox.SelectionStart = 2;
                 textBox.SelectionEnd = 4;
 
-                RaiseKeyEvent(textBox, Key.Delete, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                 Assert.Equal("Fit Sec", textBox.Text);
 
                 // (Fit Sec| )
                 textBox.Text += " ";
                 textBox.CaretIndex = 7;
-                RaiseKeyEvent(textBox, Key.Delete, InputModifiers.Control);
+                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                 Assert.Equal("Fit Sec", textBox.Text);
             }
         }
@@ -486,12 +486,12 @@ namespace Avalonia.Controls.UnitTests
                 }.RegisterInNameScope(scope));
         }
 
-        private void RaiseKeyEvent(TextBox textBox, Key key, InputModifiers inputModifiers)
+        private void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
         {
             textBox.RaiseEvent(new KeyEventArgs
             {
                 RoutedEvent = InputElement.KeyDownEvent,
-                Modifiers = inputModifiers,
+                KeyModifiers = inputModifiers,
                 Key = key
             });
         }

+ 1 - 1
tests/Avalonia.Controls.UnitTests/TopLevelTests.cs

@@ -183,7 +183,7 @@ namespace Avalonia.Controls.UnitTests
                     new Mock<IKeyboardDevice>().Object,
                     0,
                     RawKeyEventType.KeyDown,
-                    Key.A, InputModifiers.None);
+                    Key.A, RawInputModifiers.None);
                 impl.Object.Input(input);
 
                 inputManagerMock.Verify(x => x.ProcessInput(input));

+ 3 - 3
tests/Avalonia.Controls.UnitTests/TreeViewTests.cs

@@ -619,7 +619,7 @@ namespace Avalonia.Controls.UnitTests
                 {
                     RoutedEvent = InputElement.KeyDownEvent,
                     Key = selectAllGesture.Key,
-                    Modifiers = selectAllGesture.Modifiers
+                    KeyModifiers = selectAllGesture.KeyModifiers
                 };
 
                 target.RaiseEvent(keyEvent);
@@ -665,7 +665,7 @@ namespace Avalonia.Controls.UnitTests
                 {
                     RoutedEvent = InputElement.KeyDownEvent,
                     Key = selectAllGesture.Key,
-                    Modifiers = selectAllGesture.Modifiers
+                    KeyModifiers = selectAllGesture.KeyModifiers
                 };
 
                 target.RaiseEvent(keyEvent);
@@ -711,7 +711,7 @@ namespace Avalonia.Controls.UnitTests
                 {
                     RoutedEvent = InputElement.KeyDownEvent,
                     Key = selectAllGesture.Key,
-                    Modifiers = selectAllGesture.Modifiers
+                    KeyModifiers = selectAllGesture.KeyModifiers
                 };
 
                 target.RaiseEvent(keyEvent);