ShortcutHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>Represents a helper to manipulate shortcut keys used on views.</summary>
  4. public class ShortcutHelper
  5. {
  6. // TODO: Update this to use Key, not KeyCode
  7. private KeyCode shortcut;
  8. /// <summary>This is the global setting that can be used as a global shortcut to invoke the action on the view.</summary>
  9. public virtual KeyCode Shortcut
  10. {
  11. get => shortcut;
  12. set
  13. {
  14. if (shortcut != value && (PostShortcutValidation (value) || value is KeyCode.Null))
  15. {
  16. shortcut = value;
  17. }
  18. }
  19. }
  20. /// <summary>The keystroke combination used in the <see cref="Shortcut"/> as string.</summary>
  21. public virtual string ShortcutTag => Key.ToString (shortcut, MenuBar.ShortcutDelimiter);
  22. /// <summary>Lookup for a <see cref="KeyCode"/> on range of keys.</summary>
  23. /// <param name="key">The source key.</param>
  24. /// <param name="first">First key in range.</param>
  25. /// <param name="last">Last key in range.</param>
  26. public static bool CheckKeysFlagRange (KeyCode key, KeyCode first, KeyCode last)
  27. {
  28. for (var i = (uint)first; i < (uint)last; i++)
  29. {
  30. if ((key | (KeyCode)i) == key)
  31. {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. /// <summary>Allows to retrieve a <see cref="KeyCode"/> from a <see cref="ShortcutTag"/></summary>
  38. /// <param name="tag">The key as string.</param>
  39. /// <param name="delimiter">The delimiter string.</param>
  40. public static KeyCode GetShortcutFromTag (string tag, Rune delimiter = default)
  41. {
  42. string sCut = tag;
  43. if (string.IsNullOrEmpty (sCut))
  44. {
  45. return default (KeyCode);
  46. }
  47. var key = KeyCode.Null;
  48. //var hasCtrl = false;
  49. if (delimiter == default (Rune))
  50. {
  51. delimiter = MenuBar.ShortcutDelimiter;
  52. }
  53. string [] keys = sCut.Split (delimiter.ToString ());
  54. for (var i = 0; i < keys.Length; i++)
  55. {
  56. string k = keys [i];
  57. if (k == "Ctrl")
  58. {
  59. //hasCtrl = true;
  60. key |= KeyCode.CtrlMask;
  61. }
  62. else if (k == "Shift")
  63. {
  64. key |= KeyCode.ShiftMask;
  65. }
  66. else if (k == "Alt")
  67. {
  68. key |= KeyCode.AltMask;
  69. }
  70. else if (k.StartsWith ("F") && k.Length > 1)
  71. {
  72. int.TryParse (k.Substring (1), out int n);
  73. for (var j = (uint)KeyCode.F1; j <= (uint)KeyCode.F12; j++)
  74. {
  75. int.TryParse (((KeyCode)j).ToString ().Substring (1), out int f);
  76. if (f == n)
  77. {
  78. key |= (KeyCode)j;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. key |= (KeyCode)Enum.Parse (typeof (KeyCode), k);
  85. }
  86. }
  87. return key;
  88. }
  89. /// <summary>Used at key up validation.</summary>
  90. /// <param name="key">The key to validate.</param>
  91. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  92. public static bool PostShortcutValidation (KeyCode key)
  93. {
  94. GetKeyToString (key, out KeyCode knm);
  95. if (CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12) || ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) != 0 && knm != KeyCode.Null))
  96. {
  97. return true;
  98. }
  99. Debug.WriteLine ($"WARNING: {Key.ToString (key)} is not a valid shortcut key.");
  100. return false;
  101. }
  102. /// <summary>Used at key down or key press validation.</summary>
  103. /// <param name="key">The key to validate.</param>
  104. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  105. public static bool PreShortcutValidation (KeyCode key)
  106. {
  107. if ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) == 0
  108. && !CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12))
  109. {
  110. return false;
  111. }
  112. return true;
  113. }
  114. /// <summary>Return key as string.</summary>
  115. /// <param name="key">The key to extract.</param>
  116. /// <param name="knm">Correspond to the non modifier key.</param>
  117. private static string GetKeyToString (KeyCode key, out KeyCode knm)
  118. {
  119. if (key == KeyCode.Null)
  120. {
  121. knm = KeyCode.Null;
  122. return "";
  123. }
  124. knm = key;
  125. KeyCode mK = key & (KeyCode.AltMask | KeyCode.CtrlMask | KeyCode.ShiftMask);
  126. knm &= ~mK;
  127. for (var i = (uint)KeyCode.F1; i < (uint)KeyCode.F12; i++)
  128. {
  129. if (knm == (KeyCode)i)
  130. {
  131. mK |= (KeyCode)i;
  132. }
  133. }
  134. knm &= ~mK;
  135. uint.TryParse (knm.ToString (), out uint c);
  136. string s = mK == KeyCode.Null ? "" : mK.ToString ();
  137. if (s != "" && (knm != KeyCode.Null || c > 0))
  138. {
  139. s += ",";
  140. }
  141. s += c == 0 ? knm == KeyCode.Null ? "" : knm.ToString () : ((char)c).ToString ();
  142. return s;
  143. }
  144. }