KeyCode.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// The <see cref="KeyCode"/> enumeration encodes key information from <see cref="IConsoleDriver"/>s and provides a
  5. /// consistent way for application code to specify keys and receive key events.
  6. /// <para>
  7. /// The <see cref="Key"/> class provides a higher-level abstraction, with helper methods and properties for
  8. /// common operations. For example, <see cref="Key.IsAlt"/> and <see cref="Key.IsCtrl"/> provide a convenient way
  9. /// to check whether the Alt or Ctrl modifier keys were pressed when a key was pressed.
  10. /// </para>
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// Lowercase alpha keys are encoded as values between 65 and 90 corresponding to the un-shifted A to Z keys on a
  15. /// keyboard. Enum values are provided for these (e.g. <see cref="KeyCode.A"/>, <see cref="KeyCode.B"/>, etc.).
  16. /// Even though the values are the same as the ASCII values for uppercase characters, these enum values represent
  17. /// *lowercase*, un-shifted characters.
  18. /// </para>
  19. /// <para>
  20. /// Numeric keys are the values between 48 and 57 corresponding to 0 to 9 (e.g. <see cref="KeyCode.D0"/>,
  21. /// <see cref="KeyCode.D1"/>, etc.).
  22. /// </para>
  23. /// <para>
  24. /// The shift modifiers (<see cref="KeyCode.ShiftMask"/>, <see cref="KeyCode.CtrlMask"/>, and
  25. /// <see cref="KeyCode.AltMask"/>) can be combined (with logical or) with the other key codes to represent shifted
  26. /// keys. For example, the <see cref="KeyCode.A"/> enum value represents the un-shifted 'a' key, while
  27. /// <see cref="KeyCode.ShiftMask"/> | <see cref="KeyCode.A"/> represents the 'A' key (shifted 'a' key). Likewise,
  28. /// <see cref="KeyCode.AltMask"/> | <see cref="KeyCode.A"/> represents the 'Alt+A' key combination.
  29. /// </para>
  30. /// <para>
  31. /// All other keys that produce a printable character are encoded as the Unicode value of the character. For
  32. /// example, the <see cref="KeyCode"/> for the '!' character is 33, which is the Unicode value for '!'. Likewise,
  33. /// `â` is 226, `Â` is 194, etc.
  34. /// </para>
  35. /// <para>
  36. /// If the <see cref="SpecialMask"/> is set, then the value is that of the special mask, otherwise, the value is
  37. /// the one of the lower bits (as extracted by <see cref="CharMask"/>).
  38. /// </para>
  39. /// </remarks>
  40. [Flags]
  41. public enum KeyCode : uint
  42. {
  43. /// <summary>
  44. /// Mask that indicates that the key is a unicode codepoint. Values outside this range indicate the key has shift
  45. /// modifiers or is a special key like function keys, arrows keys and so on.
  46. /// </summary>
  47. CharMask = 0x_f_ffff,
  48. /// <summary>
  49. /// If the <see cref="SpecialMask"/> is set, then the value is that of the special mask, otherwise, the value is
  50. /// in the lower bits (as extracted by <see cref="CharMask"/>).
  51. /// </summary>
  52. SpecialMask = 0x_fff0_0000,
  53. /// <summary>
  54. /// When this value is set, the Key encodes the sequence Shift-KeyValue. The actual value must be extracted by
  55. /// removing the ShiftMask.
  56. /// </summary>
  57. ShiftMask = 0x_1000_0000,
  58. /// <summary>
  59. /// When this value is set, the Key encodes the sequence Alt-KeyValue. The actual value must be extracted by
  60. /// removing the AltMask.
  61. /// </summary>
  62. AltMask = 0x_8000_0000,
  63. /// <summary>
  64. /// When this value is set, the Key encodes the sequence Ctrl-KeyValue. The actual value must be extracted by
  65. /// removing the CtrlMask.
  66. /// </summary>
  67. CtrlMask = 0x_4000_0000,
  68. /// <summary>The key code representing an invalid or empty key.</summary>
  69. Null = 0,
  70. /// <summary>Backspace key.</summary>
  71. Backspace = 8,
  72. /// <summary>The key code for the tab key (forwards tab key).</summary>
  73. Tab = 9,
  74. /// <summary>The key code for the return key.</summary>
  75. Enter = ConsoleKey.Enter,
  76. /// <summary>The key code for the clear key.</summary>
  77. Clear = 12,
  78. /// <summary>The key code for the escape key.</summary>
  79. Esc = 27,
  80. /// <summary>The key code for the space bar key.</summary>
  81. Space = 32,
  82. /// <summary>Digit 0.</summary>
  83. D0 = 48,
  84. /// <summary>Digit 1.</summary>
  85. D1,
  86. /// <summary>Digit 2.</summary>
  87. D2,
  88. /// <summary>Digit 3.</summary>
  89. D3,
  90. /// <summary>Digit 4.</summary>
  91. D4,
  92. /// <summary>Digit 5.</summary>
  93. D5,
  94. /// <summary>Digit 6.</summary>
  95. D6,
  96. /// <summary>Digit 7.</summary>
  97. D7,
  98. /// <summary>Digit 8.</summary>
  99. D8,
  100. /// <summary>Digit 9.</summary>
  101. D9,
  102. /// <summary>The key code for the A key</summary>
  103. A = 65,
  104. /// <summary>The key code for the B key</summary>
  105. B,
  106. /// <summary>The key code for the C key</summary>
  107. C,
  108. /// <summary>The key code for the D key</summary>
  109. D,
  110. /// <summary>The key code for the E key</summary>
  111. E,
  112. /// <summary>The key code for the F key</summary>
  113. F,
  114. /// <summary>The key code for the G key</summary>
  115. G,
  116. /// <summary>The key code for the H key</summary>
  117. H,
  118. /// <summary>The key code for the I key</summary>
  119. I,
  120. /// <summary>The key code for the J key</summary>
  121. J,
  122. /// <summary>The key code for the K key</summary>
  123. K,
  124. /// <summary>The key code for the L key</summary>
  125. L,
  126. /// <summary>The key code for the M key</summary>
  127. M,
  128. /// <summary>The key code for the N key</summary>
  129. N,
  130. /// <summary>The key code for the O key</summary>
  131. O,
  132. /// <summary>The key code for the P key</summary>
  133. P,
  134. /// <summary>The key code for the Q key</summary>
  135. Q,
  136. /// <summary>The key code for the R key</summary>
  137. R,
  138. /// <summary>The key code for the S key</summary>
  139. S,
  140. /// <summary>The key code for the T key</summary>
  141. T,
  142. /// <summary>The key code for the U key</summary>
  143. U,
  144. /// <summary>The key code for the V key</summary>
  145. V,
  146. /// <summary>The key code for the W key</summary>
  147. W,
  148. /// <summary>The key code for the X key</summary>
  149. X,
  150. /// <summary>The key code for the Y key</summary>
  151. Y,
  152. /// <summary>The key code for the Z key</summary>
  153. Z,
  154. ///// <summary>
  155. ///// The key code for the Delete key.
  156. ///// </summary>
  157. //Delete = 127,
  158. // --- Special keys ---
  159. // The values below are common non-alphanum keys. Their values are
  160. // based on the .NET ConsoleKey values, which, in-turn are based on the
  161. // VK_ values from the Windows API.
  162. // We add MaxCodePoint to avoid conflicts with the Unicode values.
  163. /// <summary>The maximum Unicode codepoint value. Used to encode the non-alphanumeric control keys.</summary>
  164. MaxCodePoint = 0x10FFFF,
  165. /// <summary>Cursor up key</summary>
  166. CursorUp = MaxCodePoint + ConsoleKey.UpArrow,
  167. /// <summary>Cursor down key.</summary>
  168. CursorDown = MaxCodePoint + ConsoleKey.DownArrow,
  169. /// <summary>Cursor left key.</summary>
  170. CursorLeft = MaxCodePoint + ConsoleKey.LeftArrow,
  171. /// <summary>Cursor right key.</summary>
  172. CursorRight = MaxCodePoint + ConsoleKey.RightArrow,
  173. /// <summary>Page Up key.</summary>
  174. PageUp = MaxCodePoint + ConsoleKey.PageUp,
  175. /// <summary>Page Down key.</summary>
  176. PageDown = MaxCodePoint + ConsoleKey.PageDown,
  177. /// <summary>Home key.</summary>
  178. Home = MaxCodePoint + ConsoleKey.Home,
  179. /// <summary>End key.</summary>
  180. End = MaxCodePoint + ConsoleKey.End,
  181. /// <summary>Insert (INS) key.</summary>
  182. Insert = MaxCodePoint + ConsoleKey.Insert,
  183. /// <summary>Delete (DEL) key.</summary>
  184. Delete = MaxCodePoint + ConsoleKey.Delete,
  185. /// <summary>Print screen character key.</summary>
  186. PrintScreen = MaxCodePoint + ConsoleKey.PrintScreen,
  187. /// <summary>F1 key.</summary>
  188. F1 = MaxCodePoint + ConsoleKey.F1,
  189. /// <summary>F2 key.</summary>
  190. F2 = MaxCodePoint + ConsoleKey.F2,
  191. /// <summary>F3 key.</summary>
  192. F3 = MaxCodePoint + ConsoleKey.F3,
  193. /// <summary>F4 key.</summary>
  194. F4 = MaxCodePoint + ConsoleKey.F4,
  195. /// <summary>F5 key.</summary>
  196. F5 = MaxCodePoint + ConsoleKey.F5,
  197. /// <summary>F6 key.</summary>
  198. F6 = MaxCodePoint + ConsoleKey.F6,
  199. /// <summary>F7 key.</summary>
  200. F7 = MaxCodePoint + ConsoleKey.F7,
  201. /// <summary>F8 key.</summary>
  202. F8 = MaxCodePoint + ConsoleKey.F8,
  203. /// <summary>F9 key.</summary>
  204. F9 = MaxCodePoint + ConsoleKey.F9,
  205. /// <summary>F10 key.</summary>
  206. F10 = MaxCodePoint + ConsoleKey.F10,
  207. /// <summary>F11 key.</summary>
  208. F11 = MaxCodePoint + ConsoleKey.F11,
  209. /// <summary>F12 key.</summary>
  210. F12 = MaxCodePoint + ConsoleKey.F12,
  211. /// <summary>F13 key.</summary>
  212. F13 = MaxCodePoint + ConsoleKey.F13,
  213. /// <summary>F14 key.</summary>
  214. F14 = MaxCodePoint + ConsoleKey.F14,
  215. /// <summary>F15 key.</summary>
  216. F15 = MaxCodePoint + ConsoleKey.F15,
  217. /// <summary>F16 key.</summary>
  218. F16 = MaxCodePoint + ConsoleKey.F16,
  219. /// <summary>F17 key.</summary>
  220. F17 = MaxCodePoint + ConsoleKey.F17,
  221. /// <summary>F18 key.</summary>
  222. F18 = MaxCodePoint + ConsoleKey.F18,
  223. /// <summary>F19 key.</summary>
  224. F19 = MaxCodePoint + ConsoleKey.F19,
  225. /// <summary>F20 key.</summary>
  226. F20 = MaxCodePoint + ConsoleKey.F20,
  227. /// <summary>F21 key.</summary>
  228. F21 = MaxCodePoint + ConsoleKey.F21,
  229. /// <summary>F22 key.</summary>
  230. F22 = MaxCodePoint + ConsoleKey.F22,
  231. /// <summary>F23 key.</summary>
  232. F23 = MaxCodePoint + ConsoleKey.F23,
  233. /// <summary>F24 key.</summary>
  234. F24 = MaxCodePoint + ConsoleKey.F24
  235. }