Event.cs 11 KB

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