Event.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /// When this value is set, the Key encodes the sequence Ctrl-KeyValue.
  172. /// And the actual value must be extracted by removing the CtrlMask.
  173. /// </summary>
  174. CtrlMask = 0x40000000,
  175. /// <summary>
  176. /// Backspace key.
  177. /// </summary>
  178. Backspace = 0x100000,
  179. /// <summary>
  180. /// Cursor up key
  181. /// </summary>
  182. CursorUp,
  183. /// <summary>
  184. /// Cursor down key.
  185. /// </summary>
  186. CursorDown,
  187. /// <summary>
  188. /// Cursor left key.
  189. /// </summary>
  190. CursorLeft,
  191. /// <summary>
  192. /// Cursor right key.
  193. /// </summary>
  194. CursorRight,
  195. /// <summary>
  196. /// Page Up key.
  197. /// </summary>
  198. PageUp,
  199. /// <summary>
  200. /// Page Down key.
  201. /// </summary>
  202. PageDown,
  203. /// <summary>
  204. /// Home key
  205. /// </summary>
  206. Home,
  207. /// <summary>
  208. /// End key
  209. /// </summary>
  210. End,
  211. /// <summary>
  212. /// Delete character key
  213. /// </summary>
  214. DeleteChar,
  215. /// <summary>
  216. /// Insert character key
  217. /// </summary>
  218. InsertChar,
  219. /// <summary>
  220. /// F1 key.
  221. /// </summary>
  222. F1,
  223. /// <summary>
  224. /// F2 key.
  225. /// </summary>
  226. F2,
  227. /// <summary>
  228. /// F3 key.
  229. /// </summary>
  230. F3,
  231. /// <summary>
  232. /// F4 key.
  233. /// </summary>
  234. F4,
  235. /// <summary>
  236. /// F5 key.
  237. /// </summary>
  238. F5,
  239. /// <summary>
  240. /// F6 key.
  241. /// </summary>
  242. F6,
  243. /// <summary>
  244. /// F7 key.
  245. /// </summary>
  246. F7,
  247. /// <summary>
  248. /// F8 key.
  249. /// </summary>
  250. F8,
  251. /// <summary>
  252. /// F9 key.
  253. /// </summary>
  254. F9,
  255. /// <summary>
  256. /// F10 key.
  257. /// </summary>
  258. F10,
  259. /// <summary>
  260. /// Shift-tab key (backwards tab key).
  261. /// </summary>
  262. BackTab,
  263. /// <summary>
  264. /// A key with an unknown mapping was raised.
  265. /// </summary>
  266. Unknown
  267. }
  268. /// <summary>
  269. /// Describes a keyboard event.
  270. /// </summary>
  271. public struct KeyEvent {
  272. /// <summary>
  273. /// Symb olid definition for the key.
  274. /// </summary>
  275. public Key Key;
  276. /// <summary>
  277. /// The key value cast to an integer, you will typicall use this for
  278. /// extracting the Unicode rune value out of a key, when none of the
  279. /// symbolic options are in use.
  280. /// </summary>
  281. public int KeyValue => (int)Key;
  282. /// <summary>
  283. /// Gets a value indicating whether the Alt key was pressed (real or synthesized)
  284. /// </summary>
  285. /// <value><c>true</c> if is alternate; otherwise, <c>false</c>.</value>
  286. public bool IsAlt => (Key & Key.AltMask) != 0;
  287. /// <summary>
  288. /// Determines whether the value is a control key (and NOT just the ctrl key)
  289. /// </summary>
  290. /// <value><c>true</c> if is ctrl; otherwise, <c>false</c>.</value>
  291. public bool IsCtrl => ((uint)Key >= 1) && ((uint)Key <= 26);
  292. /// <summary>
  293. /// Constructs a new KeyEvent from the provided Key value - can be a rune cast into a Key value
  294. /// </summary>
  295. public KeyEvent (Key k)
  296. {
  297. Key = k;
  298. }
  299. }
  300. /// <summary>
  301. /// Mouse flags reported in MouseEvent.
  302. /// </summary>
  303. /// <remarks>
  304. /// They just happen to map to the ncurses ones.
  305. /// </remarks>
  306. [Flags]
  307. public enum MouseFlags {
  308. /// <summary>
  309. /// The first mouse button was pressed.
  310. /// </summary>
  311. Button1Pressed = unchecked((int)0x2),
  312. /// <summary>
  313. /// The first mouse button was released.
  314. /// </summary>
  315. Button1Released = unchecked((int)0x1),
  316. /// <summary>
  317. /// The first mouse button was clicked (press+release).
  318. /// </summary>
  319. Button1Clicked = unchecked((int)0x4),
  320. /// <summary>
  321. /// The first mouse button was double-clicked.
  322. /// </summary>
  323. Button1DoubleClicked = unchecked((int)0x8),
  324. /// <summary>
  325. /// The first mouse button was triple-clicked.
  326. /// </summary>
  327. Button1TripleClicked = unchecked((int)0x10),
  328. /// <summary>
  329. /// The second mouse button was pressed.
  330. /// </summary>
  331. Button2Pressed = unchecked((int)0x80),
  332. /// <summary>
  333. /// The second mouse button was released.
  334. /// </summary>
  335. Button2Released = unchecked((int)0x40),
  336. /// <summary>
  337. /// The second mouse button was clicked (press+release).
  338. /// </summary>
  339. Button2Clicked = unchecked((int)0x100),
  340. /// <summary>
  341. /// The second mouse button was double-clicked.
  342. /// </summary>
  343. Button2DoubleClicked = unchecked((int)0x200),
  344. /// <summary>
  345. /// The second mouse button was triple-clicked.
  346. /// </summary>
  347. Button2TripleClicked = unchecked((int)0x400),
  348. /// <summary>
  349. /// The third mouse button was pressed.
  350. /// </summary>
  351. Button3Pressed = unchecked((int)0x2000),
  352. /// <summary>
  353. /// The third mouse button was released.
  354. /// </summary>
  355. Button3Released = unchecked((int)0x1000),
  356. /// <summary>
  357. /// The third mouse button was clicked (press+release).
  358. /// </summary>
  359. Button3Clicked = unchecked((int)0x4000),
  360. /// <summary>
  361. /// The third mouse button was double-clicked.
  362. /// </summary>
  363. Button3DoubleClicked = unchecked((int)0x8000),
  364. /// <summary>
  365. /// The third mouse button was triple-clicked.
  366. /// </summary>
  367. Button3TripleClicked = unchecked((int)0x10000),
  368. /// <summary>
  369. /// The fourth mouse button was pressed.
  370. /// </summary>
  371. Button4Pressed = unchecked((int)0x80000),
  372. /// <summary>
  373. /// The fourth mouse button was released.
  374. /// </summary>
  375. Button4Released = unchecked((int)0x40000),
  376. /// <summary>
  377. /// The fourth button was clicked (press+release).
  378. /// </summary>
  379. Button4Clicked = unchecked((int)0x100000),
  380. /// <summary>
  381. /// The fourth button was double-clicked.
  382. /// </summary>
  383. Button4DoubleClicked = unchecked((int)0x200000),
  384. /// <summary>
  385. /// The fourth button was triple-clicked.
  386. /// </summary>
  387. Button4TripleClicked = unchecked((int)0x400000),
  388. /// <summary>
  389. /// Flag: the shift key was pressed when the mouse button took place.
  390. /// </summary>
  391. ButtonShift = unchecked((int)0x2000000),
  392. /// <summary>
  393. /// Flag: the ctrl key was pressed when the mouse button took place.
  394. /// </summary>
  395. ButtonCtrl = unchecked((int)0x1000000),
  396. /// <summary>
  397. /// Flag: the alt key was pressed when the mouse button took place.
  398. /// </summary>
  399. ButtonAlt = unchecked((int)0x4000000),
  400. /// <summary>
  401. /// The mouse position is being reported in this event.
  402. /// </summary>
  403. ReportMousePosition = unchecked((int)0x8000000),
  404. /// <summary>
  405. /// Vertical button wheeled up.
  406. /// </summary>
  407. WheeledUp = unchecked((int)0x10000000),
  408. /// <summary>
  409. /// Vertical button wheeled up.
  410. /// </summary>
  411. WheeledDown = unchecked((int)0x20000000),
  412. /// <summary>
  413. /// Mask that captures all the events.
  414. /// </summary>
  415. AllEvents = unchecked((int)0x7ffffff),
  416. }
  417. /// <summary>
  418. /// Describes a mouse event
  419. /// </summary>
  420. public struct MouseEvent {
  421. /// <summary>
  422. /// The X (column) location for the mouse event.
  423. /// </summary>
  424. public int X;
  425. /// <summary>
  426. /// The Y (column) location for the mouse event.
  427. /// </summary>
  428. public int Y;
  429. /// <summary>
  430. /// Flags indicating the kind of mouse event that is being posted.
  431. /// </summary>
  432. public MouseFlags Flags;
  433. /// <summary>
  434. /// The offset X (column) location for the mouse event.
  435. /// </summary>
  436. public int OfX;
  437. /// <summary>
  438. /// The offset Y (column) location for the mouse event.
  439. /// </summary>
  440. public int OfY;
  441. /// <summary>
  442. /// The current view at the location for the mouse event.
  443. /// </summary>
  444. public View View;
  445. /// <summary>
  446. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:Terminal.Gui.MouseEvent"/>.
  447. /// </summary>
  448. /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:Terminal.Gui.MouseEvent"/>.</returns>
  449. public override string ToString()
  450. {
  451. return $"({X},{Y}:{Flags}";
  452. }
  453. }
  454. }