KeyGesture.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Avalonia.Utilities;
  5. namespace Avalonia.Input
  6. {
  7. /// <summary>
  8. /// Defines a keyboard input combination.
  9. /// </summary>
  10. public sealed class KeyGesture : IEquatable<KeyGesture>
  11. {
  12. private static readonly Dictionary<string, Key> s_keySynonyms = new Dictionary<string, Key>
  13. {
  14. { "+", Key.OemPlus }, { "-", Key.OemMinus }, { ".", Key.OemPeriod }, { ",", Key.OemComma }
  15. };
  16. public KeyGesture(Key key, KeyModifiers modifiers = KeyModifiers.None)
  17. {
  18. Key = key;
  19. KeyModifiers = modifiers;
  20. }
  21. public bool Equals(KeyGesture? other)
  22. {
  23. if (ReferenceEquals(null, other)) return false;
  24. if (ReferenceEquals(this, other)) return true;
  25. return Key == other.Key && KeyModifiers == other.KeyModifiers;
  26. }
  27. public override bool Equals(object? obj)
  28. {
  29. if (ReferenceEquals(null, obj)) return false;
  30. if (ReferenceEquals(this, obj)) return true;
  31. return obj is KeyGesture gesture && Equals(gesture);
  32. }
  33. public override int GetHashCode()
  34. {
  35. unchecked
  36. {
  37. return ((int)Key * 397) ^ (int)KeyModifiers;
  38. }
  39. }
  40. public static bool operator ==(KeyGesture? left, KeyGesture? right)
  41. {
  42. return Equals(left, right);
  43. }
  44. public static bool operator !=(KeyGesture? left, KeyGesture? right)
  45. {
  46. return !Equals(left, right);
  47. }
  48. public Key Key { get; }
  49. public KeyModifiers KeyModifiers { get; }
  50. public static KeyGesture Parse(string gesture)
  51. {
  52. // string.Split can't be used here because "Ctrl++" is a perfectly valid key gesture
  53. var key = Key.None;
  54. var keyModifiers = KeyModifiers.None;
  55. var cstart = 0;
  56. for (var c = 0; c <= gesture.Length; c++)
  57. {
  58. var ch = c == gesture.Length ? '\0' : gesture[c];
  59. bool isLast = c == gesture.Length;
  60. if (isLast || (ch == '+' && cstart != c))
  61. {
  62. var partSpan = gesture.AsSpan(cstart, c - cstart).Trim();
  63. if (isLast)
  64. {
  65. key = ParseKey(partSpan.ToString());
  66. }
  67. else
  68. {
  69. keyModifiers |= ParseModifier(partSpan);
  70. }
  71. cstart = c + 1;
  72. }
  73. }
  74. return new KeyGesture(key, keyModifiers);
  75. }
  76. public override string ToString()
  77. {
  78. var s = StringBuilderCache.Acquire();
  79. static void Plus(StringBuilder s)
  80. {
  81. if (s.Length > 0)
  82. {
  83. s.Append("+");
  84. }
  85. }
  86. if (KeyModifiers.HasAllFlags(KeyModifiers.Control))
  87. {
  88. s.Append("Ctrl");
  89. }
  90. if (KeyModifiers.HasAllFlags(KeyModifiers.Shift))
  91. {
  92. Plus(s);
  93. s.Append("Shift");
  94. }
  95. if (KeyModifiers.HasAllFlags(KeyModifiers.Alt))
  96. {
  97. Plus(s);
  98. s.Append("Alt");
  99. }
  100. if (KeyModifiers.HasAllFlags(KeyModifiers.Meta))
  101. {
  102. Plus(s);
  103. s.Append("Cmd");
  104. }
  105. Plus(s);
  106. s.Append(Key);
  107. return StringBuilderCache.GetStringAndRelease(s);
  108. }
  109. public bool Matches(KeyEventArgs keyEvent) =>
  110. keyEvent != null &&
  111. keyEvent.KeyModifiers == KeyModifiers &&
  112. ResolveNumPadOperationKey(keyEvent.Key) == ResolveNumPadOperationKey(Key);
  113. // TODO: Move that to external key parser
  114. private static Key ParseKey(string key)
  115. {
  116. if (s_keySynonyms.TryGetValue(key.ToLower(), out Key rv))
  117. return rv;
  118. return EnumHelper.Parse<Key>(key, true);
  119. }
  120. private static KeyModifiers ParseModifier(ReadOnlySpan<char> modifier)
  121. {
  122. if (modifier.Equals("ctrl".AsSpan(), StringComparison.OrdinalIgnoreCase))
  123. {
  124. return KeyModifiers.Control;
  125. }
  126. if (modifier.Equals("cmd".AsSpan(), StringComparison.OrdinalIgnoreCase) ||
  127. modifier.Equals("win".AsSpan(), StringComparison.OrdinalIgnoreCase) ||
  128. modifier.Equals("⌘".AsSpan(), StringComparison.OrdinalIgnoreCase))
  129. {
  130. return KeyModifiers.Meta;
  131. }
  132. return EnumHelper.Parse<KeyModifiers>(modifier.ToString(), true);
  133. }
  134. private Key ResolveNumPadOperationKey(Key key)
  135. {
  136. switch (key)
  137. {
  138. case Key.Add:
  139. return Key.OemPlus;
  140. case Key.Subtract:
  141. return Key.OemMinus;
  142. case Key.Decimal:
  143. return Key.OemPeriod;
  144. default:
  145. return key;
  146. }
  147. }
  148. }
  149. }