Event.cs 13 KB

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