EscSeqUtils.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Runtime.InteropServices;
  7. using System.Threading.Tasks;
  8. namespace Terminal.Gui;
  9. /// <summary>
  10. /// Provides a platform-independent API for managing ANSI escape sequences.
  11. /// </summary>
  12. /// <remarks>
  13. /// Useful resources:
  14. /// * https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
  15. /// * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
  16. /// * https://vt100.net/
  17. /// </remarks>
  18. public static class EscSeqUtils {
  19. /// <summary>
  20. /// Escape key code (ASCII 27/0x1B).
  21. /// </summary>
  22. public static readonly char KeyEsc = (char)Key.Esc;
  23. /// <summary>
  24. /// ESC [ - The CSI (Control Sequence Introducer).
  25. /// </summary>
  26. public static readonly string CSI = $"{KeyEsc}[";
  27. /// <summary>
  28. /// ESC [ ? 1003 h - Enable mouse event tracking.
  29. /// </summary>
  30. public static readonly string CSI_EnableAnyEventMouse = CSI + "?1003h";
  31. /// <summary>
  32. /// ESC [ ? 1006 h - Enable SGR (Select Graphic Rendition).
  33. /// </summary>
  34. public static readonly string CSI_EnableSgrExtModeMouse = CSI + "?1006h";
  35. /// <summary>
  36. /// ESC [ ? 1015 h - Enable URXVT (Unicode Extended Virtual Terminal).
  37. /// </summary>
  38. public static readonly string CSI_EnableUrxvtExtModeMouse = CSI + "?1015h";
  39. /// <summary>
  40. /// ESC [ ? 1003 l - Disable any mouse event tracking.
  41. /// </summary>
  42. public static readonly string CSI_DisableAnyEventMouse = CSI + "?1003l";
  43. /// <summary>
  44. /// ESC [ ? 1006 l - Disable SGR (Select Graphic Rendition).
  45. /// </summary>
  46. public static readonly string CSI_DisableSgrExtModeMouse = CSI + "?1006l";
  47. /// <summary>
  48. /// ESC [ ? 1015 l - Disable URXVT (Unicode Extended Virtual Terminal).
  49. /// </summary>
  50. public static readonly string CSI_DisableUrxvtExtModeMouse = CSI + "?1015l";
  51. /// <summary>
  52. /// Control sequence for enabling mouse events.
  53. /// </summary>
  54. public static string CSI_EnableMouseEvents { get; set; } =
  55. CSI_EnableAnyEventMouse + CSI_EnableUrxvtExtModeMouse + CSI_EnableSgrExtModeMouse;
  56. /// <summary>
  57. /// Control sequence for disabling mouse events.
  58. /// </summary>
  59. public static string CSI_DisableMouseEvents { get; set; } =
  60. CSI_DisableAnyEventMouse + CSI_DisableUrxvtExtModeMouse + CSI_DisableSgrExtModeMouse;
  61. /// <summary>
  62. /// ESC [ ? 1047 h - Activate xterm alternative buffer (no backscroll)
  63. /// </summary>
  64. public static readonly string CSI_ActivateAltBufferNoBackscroll = CSI + "?1047h";
  65. /// <summary>
  66. /// ESC [ ? 1047 l - Restore xterm working buffer (with backscroll)
  67. /// </summary>
  68. public static readonly string CSI_RestoreAltBufferWithBackscroll = CSI + "?1047l";
  69. /// <summary>
  70. /// ESC [ ? 1049 h - Save cursor position and activate xterm alternative buffer (no backscroll)
  71. /// </summary>
  72. public static readonly string CSI_SaveCursorAndActivateAltBufferNoBackscroll = CSI + "?1049h";
  73. /// <summary>
  74. /// ESC [ ? 1049 l - Restore cursor position and restore xterm working buffer (with backscroll)
  75. /// </summary>
  76. public static readonly string CSI_RestoreCursorAndActivateAltBufferWithBackscroll = CSI + "?1049l";
  77. /// <summary>
  78. /// Options for ANSI ESC "[xJ" - Clears part of the screen.
  79. /// </summary>
  80. public enum ClearScreenOptions {
  81. /// <summary>
  82. /// If n is 0 (or missing), clear from cursor to end of screen.
  83. /// </summary>
  84. CursorToEndOfScreen = 0,
  85. /// <summary>
  86. /// If n is 1, clear from cursor to beginning of the screen.
  87. /// </summary>
  88. CursorToBeginningOfScreen = 1,
  89. /// <summary>
  90. /// If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).
  91. /// </summary>
  92. EntireScreen = 2,
  93. /// <summary>
  94. /// If n is 3, clear entire screen and delete all lines saved in the scrollback buffer
  95. /// </summary>
  96. EntireScreenAndScrollbackBuffer = 3
  97. }
  98. /// <summary>
  99. /// ESC [ x J - Clears part of the screen. See <see cref="ClearScreenOptions"/>.
  100. /// </summary>
  101. /// <param name="option"></param>
  102. /// <returns></returns>
  103. public static string CSI_ClearScreen (ClearScreenOptions option) => $"{CSI}{(int)option}J";
  104. #region Cursor
  105. //ESC [ M - RI Reverse Index – Performs the reverse operation of \n, moves cursor up one line, maintains horizontal position, scrolls buffer if necessary*
  106. /// <summary>
  107. /// ESC [ 7 - Save Cursor Position in Memory**
  108. /// </summary>
  109. public static readonly string CSI_SaveCursorPosition = CSI + "7";
  110. /// <summary>
  111. /// ESC [ 8 - DECSR Restore Cursor Position from Memory**
  112. /// </summary>
  113. public static readonly string CSI_RestoreCursorPosition = CSI + "8";
  114. /// <summary>
  115. /// ESC [ 8 ; height ; width t - Set Terminal Window Size
  116. /// https://terminalguide.namepad.de/seq/csi_st-8/
  117. /// </summary>
  118. public static string CSI_SetTerminalWindowSize (int height, int width) => $"{CSI}8;{height};{width}t";
  119. //ESC [ < n > A - CUU - Cursor Up Cursor up by < n >
  120. //ESC [ < n > B - CUD - Cursor Down Cursor down by < n >
  121. //ESC [ < n > C - CUF - Cursor Forward Cursor forward (Right) by < n >
  122. //ESC [ < n > D - CUB - Cursor Backward Cursor backward (Left) by < n >
  123. //ESC [ < n > E - CNL - Cursor Next Line - Cursor down < n > lines from current position
  124. //ESC [ < n > F - CPL - Cursor Previous Line Cursor up < n > lines from current position
  125. //ESC [ < n > G - CHA - Cursor Horizontal Absolute Cursor moves to < n > th position horizontally in the current line
  126. //ESC [ < n > d - VPA - Vertical Line Position Absolute Cursor moves to the < n > th position vertically in the current column
  127. /// <summary>
  128. /// ESC [ y ; x H - CUP Cursor Position - Cursor moves to x ; y coordinate within the viewport, where x is the column of the y line
  129. /// </summary>
  130. /// <param name="x"></param>
  131. /// <param name="y"></param>
  132. /// <returns></returns>
  133. public static string CSI_SetCursorPosition (int x, int y) => $"{CSI}{y};{x}H";
  134. //ESC [ <y> ; <x> f - HVP Horizontal Vertical Position* Cursor moves to<x>; <y> coordinate within the viewport, where <x> is the column of the<y> line
  135. //ESC [ s - ANSISYSSC Save Cursor – Ansi.sys emulation **With no parameters, performs a save cursor operation like DECSC
  136. //ESC [ u - ANSISYSRC Restore Cursor – Ansi.sys emulation **With no parameters, performs a restore cursor operation like DECRC
  137. //ESC [ ? 12 h - ATT160 Text Cursor Enable Blinking Start the cursor blinking
  138. //ESC [ ? 12 l - ATT160 Text Cursor Disable Blinking Stop blinking the cursor
  139. /// <summary>
  140. /// ESC [ ? 25 h - DECTCEM Text Cursor Enable Mode Show Show the cursor
  141. /// </summary>
  142. public static readonly string CSI_ShowCursor = CSI + "?25h";
  143. /// <summary>
  144. /// ESC [ ? 25 l - DECTCEM Text Cursor Enable Mode Hide Hide the cursor
  145. /// </summary>
  146. public static readonly string CSI_HideCursor = CSI + "?25l";
  147. //ESC [ ? 12 h - ATT160 Text Cursor Enable Blinking Start the cursor blinking
  148. //ESC [ ? 12 l - ATT160 Text Cursor Disable Blinking Stop blinking the cursor
  149. //ESC [ ? 25 h - DECTCEM Text Cursor Enable Mode Show Show the cursor
  150. //ESC [ ? 25 l - DECTCEM Text Cursor Enable Mode Hide Hide the cursor
  151. /// <summary>
  152. /// Styles for ANSI ESC "[x q" - Set Cursor Style
  153. /// </summary>
  154. public enum DECSCUSR_Style {
  155. /// <summary>
  156. /// DECSCUSR - User Shape - Default cursor shape configured by the user
  157. /// </summary>
  158. UserShape = 0,
  159. /// <summary>
  160. /// DECSCUSR - Blinking Block - Blinking block cursor shape
  161. /// </summary>
  162. BlinkingBlock = 1,
  163. /// <summary>
  164. /// DECSCUSR - Steady Block - Steady block cursor shape
  165. /// </summary>
  166. SteadyBlock = 2,
  167. /// <summary>
  168. /// DECSCUSR - Blinking Underline - Blinking underline cursor shape
  169. /// </summary>
  170. BlinkingUnderline = 3,
  171. /// <summary>
  172. /// DECSCUSR - Steady Underline - Steady underline cursor shape
  173. /// </summary>
  174. SteadyUnderline = 4,
  175. /// <summary>
  176. /// DECSCUSR - Blinking Bar - Blinking bar cursor shape
  177. /// </summary>
  178. BlinkingBar = 5,
  179. /// <summary>
  180. /// DECSCUSR - Steady Bar - Steady bar cursor shape
  181. /// </summary>
  182. SteadyBar = 6
  183. }
  184. /// <summary>
  185. /// ESC [ n SP q - Select Cursor Style (DECSCUSR)
  186. /// https://terminalguide.namepad.de/seq/csi_sq_t_space/
  187. /// </summary>
  188. /// <param name="style"></param>
  189. /// <returns></returns>
  190. public static string CSI_SetCursorStyle (DECSCUSR_Style style) => $"{CSI}{(int)style} q";
  191. #endregion
  192. #region Colors
  193. /// <summary>
  194. /// ESC [ (n) m - SGR - Set Graphics Rendition - Set the format of the screen and text as specified by (n)
  195. /// This command is special in that the (n) position can accept between 0 and 16 parameters separated by semicolons.
  196. /// When no parameters are specified, it is treated the same as a single 0 parameter.
  197. /// https://terminalguide.namepad.de/seq/csi_sm/
  198. /// </summary>
  199. public static string CSI_SetGraphicsRendition (params int [] parameters) => $"{CSI}{string.Join (";", parameters)}m";
  200. /// <summary>
  201. /// ESC[38;5;{id}m - Set foreground color (256 colors)
  202. /// </summary>
  203. public static string CSI_SetForegroundColor (int id) => $"{CSI}38;5;{id}m";
  204. /// <summary>
  205. /// ESC[48;5;{id}m - Set background color (256 colors)
  206. /// </summary>
  207. public static string CSI_SetBackgroundColor (int id) => $"{CSI}48;5;{id}m";
  208. /// <summary>
  209. /// ESC[38;2;{r};{g};{b}m Set foreground color as RGB.
  210. /// </summary>
  211. public static string CSI_SetForegroundColorRGB (int r, int g, int b) => $"{CSI}38;2;{r};{g};{b}m";
  212. /// <summary>
  213. /// ESC[48;2;{r};{g};{b}m Set background color as RGB.
  214. /// </summary>
  215. public static string CSI_SetBackgroundColorRGB (int r, int g, int b) => $"{CSI}48;2;{r};{g};{b}m";
  216. #endregion
  217. #region Requests
  218. /// <summary>
  219. /// ESC [ ? 6 n - Request Cursor Position Report (?) (DECXCPR)
  220. /// https://terminalguide.namepad.de/seq/csi_sn__p-6/
  221. /// </summary>
  222. public static readonly string CSI_RequestCursorPositionReport = CSI + "?6n";
  223. /// <summary>
  224. /// The terminal reply to <see cref="CSI_RequestCursorPositionReport"/>. ESC [ ? (y) ; (x) R
  225. /// </summary>
  226. public const string CSI_RequestCursorPositionReport_Terminator = "R";
  227. /// <summary>
  228. /// ESC [ 0 c - Send Device Attributes (Primary DA)
  229. ///
  230. /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Application-Program-Command-functions
  231. /// https://www.xfree86.org/current/ctlseqs.html
  232. /// Windows Terminal v1.17 and below emits “\x1b[?1;0c”, indicating "VT101 with No Options".
  233. /// Windows Terminal v1.18+ emits: \x1b[?61;6;7;22;23;24;28;32;42c"
  234. /// See https://github.com/microsoft/terminal/pull/14906
  235. ///
  236. /// 61 - The device conforms to level 1 of the character cell display architecture
  237. /// (See https://github.com/microsoft/terminal/issues/15693#issuecomment-1633304497)
  238. /// 6 = Selective erase
  239. /// 7 = Soft fonts
  240. /// 22 = Color text
  241. /// 23 = Greek character sets
  242. /// 24 = Turkish character sets
  243. /// 28 = Rectangular area operations
  244. /// 32 = Text macros
  245. /// 42 = ISO Latin-2 character set
  246. ///
  247. /// </summary>
  248. public static readonly string CSI_SendDeviceAttributes = CSI + "0c";
  249. /// <summary>
  250. /// ESC [ > 0 c - Send Device Attributes (Secondary DA)
  251. /// Windows Terminal v1.18+ emits: "\x1b[>0;10;1c" (vt100, firmware version 1.0, vt220)
  252. /// </summary>
  253. public static readonly string CSI_SendDeviceAttributes2 = CSI + ">0c";
  254. /// <summary>
  255. /// The terminator indicating a reply to <see cref="CSI_SendDeviceAttributes"/> or <see cref="CSI_SendDeviceAttributes2"/>
  256. ///
  257. /// </summary>
  258. public const string CSI_ReportDeviceAttributes_Terminator = "c";
  259. /// <summary>
  260. /// CSI 1 8 t | yes | yes | yes | report window size in chars
  261. /// https://terminalguide.namepad.de/seq/csi_st-18/
  262. /// </summary>
  263. public static readonly string CSI_ReportTerminalSizeInChars = CSI + "18t";
  264. /// <summary>
  265. /// The terminator indicating a reply to <see cref="CSI_ReportTerminalSizeInChars"/> : ESC [ 8 ; height ; width t
  266. /// </summary>
  267. public const string CSI_ReportTerminalSizeInChars_Terminator = "t";
  268. /// <summary>
  269. /// The value of the response to <see cref="CSI_ReportTerminalSizeInChars"/> indicating value 1 and 2 are the terminal size in chars.
  270. /// </summary>
  271. public const string CSI_ReportTerminalSizeInChars_ResponseValue = "8";
  272. #endregion
  273. /// <summary>
  274. /// Ensures a console key is mapped to one that works correctly with ANSI escape sequences.
  275. /// </summary>
  276. /// <param name="consoleKeyInfo">The <see cref="ConsoleKeyInfo"/>.</param>
  277. /// <returns>The <see cref="ConsoleKeyInfo"/> modified.</returns>
  278. public static ConsoleKeyInfo MapConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  279. {
  280. ConsoleKeyInfo newConsoleKeyInfo = consoleKeyInfo;
  281. ConsoleKey key;
  282. var keyChar = consoleKeyInfo.KeyChar;
  283. switch ((uint)keyChar) {
  284. case 0:
  285. if (consoleKeyInfo.Key == (ConsoleKey)64) { // Ctrl+Space in Windows.
  286. newConsoleKeyInfo = new ConsoleKeyInfo (' ', ConsoleKey.Spacebar,
  287. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  288. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  289. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  290. }
  291. break;
  292. case uint n when n > 0 && n <= KeyEsc:
  293. if (consoleKeyInfo.Key == 0 && consoleKeyInfo.KeyChar == '\r') {
  294. key = ConsoleKey.Enter;
  295. newConsoleKeyInfo = new ConsoleKeyInfo (consoleKeyInfo.KeyChar,
  296. key,
  297. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  298. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  299. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  300. } else if (consoleKeyInfo.Key == 0) {
  301. key = (ConsoleKey)(char)(consoleKeyInfo.KeyChar + (uint)ConsoleKey.A - 1);
  302. newConsoleKeyInfo = new ConsoleKeyInfo ((char)key,
  303. key,
  304. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  305. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  306. true);
  307. }
  308. break;
  309. case 127: // DEL
  310. newConsoleKeyInfo = new ConsoleKeyInfo (consoleKeyInfo.KeyChar, ConsoleKey.Backspace,
  311. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  312. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  313. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  314. break;
  315. default:
  316. newConsoleKeyInfo = consoleKeyInfo;
  317. break;
  318. }
  319. return newConsoleKeyInfo;
  320. }
  321. /// <summary>
  322. /// A helper to resize the <see cref="ConsoleKeyInfo"/> as needed.
  323. /// </summary>
  324. /// <param name="consoleKeyInfo">The <see cref="ConsoleKeyInfo"/>.</param>
  325. /// <param name="cki">The <see cref="ConsoleKeyInfo"/> array to resize.</param>
  326. /// <returns>The <see cref="ConsoleKeyInfo"/> resized.</returns>
  327. public static ConsoleKeyInfo [] ResizeArray (ConsoleKeyInfo consoleKeyInfo, ConsoleKeyInfo [] cki)
  328. {
  329. Array.Resize (ref cki, cki == null ? 1 : cki.Length + 1);
  330. cki [cki.Length - 1] = consoleKeyInfo;
  331. return cki;
  332. }
  333. /// <summary>
  334. /// Decodes an ANSI escape sequence.
  335. /// </summary>
  336. /// <param name="escSeqRequests">The <see cref="EscSeqRequests"/> which may contain a request.</param>
  337. /// <param name="newConsoleKeyInfo">The <see cref="ConsoleKeyInfo"/> which may changes.</param>
  338. /// <param name="key">The <see cref="ConsoleKey"/> which may changes.</param>
  339. /// <param name="cki">The <see cref="ConsoleKeyInfo"/> array.</param>
  340. /// <param name="mod">The <see cref="ConsoleModifiers"/> which may changes.</param>
  341. /// <param name="c1Control">The control returned by the <see cref="GetC1ControlChar(char)"/> method.</param>
  342. /// <param name="code">The code returned by the <see cref="GetEscapeResult(char[])"/> method.</param>
  343. /// <param name="values">The values returned by the <see cref="GetEscapeResult(char[])"/> method.</param>
  344. /// <param name="terminator">The terminator returned by the <see cref="GetEscapeResult(char[])"/> method.</param>
  345. /// <param name="isMouse">Indicates if the escape sequence is a mouse event.</param>
  346. /// <param name="buttonState">The <see cref="MouseFlags"/> button state.</param>
  347. /// <param name="pos">The <see cref="MouseFlags"/> position.</param>
  348. /// <param name="isResponse">Indicates if the escape sequence is a response to a request.</param>
  349. /// <param name="continuousButtonPressedHandler">The handler that will process the event.</param>
  350. public static void DecodeEscSeq (EscSeqRequests escSeqRequests, ref ConsoleKeyInfo newConsoleKeyInfo, ref ConsoleKey key, ConsoleKeyInfo [] cki, ref ConsoleModifiers mod, out string c1Control, out string code, out string [] values, out string terminator, out bool isMouse, out List<MouseFlags> buttonState, out Point pos, out bool isResponse, Action<MouseFlags, Point> continuousButtonPressedHandler)
  351. {
  352. char [] kChars = GetKeyCharArray (cki);
  353. (c1Control, code, values, terminator) = GetEscapeResult (kChars);
  354. isMouse = false;
  355. buttonState = new List<MouseFlags> () { 0 };
  356. pos = default;
  357. isResponse = false;
  358. switch (c1Control) {
  359. case "ESC":
  360. if (values == null && string.IsNullOrEmpty (terminator)) {
  361. key = ConsoleKey.Escape;
  362. newConsoleKeyInfo = new ConsoleKeyInfo (cki [0].KeyChar, key,
  363. (mod & ConsoleModifiers.Shift) != 0,
  364. (mod & ConsoleModifiers.Alt) != 0,
  365. (mod & ConsoleModifiers.Control) != 0);
  366. } else if ((uint)cki [1].KeyChar >= 1 && (uint)cki [1].KeyChar <= 26) {
  367. key = (ConsoleKey)(char)(cki [1].KeyChar + (uint)ConsoleKey.A - 1);
  368. newConsoleKeyInfo = new ConsoleKeyInfo (cki [1].KeyChar,
  369. key,
  370. false,
  371. true,
  372. true);
  373. } else {
  374. if (cki [1].KeyChar >= 97 && cki [1].KeyChar <= 122) {
  375. key = (ConsoleKey)cki [1].KeyChar.ToString ().ToUpper () [0];
  376. } else {
  377. key = (ConsoleKey)cki [1].KeyChar;
  378. }
  379. newConsoleKeyInfo = new ConsoleKeyInfo ((char)key,
  380. (ConsoleKey)Math.Min ((uint)key, 255),
  381. false,
  382. true,
  383. false);
  384. }
  385. break;
  386. case "SS3":
  387. key = GetConsoleKey (terminator [0], values [0], ref mod);
  388. newConsoleKeyInfo = new ConsoleKeyInfo ('\0',
  389. key,
  390. (mod & ConsoleModifiers.Shift) != 0,
  391. (mod & ConsoleModifiers.Alt) != 0,
  392. (mod & ConsoleModifiers.Control) != 0);
  393. break;
  394. case "CSI":
  395. if (!string.IsNullOrEmpty (code) && code == "<") {
  396. GetMouse (cki, out buttonState, out pos, continuousButtonPressedHandler);
  397. isMouse = true;
  398. return;
  399. } else if (escSeqRequests != null && escSeqRequests.HasResponse (terminator)) {
  400. isResponse = true;
  401. escSeqRequests.Remove (terminator);
  402. return;
  403. }
  404. key = GetConsoleKey (terminator [0], values [0], ref mod);
  405. if (key != 0 && values.Length > 1) {
  406. mod |= GetConsoleModifiers (values [1]);
  407. }
  408. newConsoleKeyInfo = new ConsoleKeyInfo ('\0',
  409. key,
  410. (mod & ConsoleModifiers.Shift) != 0,
  411. (mod & ConsoleModifiers.Alt) != 0,
  412. (mod & ConsoleModifiers.Control) != 0);
  413. break;
  414. }
  415. }
  416. /// <summary>
  417. /// Gets all the needed information about a escape sequence.
  418. /// </summary>
  419. /// <param name="kChar">The array with all chars.</param>
  420. /// <returns>
  421. /// The c1Control returned by <see cref="GetC1ControlChar(char)"/>, code, values and terminating.
  422. /// </returns>
  423. public static (string c1Control, string code, string [] values, string terminating) GetEscapeResult (char [] kChar)
  424. {
  425. if (kChar == null || kChar.Length == 0) {
  426. return (null, null, null, null);
  427. }
  428. if (kChar [0] != KeyEsc) {
  429. throw new InvalidOperationException ("Invalid escape character!");
  430. }
  431. if (kChar.Length == 1) {
  432. return ("ESC", null, null, null);
  433. }
  434. if (kChar.Length == 2) {
  435. return ("ESC", null, null, kChar [1].ToString ());
  436. }
  437. string c1Control = GetC1ControlChar (kChar [1]);
  438. string code = null;
  439. int nSep = kChar.Count (x => x == ';') + 1;
  440. string [] values = new string [nSep];
  441. int valueIdx = 0;
  442. string terminating = "";
  443. for (int i = 2; i < kChar.Length; i++) {
  444. var c = kChar [i];
  445. if (char.IsDigit (c)) {
  446. values [valueIdx] += c.ToString ();
  447. } else if (c == ';') {
  448. valueIdx++;
  449. } else if (valueIdx == nSep - 1 || i == kChar.Length - 1) {
  450. terminating += c.ToString ();
  451. } else {
  452. code += c.ToString ();
  453. }
  454. }
  455. return (c1Control, code, values, terminating);
  456. }
  457. /// <summary>
  458. /// Gets the c1Control used in the called escape sequence.
  459. /// </summary>
  460. /// <param name="c">The char used.</param>
  461. /// <returns>The c1Control.</returns>
  462. public static string GetC1ControlChar (char c)
  463. {
  464. // These control characters are used in the vtXXX emulation.
  465. switch (c) {
  466. case 'D':
  467. return "IND"; // Index
  468. case 'E':
  469. return "NEL"; // Next Line
  470. case 'H':
  471. return "HTS"; // Tab Set
  472. case 'M':
  473. return "RI"; // Reverse Index
  474. case 'N':
  475. return "SS2"; // Single Shift Select of G2 Character Set: affects next character only
  476. case 'O':
  477. return "SS3"; // Single Shift Select of G3 Character Set: affects next character only
  478. case 'P':
  479. return "DCS"; // Device Control String
  480. case 'V':
  481. return "SPA"; // Start of Guarded Area
  482. case 'W':
  483. return "EPA"; // End of Guarded Area
  484. case 'X':
  485. return "SOS"; // Start of String
  486. case 'Z':
  487. return "DECID"; // Return Terminal ID Obsolete form of CSI c (DA)
  488. case '[':
  489. return "CSI"; // Control Sequence Introducer
  490. case '\\':
  491. return "ST"; // String Terminator
  492. case ']':
  493. return "OSC"; // Operating System Command
  494. case '^':
  495. return "PM"; // Privacy Message
  496. case '_':
  497. return "APC"; // Application Program Command
  498. default:
  499. return ""; // Not supported
  500. }
  501. }
  502. /// <summary>
  503. /// Gets the <see cref="ConsoleModifiers"/> from the value.
  504. /// </summary>
  505. /// <param name="value">The value.</param>
  506. /// <returns>The <see cref="ConsoleModifiers"/> or zero.</returns>
  507. public static ConsoleModifiers GetConsoleModifiers (string value)
  508. {
  509. switch (value) {
  510. case "2":
  511. return ConsoleModifiers.Shift;
  512. case "3":
  513. return ConsoleModifiers.Alt;
  514. case "4":
  515. return ConsoleModifiers.Shift | ConsoleModifiers.Alt;
  516. case "5":
  517. return ConsoleModifiers.Control;
  518. case "6":
  519. return ConsoleModifiers.Shift | ConsoleModifiers.Control;
  520. case "7":
  521. return ConsoleModifiers.Alt | ConsoleModifiers.Control;
  522. case "8":
  523. return ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control;
  524. default:
  525. return 0;
  526. }
  527. }
  528. /// <summary>
  529. /// Gets the <see cref="ConsoleKey"/> depending on terminating and value.
  530. /// </summary>
  531. /// <param name="terminating">The terminating.</param>
  532. /// <param name="value">The value.</param>
  533. /// <param name="mod">The <see cref="ConsoleModifiers"/> which may changes.</param>
  534. /// <returns>The <see cref="ConsoleKey"/> and probably the <see cref="ConsoleModifiers"/>.</returns>
  535. public static ConsoleKey GetConsoleKey (char terminating, string value, ref ConsoleModifiers mod)
  536. {
  537. ConsoleKey key;
  538. switch (terminating) {
  539. case 'A':
  540. key = ConsoleKey.UpArrow;
  541. break;
  542. case 'B':
  543. key = ConsoleKey.DownArrow;
  544. break;
  545. case 'C':
  546. key = ConsoleKey.RightArrow;
  547. break;
  548. case 'D':
  549. key = ConsoleKey.LeftArrow;
  550. break;
  551. case 'F':
  552. key = ConsoleKey.End;
  553. break;
  554. case 'H':
  555. key = ConsoleKey.Home;
  556. break;
  557. case 'P':
  558. key = ConsoleKey.F1;
  559. break;
  560. case 'Q':
  561. key = ConsoleKey.F2;
  562. break;
  563. case 'R':
  564. key = ConsoleKey.F3;
  565. break;
  566. case 'S':
  567. key = ConsoleKey.F4;
  568. break;
  569. case 'Z':
  570. key = ConsoleKey.Tab;
  571. mod |= ConsoleModifiers.Shift;
  572. break;
  573. case '~':
  574. switch (value) {
  575. case "2":
  576. key = ConsoleKey.Insert;
  577. break;
  578. case "3":
  579. key = ConsoleKey.Delete;
  580. break;
  581. case "5":
  582. key = ConsoleKey.PageUp;
  583. break;
  584. case "6":
  585. key = ConsoleKey.PageDown;
  586. break;
  587. case "15":
  588. key = ConsoleKey.F5;
  589. break;
  590. case "17":
  591. key = ConsoleKey.F6;
  592. break;
  593. case "18":
  594. key = ConsoleKey.F7;
  595. break;
  596. case "19":
  597. key = ConsoleKey.F8;
  598. break;
  599. case "20":
  600. key = ConsoleKey.F9;
  601. break;
  602. case "21":
  603. key = ConsoleKey.F10;
  604. break;
  605. case "23":
  606. key = ConsoleKey.F11;
  607. break;
  608. case "24":
  609. key = ConsoleKey.F12;
  610. break;
  611. default:
  612. key = 0;
  613. break;
  614. }
  615. break;
  616. default:
  617. key = 0;
  618. break;
  619. }
  620. return key;
  621. }
  622. /// <summary>
  623. /// A helper to get only the <see cref="ConsoleKeyInfo.KeyChar"/> from the <see cref="ConsoleKeyInfo"/> array.
  624. /// </summary>
  625. /// <param name="cki"></param>
  626. /// <returns>The char array of the escape sequence.</returns>
  627. public static char [] GetKeyCharArray (ConsoleKeyInfo [] cki)
  628. {
  629. char [] kChar = new char [] { };
  630. var length = 0;
  631. foreach (var kc in cki) {
  632. length++;
  633. Array.Resize (ref kChar, length);
  634. kChar [length - 1] = kc.KeyChar;
  635. }
  636. return kChar;
  637. }
  638. private static MouseFlags? lastMouseButtonPressed;
  639. //private static MouseFlags? lastMouseButtonReleased;
  640. private static bool isButtonPressed;
  641. //private static bool isButtonReleased;
  642. private static bool isButtonClicked;
  643. private static bool isButtonDoubleClicked;
  644. private static bool isButtonTripleClicked;
  645. private static Point point;
  646. /// <summary>
  647. /// Gets the <see cref="MouseFlags"/> mouse button flags and the position.
  648. /// </summary>
  649. /// <param name="cki">The <see cref="ConsoleKeyInfo"/> array.</param>
  650. /// <param name="mouseFlags">The mouse button flags.</param>
  651. /// <param name="pos">The mouse position.</param>
  652. /// <param name="continuousButtonPressedHandler">The handler that will process the event.</param>
  653. public static void GetMouse (ConsoleKeyInfo [] cki, out List<MouseFlags> mouseFlags, out Point pos, Action<MouseFlags, Point> continuousButtonPressedHandler)
  654. {
  655. MouseFlags buttonState = 0;
  656. pos = new Point ();
  657. int buttonCode = 0;
  658. bool foundButtonCode = false;
  659. int foundPoint = 0;
  660. string value = "";
  661. var kChar = GetKeyCharArray (cki);
  662. //System.Diagnostics.Debug.WriteLine ($"kChar: {new string (kChar)}");
  663. for (int i = 0; i < kChar.Length; i++) {
  664. var c = kChar [i];
  665. if (c == '<') {
  666. foundButtonCode = true;
  667. } else if (foundButtonCode && c != ';') {
  668. value += c.ToString ();
  669. } else if (c == ';') {
  670. if (foundButtonCode) {
  671. foundButtonCode = false;
  672. buttonCode = int.Parse (value);
  673. }
  674. if (foundPoint == 1) {
  675. pos.X = int.Parse (value) - 1;
  676. }
  677. value = "";
  678. foundPoint++;
  679. } else if (foundPoint > 0 && c != 'm' && c != 'M') {
  680. value += c.ToString ();
  681. } else if (c == 'm' || c == 'M') {
  682. //pos.Y = int.Parse (value) + Console.WindowTop - 1;
  683. pos.Y = int.Parse (value) - 1;
  684. switch (buttonCode) {
  685. case 0:
  686. case 8:
  687. case 16:
  688. case 24:
  689. case 32:
  690. case 36:
  691. case 40:
  692. case 48:
  693. case 56:
  694. buttonState = c == 'M' ? MouseFlags.Button1Pressed
  695. : MouseFlags.Button1Released;
  696. break;
  697. case 1:
  698. case 9:
  699. case 17:
  700. case 25:
  701. case 33:
  702. case 37:
  703. case 41:
  704. case 45:
  705. case 49:
  706. case 53:
  707. case 57:
  708. case 61:
  709. buttonState = c == 'M' ? MouseFlags.Button2Pressed
  710. : MouseFlags.Button2Released;
  711. break;
  712. case 2:
  713. case 10:
  714. case 14:
  715. case 18:
  716. case 22:
  717. case 26:
  718. case 30:
  719. case 34:
  720. case 42:
  721. case 46:
  722. case 50:
  723. case 54:
  724. case 58:
  725. case 62:
  726. buttonState = c == 'M' ? MouseFlags.Button3Pressed
  727. : MouseFlags.Button3Released;
  728. break;
  729. case 35:
  730. //// Needed for Windows OS
  731. //if (isButtonPressed && c == 'm'
  732. // && (lastMouseEvent.ButtonState == MouseFlags.Button1Pressed
  733. // || lastMouseEvent.ButtonState == MouseFlags.Button2Pressed
  734. // || lastMouseEvent.ButtonState == MouseFlags.Button3Pressed)) {
  735. // switch (lastMouseEvent.ButtonState) {
  736. // case MouseFlags.Button1Pressed:
  737. // buttonState = MouseFlags.Button1Released;
  738. // break;
  739. // case MouseFlags.Button2Pressed:
  740. // buttonState = MouseFlags.Button2Released;
  741. // break;
  742. // case MouseFlags.Button3Pressed:
  743. // buttonState = MouseFlags.Button3Released;
  744. // break;
  745. // }
  746. //} else {
  747. // buttonState = MouseFlags.ReportMousePosition;
  748. //}
  749. //break;
  750. case 39:
  751. case 43:
  752. case 47:
  753. case 51:
  754. case 55:
  755. case 59:
  756. case 63:
  757. buttonState = MouseFlags.ReportMousePosition;
  758. break;
  759. case 64:
  760. buttonState = MouseFlags.WheeledUp;
  761. break;
  762. case 65:
  763. buttonState = MouseFlags.WheeledDown;
  764. break;
  765. case 68:
  766. case 72:
  767. case 80:
  768. buttonState = MouseFlags.WheeledLeft; // Shift/Ctrl+WheeledUp
  769. break;
  770. case 69:
  771. case 73:
  772. case 81:
  773. buttonState = MouseFlags.WheeledRight; // Shift/Ctrl+WheeledDown
  774. break;
  775. }
  776. // Modifiers.
  777. switch (buttonCode) {
  778. case 8:
  779. case 9:
  780. case 10:
  781. case 43:
  782. buttonState |= MouseFlags.ButtonAlt;
  783. break;
  784. case 14:
  785. case 47:
  786. buttonState |= MouseFlags.ButtonAlt | MouseFlags.ButtonShift;
  787. break;
  788. case 16:
  789. case 17:
  790. case 18:
  791. case 51:
  792. buttonState |= MouseFlags.ButtonCtrl;
  793. break;
  794. case 22:
  795. case 55:
  796. buttonState |= MouseFlags.ButtonCtrl | MouseFlags.ButtonShift;
  797. break;
  798. case 24:
  799. case 25:
  800. case 26:
  801. case 59:
  802. buttonState |= MouseFlags.ButtonCtrl | MouseFlags.ButtonAlt;
  803. break;
  804. case 30:
  805. case 63:
  806. buttonState |= MouseFlags.ButtonCtrl | MouseFlags.ButtonShift | MouseFlags.ButtonAlt;
  807. break;
  808. case 32:
  809. case 33:
  810. case 34:
  811. buttonState |= MouseFlags.ReportMousePosition;
  812. break;
  813. case 36:
  814. case 37:
  815. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonShift;
  816. break;
  817. case 39:
  818. case 68:
  819. case 69:
  820. buttonState |= MouseFlags.ButtonShift;
  821. break;
  822. case 40:
  823. case 41:
  824. case 42:
  825. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonAlt;
  826. break;
  827. case 45:
  828. case 46:
  829. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonAlt | MouseFlags.ButtonShift;
  830. break;
  831. case 48:
  832. case 49:
  833. case 50:
  834. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl;
  835. break;
  836. case 53:
  837. case 54:
  838. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl | MouseFlags.ButtonShift;
  839. break;
  840. case 56:
  841. case 57:
  842. case 58:
  843. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl | MouseFlags.ButtonAlt;
  844. break;
  845. case 61:
  846. case 62:
  847. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl | MouseFlags.ButtonShift | MouseFlags.ButtonAlt;
  848. break;
  849. }
  850. }
  851. }
  852. mouseFlags = new List<MouseFlags> () { MouseFlags.AllEvents };
  853. if (lastMouseButtonPressed != null && !isButtonPressed && !buttonState.HasFlag (MouseFlags.ReportMousePosition)
  854. && !buttonState.HasFlag (MouseFlags.Button1Released)
  855. && !buttonState.HasFlag (MouseFlags.Button2Released)
  856. && !buttonState.HasFlag (MouseFlags.Button3Released)
  857. && !buttonState.HasFlag (MouseFlags.Button4Released)) {
  858. lastMouseButtonPressed = null;
  859. isButtonPressed = false;
  860. }
  861. if (!isButtonClicked && !isButtonDoubleClicked && ((buttonState == MouseFlags.Button1Pressed || buttonState == MouseFlags.Button2Pressed ||
  862. buttonState == MouseFlags.Button3Pressed || buttonState == MouseFlags.Button4Pressed) && lastMouseButtonPressed == null) ||
  863. isButtonPressed && lastMouseButtonPressed != null && buttonState.HasFlag (MouseFlags.ReportMousePosition)) {
  864. mouseFlags [0] = buttonState;
  865. lastMouseButtonPressed = buttonState;
  866. isButtonPressed = true;
  867. if ((mouseFlags [0] & MouseFlags.ReportMousePosition) == 0) {
  868. point = new Point () {
  869. X = pos.X,
  870. Y = pos.Y
  871. };
  872. Application.MainLoop.AddIdle (() => {
  873. Task.Run (async () => await ProcessContinuousButtonPressedAsync (buttonState, continuousButtonPressedHandler));
  874. return false;
  875. });
  876. } else if (mouseFlags [0] == MouseFlags.ReportMousePosition) {
  877. isButtonPressed = false;
  878. }
  879. } else if (isButtonDoubleClicked && (buttonState == MouseFlags.Button1Pressed || buttonState == MouseFlags.Button2Pressed ||
  880. buttonState == MouseFlags.Button3Pressed || buttonState == MouseFlags.Button4Pressed)) {
  881. mouseFlags [0] = GetButtonTripleClicked (buttonState);
  882. isButtonDoubleClicked = false;
  883. isButtonTripleClicked = true;
  884. } else if (isButtonClicked && (buttonState == MouseFlags.Button1Pressed || buttonState == MouseFlags.Button2Pressed ||
  885. buttonState == MouseFlags.Button3Pressed || buttonState == MouseFlags.Button4Pressed)) {
  886. mouseFlags [0] = GetButtonDoubleClicked (buttonState);
  887. isButtonClicked = false;
  888. isButtonDoubleClicked = true;
  889. Application.MainLoop.AddIdle (() => {
  890. Task.Run (async () => await ProcessButtonDoubleClickedAsync ());
  891. return false;
  892. });
  893. }
  894. //else if (isButtonReleased && !isButtonClicked && buttonState == MouseFlags.ReportMousePosition) {
  895. // mouseFlag [0] = GetButtonClicked ((MouseFlags)lastMouseButtonReleased);
  896. // lastMouseButtonReleased = null;
  897. // isButtonReleased = false;
  898. // isButtonClicked = true;
  899. // Application.MainLoop.AddIdle (() => {
  900. // Task.Run (async () => await ProcessButtonClickedAsync ());
  901. // return false;
  902. // });
  903. //}
  904. else if (!isButtonClicked && !isButtonDoubleClicked && (buttonState == MouseFlags.Button1Released || buttonState == MouseFlags.Button2Released ||
  905. buttonState == MouseFlags.Button3Released || buttonState == MouseFlags.Button4Released)) {
  906. mouseFlags [0] = buttonState;
  907. isButtonPressed = false;
  908. if (isButtonTripleClicked) {
  909. isButtonTripleClicked = false;
  910. } else if (pos.X == point.X && pos.Y == point.Y) {
  911. mouseFlags.Add (GetButtonClicked (buttonState));
  912. isButtonClicked = true;
  913. Application.MainLoop.AddIdle (() => {
  914. Task.Run (async () => await ProcessButtonClickedAsync ());
  915. return false;
  916. });
  917. }
  918. point = pos;
  919. //if ((lastMouseButtonPressed & MouseFlags.ReportMousePosition) == 0) {
  920. // lastMouseButtonReleased = buttonState;
  921. // isButtonPressed = false;
  922. // isButtonReleased = true;
  923. //} else {
  924. // lastMouseButtonPressed = null;
  925. // isButtonPressed = false;
  926. //}
  927. } else if (buttonState == MouseFlags.WheeledUp) {
  928. mouseFlags [0] = MouseFlags.WheeledUp;
  929. } else if (buttonState == MouseFlags.WheeledDown) {
  930. mouseFlags [0] = MouseFlags.WheeledDown;
  931. } else if (buttonState == MouseFlags.WheeledLeft) {
  932. mouseFlags [0] = MouseFlags.WheeledLeft;
  933. } else if (buttonState == MouseFlags.WheeledRight) {
  934. mouseFlags [0] = MouseFlags.WheeledRight;
  935. } else if (buttonState == MouseFlags.ReportMousePosition) {
  936. mouseFlags [0] = MouseFlags.ReportMousePosition;
  937. } else {
  938. mouseFlags [0] = buttonState;
  939. //foreach (var flag in buttonState.GetUniqueFlags()) {
  940. // mouseFlag [0] |= flag;
  941. //}
  942. }
  943. mouseFlags [0] = SetControlKeyStates (buttonState, mouseFlags [0]);
  944. //buttonState = mouseFlags;
  945. //System.Diagnostics.Debug.WriteLine ($"buttonState: {buttonState} X: {pos.X} Y: {pos.Y}");
  946. //foreach (var mf in mouseFlags) {
  947. // System.Diagnostics.Debug.WriteLine ($"mouseFlags: {mf} X: {pos.X} Y: {pos.Y}");
  948. //}
  949. }
  950. private static async Task ProcessContinuousButtonPressedAsync (MouseFlags mouseFlag, Action<MouseFlags, Point> continuousButtonPressedHandler)
  951. {
  952. while (isButtonPressed) {
  953. await Task.Delay (100);
  954. //var me = new MouseEvent () {
  955. // X = point.X,
  956. // Y = point.Y,
  957. // Flags = mouseFlag
  958. //};
  959. var view = Application.WantContinuousButtonPressedView;
  960. if (view == null)
  961. break;
  962. if (isButtonPressed && lastMouseButtonPressed != null && (mouseFlag & MouseFlags.ReportMousePosition) == 0) {
  963. Application.MainLoop.Invoke (() => continuousButtonPressedHandler (mouseFlag, point));
  964. }
  965. }
  966. }
  967. private static async Task ProcessButtonClickedAsync ()
  968. {
  969. await Task.Delay (300);
  970. isButtonClicked = false;
  971. }
  972. private static async Task ProcessButtonDoubleClickedAsync ()
  973. {
  974. await Task.Delay (300);
  975. isButtonDoubleClicked = false;
  976. }
  977. private static MouseFlags GetButtonClicked (MouseFlags mouseFlag)
  978. {
  979. MouseFlags mf = default;
  980. switch (mouseFlag) {
  981. case MouseFlags.Button1Released:
  982. mf = MouseFlags.Button1Clicked;
  983. break;
  984. case MouseFlags.Button2Released:
  985. mf = MouseFlags.Button2Clicked;
  986. break;
  987. case MouseFlags.Button3Released:
  988. mf = MouseFlags.Button3Clicked;
  989. break;
  990. }
  991. return mf;
  992. }
  993. private static MouseFlags GetButtonDoubleClicked (MouseFlags mouseFlag)
  994. {
  995. MouseFlags mf = default;
  996. switch (mouseFlag) {
  997. case MouseFlags.Button1Pressed:
  998. mf = MouseFlags.Button1DoubleClicked;
  999. break;
  1000. case MouseFlags.Button2Pressed:
  1001. mf = MouseFlags.Button2DoubleClicked;
  1002. break;
  1003. case MouseFlags.Button3Pressed:
  1004. mf = MouseFlags.Button3DoubleClicked;
  1005. break;
  1006. }
  1007. return mf;
  1008. }
  1009. private static MouseFlags GetButtonTripleClicked (MouseFlags mouseFlag)
  1010. {
  1011. MouseFlags mf = default;
  1012. switch (mouseFlag) {
  1013. case MouseFlags.Button1Pressed:
  1014. mf = MouseFlags.Button1TripleClicked;
  1015. break;
  1016. case MouseFlags.Button2Pressed:
  1017. mf = MouseFlags.Button2TripleClicked;
  1018. break;
  1019. case MouseFlags.Button3Pressed:
  1020. mf = MouseFlags.Button3TripleClicked;
  1021. break;
  1022. }
  1023. return mf;
  1024. }
  1025. private static MouseFlags SetControlKeyStates (MouseFlags buttonState, MouseFlags mouseFlag)
  1026. {
  1027. if ((buttonState & MouseFlags.ButtonCtrl) != 0 && (mouseFlag & MouseFlags.ButtonCtrl) == 0)
  1028. mouseFlag |= MouseFlags.ButtonCtrl;
  1029. if ((buttonState & MouseFlags.ButtonShift) != 0 && (mouseFlag & MouseFlags.ButtonShift) == 0)
  1030. mouseFlag |= MouseFlags.ButtonShift;
  1031. if ((buttonState & MouseFlags.ButtonAlt) != 0 && (mouseFlag & MouseFlags.ButtonAlt) == 0)
  1032. mouseFlag |= MouseFlags.ButtonAlt;
  1033. return mouseFlag;
  1034. }
  1035. // TODO: Move this out of here and into ConsoleDriver or somewhere else.
  1036. /// <summary>
  1037. /// Get the terminal that holds the console driver.
  1038. /// </summary>
  1039. /// <param name="process">The process.</param>
  1040. /// <returns>If supported the executable console process, null otherwise.</returns>
  1041. public static Process GetParentProcess (Process process)
  1042. {
  1043. if (!RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  1044. return null;
  1045. }
  1046. string query = "SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = " + process.Id;
  1047. using (ManagementObjectSearcher mos = new ManagementObjectSearcher (query)) {
  1048. foreach (ManagementObject mo in mos.Get ()) {
  1049. if (mo ["ParentProcessId"] != null) {
  1050. try {
  1051. var id = Convert.ToInt32 (mo ["ParentProcessId"]);
  1052. return Process.GetProcessById (id);
  1053. } catch {
  1054. }
  1055. }
  1056. }
  1057. }
  1058. return null;
  1059. }
  1060. }