|
1 | | -using System.Windows.Input; |
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Windows.Input; |
2 | 4 |
|
3 | 5 | namespace Nodify |
4 | 6 | { |
@@ -101,10 +103,113 @@ public override bool Matches(object targetElement, InputEventArgs inputEventArgs |
101 | 103 | public static implicit operator InputGestureRef(MouseGesture gesture) |
102 | 104 | => new InputGestureRef { Value = gesture }; |
103 | 105 |
|
| 106 | + public static implicit operator InputGestureRef(System.Windows.Input.MouseGesture gesture) |
| 107 | + => new InputGestureRef { Value = gesture }; |
| 108 | + |
104 | 109 | public static implicit operator InputGestureRef(KeyGesture gesture) |
105 | 110 | => new InputGestureRef { Value = gesture }; |
106 | 111 |
|
107 | 112 | public static implicit operator InputGestureRef(MultiGesture gesture) |
108 | 113 | => new InputGestureRef { Value = gesture }; |
109 | 114 | } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Represents a mouse gesture that optionally includes a specific key press as part of the gesture. |
| 118 | + /// </summary> |
| 119 | + public sealed class MouseGesture : System.Windows.Input.MouseGesture |
| 120 | + { |
| 121 | + /// <summary> |
| 122 | + /// Gets or sets the key that must be pressed to match this gesture. |
| 123 | + /// </summary> |
| 124 | + public Key Key { get; set; } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Initializes a new instance of the <see cref="MouseGesture"/> class with the specified mouse action, modifier keys, and a specific key. |
| 128 | + /// </summary> |
| 129 | + /// <param name="action">The action associated with this gesture.</param> |
| 130 | + /// <param name="modifiers">The modifiers associated with this gesture.</param> |
| 131 | + /// <param name="key">The key required to match the gesture.</param> |
| 132 | + public MouseGesture(MouseAction action, ModifierKeys modifiers, Key key) : base(action, modifiers) |
| 133 | + { |
| 134 | + Key = key; |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Initializes a new instance of the <see cref="MouseGesture"/> class with the specified mouse action and key. |
| 139 | + /// </summary> |
| 140 | + /// <param name="action">The action associated with this gesture.</param> |
| 141 | + /// <param name="key">The key required to match the gesture.</param> |
| 142 | + public MouseGesture(MouseAction action, Key key) : base(action) |
| 143 | + { |
| 144 | + Key = key; |
| 145 | + } |
| 146 | + |
| 147 | + /// <inheritdoc /> |
| 148 | + public MouseGesture(MouseAction action, ModifierKeys modifiers) : base(action, modifiers) |
| 149 | + { |
| 150 | + } |
| 151 | + |
| 152 | + /// <inheritdoc /> |
| 153 | + public MouseGesture(MouseAction action) : base(action) |
| 154 | + { |
| 155 | + } |
| 156 | + |
| 157 | + /// <inheritdoc /> |
| 158 | + public MouseGesture() |
| 159 | + { |
| 160 | + } |
| 161 | + |
| 162 | + /// <inheritdoc /> |
| 163 | + public override bool Matches(object targetElement, InputEventArgs inputEventArgs) |
| 164 | + { |
| 165 | + if (inputEventArgs is MouseButtonEventArgs || inputEventArgs is MouseWheelEventArgs) |
| 166 | + { |
| 167 | + return base.Matches(targetElement, inputEventArgs) && MatchesKeyboard(); |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Checks whether the required key is pressed or no keys are pressed when <see cref="Key"/> is <see cref="Key.None"/>. |
| 175 | + /// </summary> |
| 176 | + private bool MatchesKeyboard() |
| 177 | + { |
| 178 | + if (Key is Key.None) |
| 179 | + { |
| 180 | + return !IsAnyKeyPressed(); |
| 181 | + } |
| 182 | + |
| 183 | + return Keyboard.GetKeyStates(Key).HasFlag(KeyStates.Down); |
| 184 | + } |
| 185 | + |
| 186 | + private static readonly Key[] _allKeys = GetValidKeys(); |
| 187 | + |
| 188 | + private static Key[] GetValidKeys() |
| 189 | + { |
| 190 | + var excludedKeys = new[] |
| 191 | + { |
| 192 | + Key.LeftCtrl, Key.RightCtrl, |
| 193 | + Key.LeftShift, Key.RightShift, |
| 194 | + Key.LeftAlt, Key.RightAlt, |
| 195 | + Key.LWin, Key.RWin, |
| 196 | + Key.None |
| 197 | + }; |
| 198 | + |
| 199 | +#if NET5_0_OR_GREATER |
| 200 | + return Enum.GetValues<Key>() |
| 201 | +#else |
| 202 | + return Enum.GetValues(typeof(Key)) |
| 203 | + .Cast<Key>() |
| 204 | +#endif |
| 205 | + .Where(key => !excludedKeys.Contains(key)) |
| 206 | + .ToArray(); |
| 207 | + } |
| 208 | + |
| 209 | + /// <summary> |
| 210 | + /// Determines whether any key (excluding modifiers) is currently pressed. |
| 211 | + /// </summary> |
| 212 | + private static bool IsAnyKeyPressed() |
| 213 | + => _allKeys.Any(Keyboard.IsKeyDown); |
| 214 | + } |
110 | 215 | } |
0 commit comments