Event.cs 12 KB

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