ShortcutHelper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Represents a helper to manipulate shortcut keys used on views.
  10. /// </summary>
  11. public class ShortcutHelper {
  12. private Key shortcut;
  13. /// <summary>
  14. /// This is the global setting that can be used as a global shortcut to invoke the action on the view.
  15. /// </summary>
  16. public virtual Key Shortcut {
  17. get => shortcut;
  18. set {
  19. if (shortcut != value && (PostShortcutValidation (value) || value == Key.Null)) {
  20. shortcut = value;
  21. }
  22. }
  23. }
  24. /// <summary>
  25. /// The keystroke combination used in the <see cref="Shortcut"/> as string.
  26. /// </summary>
  27. public virtual ustring ShortcutTag => GetShortcutTag (shortcut);
  28. /// <summary>
  29. /// The action to run if the <see cref="Shortcut"/> is defined.
  30. /// </summary>
  31. public virtual Action ShortcutAction { get; set; }
  32. /// <summary>
  33. /// Gets the key with all the keys modifiers, especially the shift key that sometimes have to be injected later.
  34. /// </summary>
  35. /// <param name="kb">The <see cref="KeyEvent"/> to check.</param>
  36. /// <returns>The <see cref="KeyEvent.Key"/> with all the keys modifiers.</returns>
  37. public static Key GetModifiersKey (KeyEvent kb)
  38. {
  39. var key = kb.Key;
  40. if (kb.IsAlt && (key & Key.AltMask) == 0) {
  41. key |= Key.AltMask;
  42. }
  43. if (kb.IsCtrl && (key & Key.CtrlMask) == 0) {
  44. key |= Key.CtrlMask;
  45. }
  46. if (kb.IsShift && (key & Key.ShiftMask) == 0) {
  47. key |= Key.ShiftMask;
  48. }
  49. return key;
  50. }
  51. /// <summary>
  52. /// Get the <see cref="Shortcut"/> key as string.
  53. /// </summary>
  54. /// <param name="shortcut">The shortcut key.</param>
  55. /// <returns></returns>
  56. public static ustring GetShortcutTag (Key shortcut)
  57. {
  58. if (shortcut == Key.Null) {
  59. return "";
  60. }
  61. var k = shortcut;
  62. var delimiter = MenuBar.ShortcutDelimiter;
  63. ustring tag = ustring.Empty;
  64. var sCut = GetKeyToString (k, out Key knm).ToString ();
  65. if (knm == Key.Unknown) {
  66. k &= ~Key.Unknown;
  67. sCut = GetKeyToString (k, out _).ToString ();
  68. }
  69. if ((k & Key.CtrlMask) != 0) {
  70. tag = "Ctrl";
  71. }
  72. if ((k & Key.ShiftMask) != 0) {
  73. if (!tag.IsEmpty) {
  74. tag += delimiter;
  75. }
  76. tag += "Shift";
  77. }
  78. if ((k & Key.AltMask) != 0) {
  79. if (!tag.IsEmpty) {
  80. tag += delimiter;
  81. }
  82. tag += "Alt";
  83. }
  84. ustring [] keys = ustring.Make (sCut).Split (",");
  85. for (int i = 0; i < keys.Length; i++) {
  86. var key = keys [i].TrimSpace ();
  87. if (key == Key.AltMask.ToString () || key == Key.ShiftMask.ToString () || key == Key.CtrlMask.ToString ()) {
  88. continue;
  89. }
  90. if (!tag.IsEmpty) {
  91. tag += delimiter;
  92. }
  93. if (!key.Contains ("F") && key.Length > 2 && keys.Length == 1) {
  94. k = (uint)Key.AltMask + k;
  95. tag += ((char)k).ToString ();
  96. } else if (key.Length == 2 && key.StartsWith ("D")) {
  97. tag += ((char)key.ElementAt (1)).ToString ();
  98. } else {
  99. tag += key;
  100. }
  101. }
  102. return tag;
  103. }
  104. /// <summary>
  105. /// Return key as string.
  106. /// </summary>
  107. /// <param name="key">The key to extract.</param>
  108. /// <param name="knm">Correspond to the non modifier key.</param>
  109. public static ustring GetKeyToString (Key key, out Key knm)
  110. {
  111. if (key == Key.Null) {
  112. knm = Key.Null;
  113. return "";
  114. }
  115. knm = key;
  116. var mK = key & (Key.AltMask | Key.CtrlMask | Key.ShiftMask);
  117. knm &= ~mK;
  118. for (uint i = (uint)Key.F1; i < (uint)Key.F12; i++) {
  119. if (knm == (Key)i) {
  120. mK |= (Key)i;
  121. }
  122. }
  123. knm &= ~mK;
  124. uint.TryParse (knm.ToString (), out uint c);
  125. var s = mK == Key.Null ? "" : mK.ToString ();
  126. if (s != "" && (knm != Key.Null || c > 0)) {
  127. s += ",";
  128. }
  129. s += c == 0 ? knm == Key.Null ? "" : knm.ToString () : ((char)c).ToString ();
  130. return s;
  131. }
  132. /// <summary>
  133. /// Allows to retrieve a <see cref="Key"/> from a <see cref="ShortcutTag"/>
  134. /// </summary>
  135. /// <param name="tag">The key as string.</param>
  136. public static Key GetShortcutFromTag (ustring tag)
  137. {
  138. var sCut = tag;
  139. if (sCut.IsEmpty) {
  140. return default;
  141. }
  142. Key key = Key.Null;
  143. //var hasCtrl = false;
  144. var delimiter = MenuBar.ShortcutDelimiter;
  145. ustring [] keys = sCut.Split (delimiter);
  146. for (int i = 0; i < keys.Length; i++) {
  147. var k = keys [i];
  148. if (k == "Ctrl") {
  149. //hasCtrl = true;
  150. key |= Key.CtrlMask;
  151. } else if (k == "Shift") {
  152. key |= Key.ShiftMask;
  153. } else if (k == "Alt") {
  154. key |= Key.AltMask;
  155. } else if (k.StartsWith ("F") && k.Length > 1) {
  156. int.TryParse (k.Substring (1).ToString (), out int n);
  157. for (uint j = (uint)Key.F1; j <= (uint)Key.F12; j++) {
  158. int.TryParse (((Key)j).ToString ().Substring (1), out int f);
  159. if (f == n) {
  160. key |= (Key)j;
  161. }
  162. }
  163. } else {
  164. key |= (Key)Enum.Parse (typeof (Key), k.ToString ());
  165. }
  166. }
  167. return key;
  168. }
  169. /// <summary>
  170. /// Lookup for a <see cref="Key"/> on range of keys.
  171. /// </summary>
  172. /// <param name="key">The source key.</param>
  173. /// <param name="first">First key in range.</param>
  174. /// <param name="last">Last key in range.</param>
  175. public static bool CheckKeysFlagRange (Key key, Key first, Key last)
  176. {
  177. for (uint i = (uint)first; i < (uint)last; i++) {
  178. if ((key | (Key)i) == key) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /// <summary>
  185. /// Used at key down or key press validation.
  186. /// </summary>
  187. /// <param name="key">The key to validate.</param>
  188. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  189. public static bool PreShortcutValidation (Key key)
  190. {
  191. if ((key & (Key.CtrlMask | Key.ShiftMask | Key.AltMask)) == 0 && !CheckKeysFlagRange (key, Key.F1, Key.F12)) {
  192. return false;
  193. }
  194. return true;
  195. }
  196. /// <summary>
  197. /// Used at key up validation.
  198. /// </summary>
  199. /// <param name="key">The key to validate.</param>
  200. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  201. public static bool PostShortcutValidation (Key key)
  202. {
  203. GetKeyToString (key, out Key knm);
  204. if (CheckKeysFlagRange (key, Key.F1, Key.F12) ||
  205. ((key & (Key.CtrlMask | Key.ShiftMask | Key.AltMask)) != 0 && knm != Key.Null && knm != Key.Unknown)) {
  206. return true;
  207. }
  208. return false;
  209. }
  210. /// <summary>
  211. /// Allows a view to run a <see cref="View.ShortcutAction"/> if defined.
  212. /// </summary>
  213. /// <param name="kb">The <see cref="KeyEvent"/></param>
  214. /// <param name="view">The <see cref="View"/></param>
  215. /// <returns><c>true</c> if defined <c>false</c>otherwise.</returns>
  216. public static bool FindAndOpenByShortcut (KeyEvent kb, View view = null)
  217. {
  218. if (view == null) {
  219. return false; }
  220. var key = kb.KeyValue;
  221. var keys = GetModifiersKey (kb);
  222. key |= (int)keys;
  223. foreach (var v in view.Subviews) {
  224. if (v.Shortcut != Key.Null && v.Shortcut == (Key)key) {
  225. var action = v.ShortcutAction;
  226. if (action != null) {
  227. Application.MainLoop.AddIdle (() => {
  228. action ();
  229. return false;
  230. });
  231. }
  232. return true;
  233. }
  234. if (FindAndOpenByShortcut (kb, v)) {
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. }
  241. }