ShortcutHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. // TODO: Nuke when #2975 is completed
  4. /// <summary>Represents a helper to manipulate shortcut keys used on views.</summary>
  5. public class ShortcutHelper
  6. {
  7. // TODO: Update this to use Key, not KeyCode
  8. private KeyCode shortcut;
  9. /// <summary>This is the global setting that can be used as a global shortcut to invoke the action on the view.</summary>
  10. public virtual KeyCode Shortcut
  11. {
  12. get => shortcut;
  13. set
  14. {
  15. if (shortcut != value && (PostShortcutValidation (value) || value is KeyCode.Null))
  16. {
  17. shortcut = value;
  18. }
  19. }
  20. }
  21. /// <summary>The keystroke combination used in the <see cref="Shortcut"/> as string.</summary>
  22. public virtual string ShortcutTag => Key.ToString (shortcut, Key.Separator);
  23. /// <summary>Lookup for a <see cref="KeyCode"/> on range of keys.</summary>
  24. /// <param name="key">The source key.</param>
  25. /// <param name="first">First key in range.</param>
  26. /// <param name="last">Last key in range.</param>
  27. public static bool CheckKeysFlagRange (KeyCode key, KeyCode first, KeyCode last)
  28. {
  29. for (var i = (uint)first; i < (uint)last; i++)
  30. {
  31. if ((key | (KeyCode)i) == key)
  32. {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. /// <summary>Allows to retrieve a <see cref="KeyCode"/> from a <see cref="ShortcutTag"/></summary>
  39. /// <param name="tag">The key as string.</param>
  40. /// <param name="delimiter">The delimiter string.</param>
  41. public static KeyCode GetShortcutFromTag (string tag, Rune delimiter = default)
  42. {
  43. string sCut = tag;
  44. if (string.IsNullOrEmpty (sCut))
  45. {
  46. return default (KeyCode);
  47. }
  48. var key = KeyCode.Null;
  49. //var hasCtrl = false;
  50. if (delimiter == default (Rune))
  51. {
  52. delimiter = Key.Separator;
  53. }
  54. string [] keys = sCut.Split (delimiter.ToString ());
  55. for (var i = 0; i < keys.Length; i++)
  56. {
  57. string k = keys [i];
  58. if (k == "Ctrl")
  59. {
  60. //hasCtrl = true;
  61. key |= KeyCode.CtrlMask;
  62. }
  63. else if (k == "Shift")
  64. {
  65. key |= KeyCode.ShiftMask;
  66. }
  67. else if (k == "Alt")
  68. {
  69. key |= KeyCode.AltMask;
  70. }
  71. else if (k.StartsWith ("F") && k.Length > 1)
  72. {
  73. int.TryParse (k.Substring (1), out int n);
  74. for (var j = (uint)KeyCode.F1; j <= (uint)KeyCode.F12; j++)
  75. {
  76. int.TryParse (((KeyCode)j).ToString ().Substring (1), out int f);
  77. if (f == n)
  78. {
  79. key |= (KeyCode)j;
  80. }
  81. }
  82. }
  83. else
  84. {
  85. key |= (KeyCode)Enum.Parse (typeof (KeyCode), k);
  86. }
  87. }
  88. return key;
  89. }
  90. /// <summary>Used at key up validation.</summary>
  91. /// <param name="key">The key to validate.</param>
  92. /// <returns><c>true</c> if is valid.<c>false</c>otherwise.</returns>
  93. public static bool PostShortcutValidation (KeyCode key)
  94. {
  95. GetKeyToString (key, out KeyCode knm);
  96. if (CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12) || ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) != 0 && knm != KeyCode.Null))
  97. {
  98. return true;
  99. }
  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. }