Event.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. /// Identifies the state of the "shift"-keys within a event.
  11. /// </summary>
  12. public class KeyModifiers {
  13. /// <summary>
  14. /// Check if the Shift key was pressed or not.
  15. /// </summary>
  16. public bool Shift;
  17. /// <summary>
  18. /// Check if the Alt key was pressed or not.
  19. /// </summary>
  20. public bool Alt;
  21. /// <summary>
  22. /// Check if the Ctrl key was pressed or not.
  23. /// </summary>
  24. public bool Ctrl;
  25. /// <summary>
  26. /// Check if the Caps lock key was pressed or not.
  27. /// </summary>
  28. public bool Capslock;
  29. /// <summary>
  30. /// Check if the Num lock key was pressed or not.
  31. /// </summary>
  32. public bool Numlock;
  33. /// <summary>
  34. /// Check if the Scroll lock key was pressed or not.
  35. /// </summary>
  36. public bool Scrolllock;
  37. }
  38. /// <summary>
  39. /// The <see cref="Key"/> enumeration contains special encoding for some keys, but can also
  40. /// encode all the unicode values that can be passed.
  41. /// </summary>
  42. /// <remarks>
  43. /// <para>
  44. /// If the <see cref="SpecialMask"/> is set, then the value is that of the special mask,
  45. /// otherwise, the value is the one of the lower bits (as extracted by <see cref="CharMask"/>)
  46. /// <para>
  47. /// Numerics keys are the values between 48 and 57 corresponding to 0 to 9
  48. /// </para>
  49. /// </para>
  50. /// <para>
  51. /// Upper alpha keys are the values between 65 and 90 corresponding to A to Z
  52. /// </para>
  53. /// <para>
  54. /// Unicode runes are also stored here, the letter 'A" for example is encoded as a value 65 (not surfaced in the enum).
  55. /// </para>
  56. /// </remarks>
  57. [Flags]
  58. public enum Key : uint {
  59. /// <summary>
  60. /// Mask that indicates that this is a character value, values outside this range
  61. /// indicate special characters like Alt-key combinations or special keys on the
  62. /// keyboard like function keys, arrows keys and so on.
  63. /// </summary>
  64. CharMask = 0xfffff,
  65. /// <summary>
  66. /// If the <see cref="SpecialMask"/> is set, then the value is that of the special mask,
  67. /// otherwise, the value is the one of the lower bits (as extracted by <see cref="CharMask"/>).
  68. /// </summary>
  69. SpecialMask = 0xfff00000,
  70. /// <summary>
  71. /// The key code representing null or empty
  72. /// </summary>
  73. Null = 0,
  74. /// <summary>
  75. /// The key code for the user pressing the return key.
  76. /// </summary>
  77. Enter = '\n',
  78. /// <summary>
  79. /// The key code for the user pressing the escape key
  80. /// </summary>
  81. Esc = 27,
  82. /// <summary>
  83. /// The key code for the user pressing the space bar
  84. /// </summary>
  85. Space = 32,
  86. /// <summary>
  87. /// Digit 0.
  88. /// </summary>
  89. D0 = 48,
  90. /// <summary>
  91. /// Digit 1.
  92. /// </summary>
  93. D1,
  94. /// <summary>
  95. /// Digit 2.
  96. /// </summary>
  97. D2,
  98. /// <summary>
  99. /// Digit 3.
  100. /// </summary>
  101. D3,
  102. /// <summary>
  103. /// Digit 4.
  104. /// </summary>
  105. D4,
  106. /// <summary>
  107. /// Digit 5.
  108. /// </summary>
  109. D5,
  110. /// <summary>
  111. /// Digit 6.
  112. /// </summary>
  113. D6,
  114. /// <summary>
  115. /// Digit 7.
  116. /// </summary>
  117. D7,
  118. /// <summary>
  119. /// Digit 8.
  120. /// </summary>
  121. D8,
  122. /// <summary>
  123. /// Digit 9.
  124. /// </summary>
  125. D9,
  126. /// <summary>
  127. /// The key code for the user pressing Shift-A
  128. /// </summary>
  129. A = 65,
  130. /// <summary>
  131. /// The key code for the user pressing Shift-B
  132. /// </summary>
  133. B,
  134. /// <summary>
  135. /// The key code for the user pressing Shift-C
  136. /// </summary>
  137. C,
  138. /// <summary>
  139. /// The key code for the user pressing Shift-D
  140. /// </summary>
  141. D,
  142. /// <summary>
  143. /// The key code for the user pressing Shift-E
  144. /// </summary>
  145. E,
  146. /// <summary>
  147. /// The key code for the user pressing Shift-F
  148. /// </summary>
  149. F,
  150. /// <summary>
  151. /// The key code for the user pressing Shift-G
  152. /// </summary>
  153. G,
  154. /// <summary>
  155. /// The key code for the user pressing Shift-H
  156. /// </summary>
  157. H,
  158. /// <summary>
  159. /// The key code for the user pressing Shift-I
  160. /// </summary>
  161. I,
  162. /// <summary>
  163. /// The key code for the user pressing Shift-J
  164. /// </summary>
  165. J,
  166. /// <summary>
  167. /// The key code for the user pressing Shift-K
  168. /// </summary>
  169. K,
  170. /// <summary>
  171. /// The key code for the user pressing Shift-L
  172. /// </summary>
  173. L,
  174. /// <summary>
  175. /// The key code for the user pressing Shift-M
  176. /// </summary>
  177. M,
  178. /// <summary>
  179. /// The key code for the user pressing Shift-N
  180. /// </summary>
  181. N,
  182. /// <summary>
  183. /// The key code for the user pressing Shift-O
  184. /// </summary>
  185. O,
  186. /// <summary>
  187. /// The key code for the user pressing Shift-P
  188. /// </summary>
  189. P,
  190. /// <summary>
  191. /// The key code for the user pressing Shift-Q
  192. /// </summary>
  193. Q,
  194. /// <summary>
  195. /// The key code for the user pressing Shift-R
  196. /// </summary>
  197. R,
  198. /// <summary>
  199. /// The key code for the user pressing Shift-S
  200. /// </summary>
  201. S,
  202. /// <summary>
  203. /// The key code for the user pressing Shift-T
  204. /// </summary>
  205. T,
  206. /// <summary>
  207. /// The key code for the user pressing Shift-U
  208. /// </summary>
  209. U,
  210. /// <summary>
  211. /// The key code for the user pressing Shift-V
  212. /// </summary>
  213. V,
  214. /// <summary>
  215. /// The key code for the user pressing Shift-W
  216. /// </summary>
  217. W,
  218. /// <summary>
  219. /// The key code for the user pressing Shift-X
  220. /// </summary>
  221. X,
  222. /// <summary>
  223. /// The key code for the user pressing Shift-Y
  224. /// </summary>
  225. Y,
  226. /// <summary>
  227. /// The key code for the user pressing Shift-Z
  228. /// </summary>
  229. Z,
  230. /// <summary>
  231. /// The key code for the user pressing the delete key.
  232. /// </summary>
  233. Delete = 127,
  234. /// <summary>
  235. /// When this value is set, the Key encodes the sequence Shift-KeyValue.
  236. /// </summary>
  237. ShiftMask = 0x10000000,
  238. /// <summary>
  239. /// When this value is set, the Key encodes the sequence Alt-KeyValue.
  240. /// And the actual value must be extracted by removing the AltMask.
  241. /// </summary>
  242. AltMask = 0x80000000,
  243. /// <summary>
  244. /// When this value is set, the Key encodes the sequence Ctrl-KeyValue.
  245. /// And the actual value must be extracted by removing the CtrlMask.
  246. /// </summary>
  247. CtrlMask = 0x40000000,
  248. /// <summary>
  249. /// Backspace key.
  250. /// </summary>
  251. Backspace = 0x100000,
  252. /// <summary>
  253. /// Cursor up key
  254. /// </summary>
  255. CursorUp,
  256. /// <summary>
  257. /// Cursor down key.
  258. /// </summary>
  259. CursorDown,
  260. /// <summary>
  261. /// Cursor left key.
  262. /// </summary>
  263. CursorLeft,
  264. /// <summary>
  265. /// Cursor right key.
  266. /// </summary>
  267. CursorRight,
  268. /// <summary>
  269. /// Page Up key.
  270. /// </summary>
  271. PageUp,
  272. /// <summary>
  273. /// Page Down key.
  274. /// </summary>
  275. PageDown,
  276. /// <summary>
  277. /// Home key
  278. /// </summary>
  279. Home,
  280. /// <summary>
  281. /// End key
  282. /// </summary>
  283. End,
  284. /// <summary>
  285. /// Delete character key
  286. /// </summary>
  287. DeleteChar,
  288. /// <summary>
  289. /// Insert character key
  290. /// </summary>
  291. InsertChar,
  292. /// <summary>
  293. /// F1 key.
  294. /// </summary>
  295. F1,
  296. /// <summary>
  297. /// F2 key.
  298. /// </summary>
  299. F2,
  300. /// <summary>
  301. /// F3 key.
  302. /// </summary>
  303. F3,
  304. /// <summary>
  305. /// F4 key.
  306. /// </summary>
  307. F4,
  308. /// <summary>
  309. /// F5 key.
  310. /// </summary>
  311. F5,
  312. /// <summary>
  313. /// F6 key.
  314. /// </summary>
  315. F6,
  316. /// <summary>
  317. /// F7 key.
  318. /// </summary>
  319. F7,
  320. /// <summary>
  321. /// F8 key.
  322. /// </summary>
  323. F8,
  324. /// <summary>
  325. /// F9 key.
  326. /// </summary>
  327. F9,
  328. /// <summary>
  329. /// F10 key.
  330. /// </summary>
  331. F10,
  332. /// <summary>
  333. /// F11 key.
  334. /// </summary>
  335. F11,
  336. /// <summary>
  337. /// F12 key.
  338. /// </summary>
  339. F12,
  340. /// <summary>
  341. /// The key code for the user pressing the tab key (forwards tab key).
  342. /// </summary>
  343. Tab,
  344. /// <summary>
  345. /// Shift-tab key (backwards tab key).
  346. /// </summary>
  347. BackTab,
  348. /// <summary>
  349. /// A key with an unknown mapping was raised.
  350. /// </summary>
  351. Unknown
  352. }
  353. /// <summary>
  354. /// Describes a keyboard event.
  355. /// </summary>
  356. public class KeyEvent {
  357. KeyModifiers keyModifiers;
  358. /// <summary>
  359. /// Symb olid definition for the key.
  360. /// </summary>
  361. public Key Key;
  362. /// <summary>
  363. /// The key value cast to an integer, you will typical use this for
  364. /// extracting the Unicode rune value out of a key, when none of the
  365. /// symbolic options are in use.
  366. /// </summary>
  367. public int KeyValue => (int)Key;
  368. /// <summary>
  369. /// Gets a value indicating whether the Shift key was pressed.
  370. /// </summary>
  371. /// <value><c>true</c> if is shift; otherwise, <c>false</c>.</value>
  372. public bool IsShift => keyModifiers.Shift;
  373. /// <summary>
  374. /// Gets a value indicating whether the Alt key was pressed (real or synthesized)
  375. /// </summary>
  376. /// <value><c>true</c> if is alternate; otherwise, <c>false</c>.</value>
  377. public bool IsAlt => keyModifiers.Alt;
  378. /// <summary>
  379. /// Determines whether the value is a control key (and NOT just the ctrl key)
  380. /// </summary>
  381. /// <value><c>true</c> if is ctrl; otherwise, <c>false</c>.</value>
  382. //public bool IsCtrl => ((uint)Key >= 1) && ((uint)Key <= 26);
  383. public bool IsCtrl => keyModifiers.Ctrl;
  384. /// <summary>
  385. /// Gets a value indicating whether the Caps lock key was pressed (real or synthesized)
  386. /// </summary>
  387. /// <value><c>true</c> if is alternate; otherwise, <c>false</c>.</value>
  388. public bool IsCapslock => keyModifiers.Capslock;
  389. /// <summary>
  390. /// Gets a value indicating whether the Num lock key was pressed (real or synthesized)
  391. /// </summary>
  392. /// <value><c>true</c> if is alternate; otherwise, <c>false</c>.</value>
  393. public bool IsNumlock => keyModifiers.Numlock;
  394. /// <summary>
  395. /// Gets a value indicating whether the Scroll lock key was pressed (real or synthesized)
  396. /// </summary>
  397. /// <value><c>true</c> if is alternate; otherwise, <c>false</c>.</value>
  398. public bool IsScrolllock => keyModifiers.Scrolllock;
  399. /// <summary>
  400. /// Constructs a new <see cref="KeyEvent"/>
  401. /// </summary>
  402. public KeyEvent ()
  403. {
  404. Key = Key.Unknown;
  405. keyModifiers = new KeyModifiers ();
  406. }
  407. /// <summary>
  408. /// Constructs a new <see cref="KeyEvent"/> from the provided Key value - can be a rune cast into a Key value
  409. /// </summary>
  410. public KeyEvent (Key k, KeyModifiers km)
  411. {
  412. Key = k;
  413. keyModifiers = km;
  414. }
  415. /// <summary>
  416. /// Pretty prints the KeyEvent
  417. /// </summary>
  418. /// <returns></returns>
  419. public override string ToString ()
  420. {
  421. string msg = "";
  422. var key = this.Key;
  423. if (keyModifiers.Shift) {
  424. msg += "Shift-";
  425. }
  426. if (keyModifiers.Alt) {
  427. msg += "Alt-";
  428. }
  429. if (keyModifiers.Ctrl) {
  430. msg += "Ctrl-";
  431. }
  432. if (keyModifiers.Capslock) {
  433. msg += "Capslock-";
  434. }
  435. if (keyModifiers.Numlock) {
  436. msg += "Numlock-";
  437. }
  438. if (keyModifiers.Scrolllock) {
  439. msg += "Scrolllock-";
  440. }
  441. msg += $"{(((uint)this.KeyValue & (uint)Key.CharMask) > 27 ? $"{(char)this.KeyValue}" : $"{key}")}";
  442. return msg;
  443. }
  444. }
  445. /// <summary>
  446. /// Mouse flags reported in <see cref="MouseEvent"/>.
  447. /// </summary>
  448. /// <remarks>
  449. /// They just happen to map to the ncurses ones.
  450. /// </remarks>
  451. [Flags]
  452. public enum MouseFlags {
  453. /// <summary>
  454. /// The first mouse button was pressed.
  455. /// </summary>
  456. Button1Pressed = unchecked((int)0x2),
  457. /// <summary>
  458. /// The first mouse button was released.
  459. /// </summary>
  460. Button1Released = unchecked((int)0x1),
  461. /// <summary>
  462. /// The first mouse button was clicked (press+release).
  463. /// </summary>
  464. Button1Clicked = unchecked((int)0x4),
  465. /// <summary>
  466. /// The first mouse button was double-clicked.
  467. /// </summary>
  468. Button1DoubleClicked = unchecked((int)0x8),
  469. /// <summary>
  470. /// The first mouse button was triple-clicked.
  471. /// </summary>
  472. Button1TripleClicked = unchecked((int)0x10),
  473. /// <summary>
  474. /// The second mouse button was pressed.
  475. /// </summary>
  476. Button2Pressed = unchecked((int)0x80),
  477. /// <summary>
  478. /// The second mouse button was released.
  479. /// </summary>
  480. Button2Released = unchecked((int)0x40),
  481. /// <summary>
  482. /// The second mouse button was clicked (press+release).
  483. /// </summary>
  484. Button2Clicked = unchecked((int)0x100),
  485. /// <summary>
  486. /// The second mouse button was double-clicked.
  487. /// </summary>
  488. Button2DoubleClicked = unchecked((int)0x200),
  489. /// <summary>
  490. /// The second mouse button was triple-clicked.
  491. /// </summary>
  492. Button2TripleClicked = unchecked((int)0x400),
  493. /// <summary>
  494. /// The third mouse button was pressed.
  495. /// </summary>
  496. Button3Pressed = unchecked((int)0x2000),
  497. /// <summary>
  498. /// The third mouse button was released.
  499. /// </summary>
  500. Button3Released = unchecked((int)0x1000),
  501. /// <summary>
  502. /// The third mouse button was clicked (press+release).
  503. /// </summary>
  504. Button3Clicked = unchecked((int)0x4000),
  505. /// <summary>
  506. /// The third mouse button was double-clicked.
  507. /// </summary>
  508. Button3DoubleClicked = unchecked((int)0x8000),
  509. /// <summary>
  510. /// The third mouse button was triple-clicked.
  511. /// </summary>
  512. Button3TripleClicked = unchecked((int)0x10000),
  513. /// <summary>
  514. /// The fourth mouse button was pressed.
  515. /// </summary>
  516. Button4Pressed = unchecked((int)0x80000),
  517. /// <summary>
  518. /// The fourth mouse button was released.
  519. /// </summary>
  520. Button4Released = unchecked((int)0x40000),
  521. /// <summary>
  522. /// The fourth button was clicked (press+release).
  523. /// </summary>
  524. Button4Clicked = unchecked((int)0x100000),
  525. /// <summary>
  526. /// The fourth button was double-clicked.
  527. /// </summary>
  528. Button4DoubleClicked = unchecked((int)0x200000),
  529. /// <summary>
  530. /// The fourth button was triple-clicked.
  531. /// </summary>
  532. Button4TripleClicked = unchecked((int)0x400000),
  533. /// <summary>
  534. /// Flag: the shift key was pressed when the mouse button took place.
  535. /// </summary>
  536. ButtonShift = unchecked((int)0x2000000),
  537. /// <summary>
  538. /// Flag: the ctrl key was pressed when the mouse button took place.
  539. /// </summary>
  540. ButtonCtrl = unchecked((int)0x1000000),
  541. /// <summary>
  542. /// Flag: the alt key was pressed when the mouse button took place.
  543. /// </summary>
  544. ButtonAlt = unchecked((int)0x4000000),
  545. /// <summary>
  546. /// The mouse position is being reported in this event.
  547. /// </summary>
  548. ReportMousePosition = unchecked((int)0x8000000),
  549. /// <summary>
  550. /// Vertical button wheeled up.
  551. /// </summary>
  552. WheeledUp = unchecked((int)0x10000000),
  553. /// <summary>
  554. /// Vertical button wheeled up.
  555. /// </summary>
  556. WheeledDown = unchecked((int)0x20000000),
  557. /// <summary>
  558. /// Vertical button wheeled up while pressing ButtonShift.
  559. /// </summary>
  560. WheeledLeft = ButtonShift | WheeledUp,
  561. /// <summary>
  562. /// Vertical button wheeled down while pressing ButtonShift.
  563. /// </summary>
  564. WheeledRight = ButtonShift | WheeledDown,
  565. /// <summary>
  566. /// Mask that captures all the events.
  567. /// </summary>
  568. AllEvents = unchecked((int)0x7ffffff),
  569. }
  570. /// <summary>
  571. /// Describes a mouse event
  572. /// </summary>
  573. public struct MouseEvent {
  574. /// <summary>
  575. /// The X (column) location for the mouse event.
  576. /// </summary>
  577. public int X;
  578. /// <summary>
  579. /// The Y (column) location for the mouse event.
  580. /// </summary>
  581. public int Y;
  582. /// <summary>
  583. /// Flags indicating the kind of mouse event that is being posted.
  584. /// </summary>
  585. public MouseFlags Flags;
  586. /// <summary>
  587. /// The offset X (column) location for the mouse event.
  588. /// </summary>
  589. public int OfX;
  590. /// <summary>
  591. /// The offset Y (column) location for the mouse event.
  592. /// </summary>
  593. public int OfY;
  594. /// <summary>
  595. /// The current view at the location for the mouse event.
  596. /// </summary>
  597. public View View;
  598. /// <summary>
  599. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="MouseEvent"/>.
  600. /// </summary>
  601. /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="MouseEvent"/>.</returns>
  602. public override string ToString ()
  603. {
  604. return $"({X},{Y}:{Flags}";
  605. }
  606. }
  607. }