KeyCode.cs 9.5 KB

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