Event.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // Evemts.cs: Events, Key mappings
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. namespace Terminal.Gui {
  9. /// <summary>
  10. /// The Key enumeration contains special encoding for some keys, but can also
  11. /// encode all the unicode values that can be passed.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// If the SpecialMask is set, then the value is that of the special mask,
  16. /// otherwise, the value is the one of the lower bits (as extracted by CharMask)
  17. /// </para>
  18. /// <para>
  19. /// Control keys are the values between 1 and 26 corresponding to Control-A to Control-Z
  20. /// </para>
  21. /// <para>
  22. /// Unicode runes are also stored here, the letter 'A" for example is encoded as a value 65 (not surfaced in the enum).
  23. /// </para>
  24. /// </remarks>
  25. public enum Key : uint {
  26. /// <summary>
  27. /// Mask that indictes that this is a character value, values outside this range
  28. /// indicate special characters like Alt-key combinations or special keys on the
  29. /// keyboard like function keys, arrows keys and so on.
  30. /// </summary>
  31. CharMask = 0xfffff,
  32. /// <summary>
  33. /// If the SpecialMask is set, then the value is that of the special mask,
  34. /// otherwise, the value is the one of the lower bits (as extracted by CharMask).
  35. /// </summary>
  36. SpecialMask = 0xfff00000,
  37. /// <summary>
  38. /// The key code for the user pressing Control-A
  39. /// </summary>
  40. ControlA = 1,
  41. /// <summary>
  42. /// The key code for the user pressing Control-B
  43. /// </summary>
  44. ControlB,
  45. /// <summary>
  46. /// The key code for the user pressing Control-C
  47. /// </summary>
  48. ControlC,
  49. /// <summary>
  50. /// The key code for the user pressing Control-D
  51. /// </summary>
  52. ControlD,
  53. /// <summary>
  54. /// The key code for the user pressing Control-E
  55. /// </summary>
  56. ControlE,
  57. /// <summary>
  58. /// The key code for the user pressing Control-F
  59. /// </summary>
  60. ControlF,
  61. /// <summary>
  62. /// The key code for the user pressing Control-G
  63. /// </summary>
  64. ControlG,
  65. /// <summary>
  66. /// The key code for the user pressing Control-H
  67. /// </summary>
  68. ControlH,
  69. /// <summary>
  70. /// The key code for the user pressing Control-I (same as the tab key).
  71. /// </summary>
  72. ControlI,
  73. /// <summary>
  74. /// The key code for the user pressing the tab key (same as pressing Control-I).
  75. /// </summary>
  76. Tab = ControlI,
  77. /// <summary>
  78. /// The key code for the user pressing Control-J
  79. /// </summary>
  80. ControlJ,
  81. /// <summary>
  82. /// The key code for the user pressing Control-K
  83. /// </summary>
  84. ControlK,
  85. /// <summary>
  86. /// The key code for the user pressing Control-L
  87. /// </summary>
  88. ControlL,
  89. /// <summary>
  90. /// The key code for the user pressing Control-M
  91. /// </summary>
  92. ControlM,
  93. /// <summary>
  94. /// The key code for the user pressing Control-N (same as the return key).
  95. /// </summary>
  96. ControlN,
  97. /// <summary>
  98. /// The key code for the user pressing Control-O
  99. /// </summary>
  100. ControlO,
  101. /// <summary>
  102. /// The key code for the user pressing Control-P
  103. /// </summary>
  104. ControlP,
  105. /// <summary>
  106. /// The key code for the user pressing Control-Q
  107. /// </summary>
  108. ControlQ,
  109. /// <summary>
  110. /// The key code for the user pressing Control-R
  111. /// </summary>
  112. ControlR,
  113. /// <summary>
  114. /// The key code for the user pressing Control-S
  115. /// </summary>
  116. ControlS,
  117. /// <summary>
  118. /// The key code for the user pressing Control-T
  119. /// </summary>
  120. ControlT,
  121. /// <summary>
  122. /// The key code for the user pressing Control-U
  123. /// </summary>
  124. ControlU,
  125. /// <summary>
  126. /// The key code for the user pressing Control-V
  127. /// </summary>
  128. ControlV,
  129. /// <summary>
  130. /// The key code for the user pressing Control-W
  131. /// </summary>
  132. ControlW,
  133. /// <summary>
  134. /// The key code for the user pressing Control-X
  135. /// </summary>
  136. ControlX,
  137. /// <summary>
  138. /// The key code for the user pressing Control-Y
  139. /// </summary>
  140. ControlY,
  141. /// <summary>
  142. /// The key code for the user pressing Control-Z
  143. /// </summary>
  144. ControlZ,
  145. /// <summary>
  146. /// The key code for the user pressing the escape key
  147. /// </summary>
  148. Esc = 27,
  149. /// <summary>
  150. /// The key code for the user pressing the return key.
  151. /// </summary>
  152. Enter = '\n',
  153. /// <summary>
  154. /// The key code for the user pressing the space bar
  155. /// </summary>
  156. Space = 32,
  157. /// <summary>
  158. /// The key code for the user pressing the delete key.
  159. /// </summary>
  160. Delete = 127,
  161. /// <summary>
  162. /// When this value is set, the Key encodes the sequence Alt-KeyValue.
  163. /// And the actual value must be extracted by removing the AltMask.
  164. /// </summary>
  165. AltMask = 0x80000000,
  166. /// <summary>
  167. /// Backspace key.
  168. /// </summary>
  169. Backspace = 0x100000,
  170. /// <summary>
  171. /// Cursor up key
  172. /// </summary>
  173. CursorUp,
  174. /// <summary>
  175. /// Cursor down key.
  176. /// </summary>
  177. CursorDown,
  178. /// <summary>
  179. /// Cursor left key.
  180. /// </summary>
  181. CursorLeft,
  182. /// <summary>
  183. /// Cursor right key.
  184. /// </summary>
  185. CursorRight,
  186. /// <summary>
  187. /// Page Up key.
  188. /// </summary>
  189. PageUp,
  190. /// <summary>
  191. /// Page Down key.
  192. /// </summary>
  193. PageDown,
  194. /// <summary>
  195. /// Home key
  196. /// </summary>
  197. Home,
  198. /// <summary>
  199. /// End key
  200. /// </summary>
  201. End,
  202. /// <summary>
  203. /// Delete character key
  204. /// </summary>
  205. DeleteChar,
  206. /// <summary>
  207. /// Insert character key
  208. /// </summary>
  209. InsertChar,
  210. /// <summary>
  211. /// F1 key.
  212. /// </summary>
  213. F1,
  214. /// <summary>
  215. /// F2 key.
  216. /// </summary>
  217. F2,
  218. /// <summary>
  219. /// F3 key.
  220. /// </summary>
  221. F3,
  222. /// <summary>
  223. /// F4 key.
  224. /// </summary>
  225. F4,
  226. /// <summary>
  227. /// F5 key.
  228. /// </summary>
  229. F5,
  230. /// <summary>
  231. /// F6 key.
  232. /// </summary>
  233. F6,
  234. /// <summary>
  235. /// F7 key.
  236. /// </summary>
  237. F7,
  238. /// <summary>
  239. /// F8 key.
  240. /// </summary>
  241. F8,
  242. /// <summary>
  243. /// F9 key.
  244. /// </summary>
  245. F9,
  246. /// <summary>
  247. /// F10 key.
  248. /// </summary>
  249. F10,
  250. /// <summary>
  251. /// Shift-tab key (backwards tab key).
  252. /// </summary>
  253. BackTab,
  254. /// <summary>
  255. /// A key with an unknown mapping was raised.
  256. /// </summary>
  257. Unknown
  258. }
  259. /// <summary>
  260. /// Describes a keyboard event.
  261. /// </summary>
  262. public struct KeyEvent {
  263. /// <summary>
  264. /// Symb olid definition for the key.
  265. /// </summary>
  266. public Key Key;
  267. /// <summary>
  268. /// The key value cast to an integer, you will typicall use this for
  269. /// extracting the Unicode rune value out of a key, when none of the
  270. /// symbolic options are in use.
  271. /// </summary>
  272. public int KeyValue => (int)Key;
  273. /// <summary>
  274. /// Gets a value indicating whether the Alt key was pressed (real or synthesized)
  275. /// </summary>
  276. /// <value><c>true</c> if is alternate; otherwise, <c>false</c>.</value>
  277. public bool IsAlt => (Key & Key.AltMask) != 0;
  278. /// <summary>
  279. /// Determines whether the value is a control key
  280. /// </summary>
  281. /// <value><c>true</c> if is ctrl; otherwise, <c>false</c>.</value>
  282. public bool IsCtrl => ((uint)Key >= 1) && ((uint)Key <= 26);
  283. /// <summary>
  284. /// Constructs a new KeyEvent from the provided Key value - can be a rune cast into a Key value
  285. /// </summary>
  286. public KeyEvent (Key k)
  287. {
  288. Key = k;
  289. }
  290. }
  291. /// <summary>
  292. /// Mouse flags reported in MouseEvent.
  293. /// </summary>
  294. /// <remarks>
  295. /// They just happen to map to the ncurses ones.
  296. /// </remarks>
  297. [Flags]
  298. public enum MouseFlags {
  299. /// <summary>
  300. /// The first mouse button was pressed.
  301. /// </summary>
  302. Button1Pressed = unchecked((int)0x2),
  303. /// <summary>
  304. /// The first mouse button was released.
  305. /// </summary>
  306. Button1Released = unchecked((int)0x1),
  307. /// <summary>
  308. /// The first mouse button was clicked (press+release).
  309. /// </summary>
  310. Button1Clicked = unchecked((int)0x4),
  311. /// <summary>
  312. /// The first mouse button was double-clicked.
  313. /// </summary>
  314. Button1DoubleClicked = unchecked((int)0x8),
  315. /// <summary>
  316. /// The first mouse button was tripple-clicked.
  317. /// </summary>
  318. Button1TripleClicked = unchecked((int)0x10),
  319. /// <summary>
  320. /// The second mouse button was pressed.
  321. /// </summary>
  322. Button2Pressed = unchecked((int)0x80),
  323. /// <summary>
  324. /// The second mouse button was released.
  325. /// </summary>
  326. Button2Released = unchecked((int)0x40),
  327. /// <summary>
  328. /// The second mouse button was clicked (press+release).
  329. /// </summary>
  330. Button2Clicked = unchecked((int)0x100),
  331. /// <summary>
  332. /// The second mouse button was double-clicked.
  333. /// </summary>
  334. Button2DoubleClicked = unchecked((int)0x200),
  335. /// <summary>
  336. /// The second mouse button was tripple-clicked.
  337. /// </summary>
  338. Button2TrippleClicked = unchecked((int)0x400),
  339. /// <summary>
  340. /// The third mouse button was pressed.
  341. /// </summary>
  342. Button3Pressed = unchecked((int)0x2000),
  343. /// <summary>
  344. /// The third mouse button was released.
  345. /// </summary>
  346. Button3Released = unchecked((int)0x1000),
  347. /// <summary>
  348. /// The third mouse button was clicked (press+release).
  349. /// </summary>
  350. Button3Clicked = unchecked((int)0x4000),
  351. /// <summary>
  352. /// The third mouse button was double-clicked.
  353. /// </summary>
  354. Button3DoubleClicked = unchecked((int)0x8000),
  355. /// <summary>
  356. /// The third mouse button was tripple-clicked.
  357. /// </summary>
  358. Button3TripleClicked = unchecked((int)0x10000),
  359. /// <summary>
  360. /// The fourth mouse button was pressed.
  361. /// </summary>
  362. Button4Pressed = unchecked((int)0x80000),
  363. /// <summary>
  364. /// The fourth mouse button was released.
  365. /// </summary>
  366. Button4Released = unchecked((int)0x40000),
  367. /// <summary>
  368. /// The fourth button was clicked (press+release).
  369. /// </summary>
  370. Button4Clicked = unchecked((int)0x100000),
  371. /// <summary>
  372. /// The fourth button was double-clicked.
  373. /// </summary>
  374. Button4DoubleClicked = unchecked((int)0x200000),
  375. /// <summary>
  376. /// The fourth button was tripple-clicked.
  377. /// </summary>
  378. Button4TripleClicked = unchecked((int)0x400000),
  379. /// <summary>
  380. /// The fourth button was pressed.
  381. /// </summary>
  382. ButtonShift = unchecked((int)0x2000000),
  383. /// <summary>
  384. /// Flag: the shift key was pressed when the mouse button took place.
  385. /// </summary>
  386. ButtonCtrl = unchecked((int)0x1000000),
  387. /// <summary>
  388. /// Flag: the alt key was pressed when the mouse button took place.
  389. /// </summary>
  390. ButtonAlt = unchecked((int)0x4000000),
  391. /// <summary>
  392. /// The mouse position is being reported in this event.
  393. /// </summary>
  394. ReportMousePosition = unchecked((int)0x8000000),
  395. /// <summary>
  396. /// Mask that captures all the events.
  397. /// </summary>
  398. AllEvents = unchecked((int)0x7ffffff),
  399. }
  400. /// <summary>
  401. /// Describes a mouse event
  402. /// </summary>
  403. public struct MouseEvent {
  404. /// <summary>
  405. /// The X (column) location for the mouse event.
  406. /// </summary>
  407. public int X;
  408. /// <summary>
  409. /// The Y (column) location for the mouse event.
  410. /// </summary>
  411. public int Y;
  412. /// <summary>
  413. /// Flags indicating the kind of mouse event that is being posted.
  414. /// </summary>
  415. public MouseFlags Flags;
  416. /// <summary>
  417. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:Terminal.Gui.MouseEvent"/>.
  418. /// </summary>
  419. /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:Terminal.Gui.MouseEvent"/>.</returns>
  420. public override string ToString()
  421. {
  422. return $"({X},{Y}:{Flags}";
  423. }
  424. }
  425. }