ConsoleKeyMapping.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Terminal.Gui.ConsoleDrivers;
  8. /// <summary>
  9. /// Helper class to handle the scan code and virtual key from a <see cref="ConsoleKey"/>.
  10. /// </summary>
  11. public static class ConsoleKeyMapping {
  12. #if !WT_ISSUE_8871_FIXED // https://github.com/microsoft/terminal/issues/8871
  13. /// <summary>
  14. /// Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
  15. /// </summary>
  16. /// <param name="vk"></param>
  17. /// <param name="uMapType">
  18. /// If MAPVK_VK_TO_CHAR (2) - The uCode parameter is a virtual-key code and is translated into an un-shifted
  19. /// character value in the low order word of the return value.
  20. /// </param>
  21. /// <param name="dwhkl"></param>
  22. /// <returns>An un-shifted character value in the low order word of the return value. Dead keys (diacritics)
  23. /// are indicated by setting the top bit of the return value. If there is no translation,
  24. /// the function returns 0. See Remarks.</returns>
  25. [DllImport ("user32.dll", EntryPoint = "MapVirtualKeyExW", CharSet = CharSet.Unicode)]
  26. extern static uint MapVirtualKeyEx (VK vk, uint uMapType, IntPtr dwhkl);
  27. /// <summary>
  28. /// Retrieves the active input locale identifier (formerly called the keyboard layout).
  29. /// </summary>
  30. /// <param name="idThread">0 for current thread</param>
  31. /// <returns>The return value is the input locale identifier for the thread.
  32. /// The low word contains a Language Identifier for the input language
  33. /// and the high word contains a device handle to the physical layout of the keyboard.
  34. /// </returns>
  35. [DllImport ("user32.dll", EntryPoint = "GetKeyboardLayout", CharSet = CharSet.Unicode)]
  36. extern static IntPtr GetKeyboardLayout (IntPtr idThread);
  37. //[DllImport ("user32.dll", EntryPoint = "GetKeyboardLayoutNameW", CharSet = CharSet.Unicode)]
  38. //extern static uint GetKeyboardLayoutName (uint idThread);
  39. [DllImport ("user32.dll")]
  40. extern static IntPtr GetForegroundWindow ();
  41. [DllImport ("user32.dll")]
  42. extern static IntPtr GetWindowThreadProcessId (IntPtr hWnd, IntPtr ProcessId);
  43. /// <summary>
  44. /// Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters using
  45. /// the Win32 API MapVirtualKey.
  46. /// </summary>
  47. /// <param name="vk"></param>
  48. /// <returns>An un-shifted character value in the low order word of the return value. Dead keys (diacritics)
  49. /// are indicated by setting the top bit of the return value. If there is no translation,
  50. /// the function returns 0.</returns>
  51. public static uint MapVKtoChar (VK vk)
  52. {
  53. if (Environment.OSVersion.Platform != PlatformID.Win32NT) {
  54. return 0;
  55. }
  56. var tid = GetWindowThreadProcessId (GetForegroundWindow (), 0);
  57. var hkl = GetKeyboardLayout (tid);
  58. return MapVirtualKeyEx (vk, 2, hkl);
  59. }
  60. #else
  61. /// <summary>
  62. /// Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
  63. /// </summary>
  64. /// <param name="vk"></param>
  65. /// <param name="uMapType">
  66. /// If MAPVK_VK_TO_CHAR (2) - The uCode parameter is a virtual-key code and is translated into an unshifted
  67. /// character value in the low order word of the return value.
  68. /// </param>
  69. /// <returns>An unshifted character value in the low order word of the return value. Dead keys (diacritics)
  70. /// are indicated by setting the top bit of the return value. If there is no translation,
  71. /// the function returns 0. See Remarks.</returns>
  72. [DllImport ("user32.dll", EntryPoint = "MapVirtualKeyW", CharSet = CharSet.Unicode)]
  73. extern static uint MapVirtualKey (VK vk, uint uMapType = 2);
  74. uint MapVKtoChar (VK vk) => MapVirtualKeyToCharEx (vk);
  75. #endif
  76. /// <summary>
  77. /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the calling thread.
  78. /// </summary>
  79. /// <param name="pwszKLID"></param>
  80. /// <returns></returns>
  81. [DllImport ("user32.dll")]
  82. extern static bool GetKeyboardLayoutName ([Out] StringBuilder pwszKLID);
  83. /// <summary>
  84. /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the calling thread.
  85. /// </summary>
  86. /// <returns></returns>
  87. public static string GetKeyboardLayoutName ()
  88. {
  89. if (Environment.OSVersion.Platform != PlatformID.Win32NT) {
  90. return "none";
  91. }
  92. StringBuilder klidSB = new StringBuilder ();
  93. GetKeyboardLayoutName (klidSB);
  94. return klidSB.ToString ();
  95. }
  96. class ScanCodeMapping : IEquatable<ScanCodeMapping> {
  97. public uint ScanCode;
  98. public VK VirtualKey;
  99. public ConsoleModifiers Modifiers;
  100. public uint UnicodeChar;
  101. public ScanCodeMapping (uint scanCode, VK virtualKey, ConsoleModifiers modifiers, uint unicodeChar)
  102. {
  103. ScanCode = scanCode;
  104. VirtualKey = virtualKey;
  105. Modifiers = modifiers;
  106. UnicodeChar = unicodeChar;
  107. }
  108. public bool Equals (ScanCodeMapping other)
  109. {
  110. return ScanCode.Equals (other.ScanCode) &&
  111. VirtualKey.Equals (other.VirtualKey) &&
  112. Modifiers.Equals (other.Modifiers) &&
  113. UnicodeChar.Equals (other.UnicodeChar);
  114. }
  115. }
  116. static ConsoleModifiers GetModifiers (ConsoleModifiers modifiers)
  117. {
  118. if (modifiers.HasFlag (ConsoleModifiers.Shift)
  119. && !modifiers.HasFlag (ConsoleModifiers.Alt)
  120. && !modifiers.HasFlag (ConsoleModifiers.Control)) {
  121. return ConsoleModifiers.Shift;
  122. } else if (modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control)) {
  123. return modifiers;
  124. }
  125. return 0;
  126. }
  127. static ScanCodeMapping GetScanCode (string propName, uint keyValue, ConsoleModifiers modifiers)
  128. {
  129. switch (propName) {
  130. case "UnicodeChar":
  131. var sCode = _scanCodes.FirstOrDefault ((e) => e.UnicodeChar == keyValue && e.Modifiers == modifiers);
  132. if (sCode == null && modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control)) {
  133. return _scanCodes.FirstOrDefault ((e) => e.UnicodeChar == keyValue && e.Modifiers == 0);
  134. }
  135. return sCode;
  136. case "VirtualKey":
  137. sCode = _scanCodes.FirstOrDefault ((e) => e.VirtualKey == (VK)keyValue && e.Modifiers == modifiers);
  138. if (sCode == null && modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control)) {
  139. return _scanCodes.FirstOrDefault ((e) => e.VirtualKey == (VK)keyValue && e.Modifiers == 0);
  140. }
  141. return sCode;
  142. }
  143. return null;
  144. }
  145. // BUGBUG: This API is not correct. It is only used by WindowsDriver in VKPacket scenarios
  146. /// <summary>
  147. /// Get the scan code from a <see cref="ConsoleKeyInfo"/>.
  148. /// </summary>
  149. /// <param name="consoleKeyInfo">The console key info.</param>
  150. /// <returns>The value if apply.</returns>
  151. public static uint GetScanCodeFromConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  152. {
  153. var mod = GetModifiers (consoleKeyInfo.Modifiers);
  154. ScanCodeMapping scode = GetScanCode ("VirtualKey", (uint)consoleKeyInfo.Key, mod);
  155. if (scode != null) {
  156. return scode.ScanCode;
  157. }
  158. return 0;
  159. }
  160. // BUGBUG: This API is not correct. It is only used by FakeDriver and VkeyPacketSimulator
  161. /// <summary>
  162. /// Gets the <see cref="ConsoleKeyInfo"/> from the provided <see cref="KeyCode"/>.
  163. /// </summary>
  164. /// <param name="key">The key code.</param>
  165. /// <returns>The console key info.</returns>
  166. public static ConsoleKeyInfo GetConsoleKeyInfoFromKeyCode (KeyCode key)
  167. {
  168. var modifiers = MapToConsoleModifiers (key);
  169. var keyValue = MapKeyCodeToConsoleKey (key, out bool isConsoleKey);
  170. if (isConsoleKey) {
  171. var mod = GetModifiers (modifiers);
  172. var scode = GetScanCode ("VirtualKey", (uint)keyValue, mod);
  173. if (scode != null) {
  174. return new ConsoleKeyInfo ((char)scode.UnicodeChar, (ConsoleKey)scode.VirtualKey, modifiers.HasFlag (ConsoleModifiers.Shift),
  175. modifiers.HasFlag (ConsoleModifiers.Alt), modifiers.HasFlag (ConsoleModifiers.Control));
  176. }
  177. } else {
  178. var keyChar = GetKeyCharFromUnicodeChar ((uint)keyValue, modifiers, out uint consoleKey, out _, isConsoleKey);
  179. if (consoleKey != 0) {
  180. return new ConsoleKeyInfo ((char)keyChar, (ConsoleKey)consoleKey, modifiers.HasFlag (ConsoleModifiers.Shift),
  181. modifiers.HasFlag (ConsoleModifiers.Alt), modifiers.HasFlag (ConsoleModifiers.Control));
  182. }
  183. }
  184. return new ConsoleKeyInfo ((char)keyValue, ConsoleKey.None, modifiers.HasFlag (ConsoleModifiers.Shift),
  185. modifiers.HasFlag (ConsoleModifiers.Alt), modifiers.HasFlag (ConsoleModifiers.Control));
  186. }
  187. /// <summary>
  188. /// Map existing <see cref="KeyCode"/> modifiers to <see cref="ConsoleModifiers"/>.
  189. /// </summary>
  190. /// <param name="key">The key code.</param>
  191. /// <returns>The console modifiers.</returns>
  192. public static ConsoleModifiers MapToConsoleModifiers (KeyCode key)
  193. {
  194. var modifiers = new ConsoleModifiers ();
  195. if (key.HasFlag (KeyCode.ShiftMask)) {
  196. modifiers |= ConsoleModifiers.Shift;
  197. }
  198. if (key.HasFlag (KeyCode.AltMask)) {
  199. modifiers |= ConsoleModifiers.Alt;
  200. }
  201. if (key.HasFlag (KeyCode.CtrlMask)) {
  202. modifiers |= ConsoleModifiers.Control;
  203. }
  204. return modifiers;
  205. }
  206. /// <summary>
  207. /// Gets <see cref="ConsoleModifiers"/> from <see cref="bool"/> modifiers.
  208. /// </summary>
  209. /// <param name="shift">The shift key.</param>
  210. /// <param name="alt">The alt key.</param>
  211. /// <param name="control">The control key.</param>
  212. /// <returns>The console modifiers.</returns>
  213. public static ConsoleModifiers GetModifiers (bool shift, bool alt, bool control)
  214. {
  215. var modifiers = new ConsoleModifiers ();
  216. if (shift) {
  217. modifiers |= ConsoleModifiers.Shift;
  218. }
  219. if (alt) {
  220. modifiers |= ConsoleModifiers.Alt;
  221. }
  222. if (control) {
  223. modifiers |= ConsoleModifiers.Control;
  224. }
  225. return modifiers;
  226. }
  227. /// <summary>
  228. /// Get the <see cref="ConsoleKeyInfo"/> from a unicode character and modifiers (e.g. (Key)'a' and (Key)Key.CtrlMask).
  229. /// </summary>
  230. /// <param name="keyValue">The key as a unicode codepoint.</param>
  231. /// <param name="modifiers">The modifier keys.</param>
  232. /// <param name="scanCode">The resulting scan code.</param>
  233. /// <returns>The <see cref="ConsoleKeyInfo"/>.</returns>
  234. static ConsoleKeyInfo GetConsoleKeyInfoFromKeyChar (uint keyValue, ConsoleModifiers modifiers, out uint scanCode)
  235. {
  236. scanCode = 0;
  237. if (keyValue == 0) {
  238. return new ConsoleKeyInfo ((char)keyValue, ConsoleKey.None, modifiers.HasFlag (ConsoleModifiers.Shift),
  239. modifiers.HasFlag (ConsoleModifiers.Alt), modifiers.HasFlag (ConsoleModifiers.Control));
  240. }
  241. uint outputChar = keyValue;
  242. uint consoleKey;
  243. if (keyValue > byte.MaxValue) {
  244. var sCode = _scanCodes.FirstOrDefault ((e) => e.UnicodeChar == keyValue);
  245. if (sCode == null) {
  246. consoleKey = (byte)(keyValue & byte.MaxValue);
  247. sCode = _scanCodes.FirstOrDefault ((e) => e.VirtualKey == (VK)consoleKey);
  248. if (sCode == null) {
  249. consoleKey = 0;
  250. outputChar = keyValue;
  251. } else {
  252. outputChar = (char)(keyValue >> 8);
  253. }
  254. } else {
  255. consoleKey = (byte)sCode.VirtualKey;
  256. outputChar = keyValue;
  257. }
  258. } else {
  259. consoleKey = (byte)keyValue;
  260. outputChar = '\0';
  261. }
  262. return new ConsoleKeyInfo ((char)outputChar, (ConsoleKey)consoleKey, modifiers.HasFlag (ConsoleModifiers.Shift),
  263. modifiers.HasFlag (ConsoleModifiers.Alt), modifiers.HasFlag (ConsoleModifiers.Control));
  264. }
  265. // Used only by unit tests
  266. internal static uint GetKeyChar (uint keyValue, ConsoleModifiers modifiers)
  267. {
  268. if (modifiers == ConsoleModifiers.Shift && keyValue - 32 is >= 'A' and <= 'Z') {
  269. return keyValue - 32;
  270. } else if (modifiers == ConsoleModifiers.None && keyValue is >= 'A' and <= 'Z') {
  271. return keyValue + 32;
  272. }
  273. if (modifiers == ConsoleModifiers.Shift && keyValue - 32 is >= 'À' and <= 'Ý') {
  274. return keyValue - 32;
  275. } else if (modifiers == ConsoleModifiers.None && keyValue is >= 'À' and <= 'Ý') {
  276. return keyValue + 32;
  277. }
  278. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '0') {
  279. return keyValue + 13;
  280. } else if (modifiers == ConsoleModifiers.None && keyValue - 13 is '0') {
  281. return keyValue - 13;
  282. }
  283. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is >= '1' and <= '9' and not '7') {
  284. return keyValue - 16;
  285. } else if (modifiers == ConsoleModifiers.None && keyValue + 16 is >= '1' and <= '9' and not '7') {
  286. return keyValue + 16;
  287. }
  288. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '7') {
  289. return keyValue - 8;
  290. } else if (modifiers == ConsoleModifiers.None && keyValue + 8 is '7') {
  291. return keyValue + 8;
  292. }
  293. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '\'') {
  294. return keyValue + 24;
  295. } else if (modifiers == ConsoleModifiers.None && keyValue - 24 is '\'') {
  296. return keyValue - 24;
  297. }
  298. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '«') {
  299. return keyValue + 16;
  300. } else if (modifiers == ConsoleModifiers.None && keyValue - 16 is '«') {
  301. return keyValue - 16;
  302. }
  303. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '\\') {
  304. return keyValue + 32;
  305. } else if (modifiers == ConsoleModifiers.None && keyValue - 32 is '\\') {
  306. return keyValue - 32;
  307. }
  308. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '+') {
  309. return keyValue - 1;
  310. } else if (modifiers == ConsoleModifiers.None && keyValue + 1 is '+') {
  311. return keyValue + 1;
  312. }
  313. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '´') {
  314. return keyValue - 84;
  315. } else if (modifiers == ConsoleModifiers.None && keyValue + 84 is '´') {
  316. return keyValue + 84;
  317. }
  318. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is 'º') {
  319. return keyValue - 16;
  320. } else if (modifiers == ConsoleModifiers.None && keyValue + 16 is 'º') {
  321. return keyValue + 16;
  322. }
  323. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '~') {
  324. return keyValue - 32;
  325. } else if (modifiers == ConsoleModifiers.None && keyValue + 32 is '~') {
  326. return keyValue + 32;
  327. }
  328. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '<') {
  329. return keyValue + 2;
  330. } else if (modifiers == ConsoleModifiers.None && keyValue - 2 is '<') {
  331. return keyValue - 2;
  332. }
  333. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is ',') {
  334. return keyValue + 15;
  335. } else if (modifiers == ConsoleModifiers.None && keyValue - 15 is ',') {
  336. return keyValue - 15;
  337. }
  338. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '.') {
  339. return keyValue + 12;
  340. } else if (modifiers == ConsoleModifiers.None && keyValue - 12 is '.') {
  341. return keyValue - 12;
  342. }
  343. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '-') {
  344. return keyValue + 50;
  345. } else if (modifiers == ConsoleModifiers.None && keyValue - 50 is '-') {
  346. return keyValue - 50;
  347. }
  348. return keyValue;
  349. }
  350. /// <summary>
  351. /// Get the output character from the <see cref="GetConsoleKeyInfoFromKeyCode"/>, with the correct <see cref="ConsoleKey"/>
  352. /// and the scan code used on <see cref="WindowsDriver"/>.
  353. /// </summary>
  354. /// <param name="unicodeChar">The unicode character.</param>
  355. /// <param name="modifiers">The modifiers keys.</param>
  356. /// <param name="consoleKey">The resulting console key.</param>
  357. /// <param name="scanCode">The resulting scan code.</param>
  358. /// <param name="isConsoleKey">Indicates if the <paramref name="unicodeChar"/> is a <see cref="ConsoleKey"/>.</param>
  359. /// <returns>The output character or the <paramref name="consoleKey"/>.</returns>
  360. /// <remarks>This is only used by the <see cref="GetConsoleKeyInfoFromKeyCode"/> and by unit tests.</remarks>
  361. internal static uint GetKeyCharFromUnicodeChar (uint unicodeChar, ConsoleModifiers modifiers, out uint consoleKey, out uint scanCode, bool isConsoleKey = false)
  362. {
  363. uint decodedChar = unicodeChar >> 8 == 0xff ? unicodeChar & 0xff : unicodeChar;
  364. uint keyChar = decodedChar;
  365. consoleKey = 0;
  366. var mod = GetModifiers (modifiers);
  367. scanCode = 0;
  368. ScanCodeMapping scode = null;
  369. if (unicodeChar != 0 && unicodeChar >> 8 != 0xff && isConsoleKey) {
  370. scode = GetScanCode ("VirtualKey", decodedChar, mod);
  371. }
  372. if (isConsoleKey && scode != null) {
  373. consoleKey = (uint)scode.VirtualKey;
  374. keyChar = scode.UnicodeChar;
  375. scanCode = scode.ScanCode;
  376. }
  377. if (scode == null) {
  378. scode = unicodeChar != 0 ? GetScanCode ("UnicodeChar", decodedChar, mod) : null;
  379. if (scode != null) {
  380. consoleKey = (uint)scode.VirtualKey;
  381. keyChar = scode.UnicodeChar;
  382. scanCode = scode.ScanCode;
  383. }
  384. }
  385. if (decodedChar != 0 && scanCode == 0 && char.IsLetter ((char)decodedChar)) {
  386. string stFormD = ((char)decodedChar).ToString ().Normalize (System.Text.NormalizationForm.FormD);
  387. for (int i = 0; i < stFormD.Length; i++) {
  388. var uc = CharUnicodeInfo.GetUnicodeCategory (stFormD [i]);
  389. if (uc != UnicodeCategory.NonSpacingMark && uc != UnicodeCategory.OtherLetter) {
  390. consoleKey = char.ToUpper (stFormD [i]);
  391. scode = GetScanCode ("VirtualKey", char.ToUpper (stFormD [i]), 0);
  392. if (scode != null) {
  393. scanCode = scode.ScanCode;
  394. }
  395. }
  396. }
  397. }
  398. if (keyChar < 255 && consoleKey == 0 && scanCode == 0) {
  399. scode = GetScanCode ("VirtualKey", keyChar, mod);
  400. if (scode != null) {
  401. consoleKey = (uint)scode.VirtualKey;
  402. keyChar = scode.UnicodeChar;
  403. scanCode = scode.ScanCode;
  404. }
  405. }
  406. return keyChar;
  407. }
  408. /// <summary>
  409. /// Maps a unicode character (e.g. (Key)'a') to a uint representing a <see cref="ConsoleKey"/>.
  410. /// </summary>
  411. /// <param name="keyValue">The key value.</param>
  412. /// <param name="isConsoleKey">Indicates if the <paramref name="keyValue"/> is a <see cref="ConsoleKey"/>.
  413. /// <see langword="true"/> means the return value is in the ConsoleKey enum.
  414. /// <see langword="false"/> means the return value can be mapped to a valid unicode character.
  415. /// </param>
  416. /// <returns>The <see cref="ConsoleKey"/> or the <paramref name="keyValue"/>.</returns>
  417. /// <remarks>This is only used by the <see cref="GetConsoleKeyInfoFromKeyCode"/> and by unit tests.</remarks>
  418. internal static uint MapKeyCodeToConsoleKey (KeyCode keyValue, out bool isConsoleKey)
  419. {
  420. isConsoleKey = true;
  421. keyValue = keyValue & ~KeyCode.CtrlMask & ~KeyCode.ShiftMask & ~KeyCode.AltMask;
  422. switch (keyValue) {
  423. case KeyCode.Enter:
  424. return (uint)ConsoleKey.Enter;
  425. case KeyCode.CursorUp:
  426. return (uint)ConsoleKey.UpArrow;
  427. case KeyCode.CursorDown:
  428. return (uint)ConsoleKey.DownArrow;
  429. case KeyCode.CursorLeft:
  430. return (uint)ConsoleKey.LeftArrow;
  431. case KeyCode.CursorRight:
  432. return (uint)ConsoleKey.RightArrow;
  433. case KeyCode.PageUp:
  434. return (uint)ConsoleKey.PageUp;
  435. case KeyCode.PageDown:
  436. return (uint)ConsoleKey.PageDown;
  437. case KeyCode.Home:
  438. return (uint)ConsoleKey.Home;
  439. case KeyCode.End:
  440. return (uint)ConsoleKey.End;
  441. case KeyCode.Insert:
  442. return (uint)ConsoleKey.Insert;
  443. case KeyCode.Delete:
  444. return (uint)ConsoleKey.Delete;
  445. case KeyCode.F1:
  446. return (uint)ConsoleKey.F1;
  447. case KeyCode.F2:
  448. return (uint)ConsoleKey.F2;
  449. case KeyCode.F3:
  450. return (uint)ConsoleKey.F3;
  451. case KeyCode.F4:
  452. return (uint)ConsoleKey.F4;
  453. case KeyCode.F5:
  454. return (uint)ConsoleKey.F5;
  455. case KeyCode.F6:
  456. return (uint)ConsoleKey.F6;
  457. case KeyCode.F7:
  458. return (uint)ConsoleKey.F7;
  459. case KeyCode.F8:
  460. return (uint)ConsoleKey.F8;
  461. case KeyCode.F9:
  462. return (uint)ConsoleKey.F9;
  463. case KeyCode.F10:
  464. return (uint)ConsoleKey.F10;
  465. case KeyCode.F11:
  466. return (uint)ConsoleKey.F11;
  467. case KeyCode.F12:
  468. return (uint)ConsoleKey.F12;
  469. case KeyCode.F13:
  470. return (uint)ConsoleKey.F13;
  471. case KeyCode.F14:
  472. return (uint)ConsoleKey.F14;
  473. case KeyCode.F15:
  474. return (uint)ConsoleKey.F15;
  475. case KeyCode.F16:
  476. return (uint)ConsoleKey.F16;
  477. case KeyCode.F17:
  478. return (uint)ConsoleKey.F17;
  479. case KeyCode.F18:
  480. return (uint)ConsoleKey.F18;
  481. case KeyCode.F19:
  482. return (uint)ConsoleKey.F19;
  483. case KeyCode.F20:
  484. return (uint)ConsoleKey.F20;
  485. case KeyCode.F21:
  486. return (uint)ConsoleKey.F21;
  487. case KeyCode.F22:
  488. return (uint)ConsoleKey.F22;
  489. case KeyCode.F23:
  490. return (uint)ConsoleKey.F23;
  491. case KeyCode.F24:
  492. return (uint)ConsoleKey.F24;
  493. case KeyCode.Tab | KeyCode.ShiftMask:
  494. return (uint)ConsoleKey.Tab;
  495. }
  496. isConsoleKey = false;
  497. return (uint)keyValue;
  498. }
  499. /// <summary>
  500. /// Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.
  501. /// </summary>
  502. /// <param name="consoleKeyInfo">The console key.</param>
  503. /// <returns>The <see cref="KeyCode"/> or the <paramref name="consoleKeyInfo"/>.</returns>
  504. public static KeyCode MapConsoleKeyInfoToKeyCode (ConsoleKeyInfo consoleKeyInfo)
  505. {
  506. KeyCode keyCode;
  507. switch (consoleKeyInfo.Key) {
  508. case ConsoleKey.Enter:
  509. keyCode = KeyCode.Enter;
  510. break;
  511. case ConsoleKey.Delete:
  512. keyCode = KeyCode.Delete;
  513. break;
  514. case ConsoleKey.UpArrow:
  515. keyCode = KeyCode.CursorUp;
  516. break;
  517. case ConsoleKey.DownArrow:
  518. keyCode = KeyCode.CursorDown;
  519. break;
  520. case ConsoleKey.LeftArrow:
  521. keyCode = KeyCode.CursorLeft;
  522. break;
  523. case ConsoleKey.RightArrow:
  524. keyCode = KeyCode.CursorRight;
  525. break;
  526. case ConsoleKey.PageUp:
  527. keyCode = KeyCode.PageUp;
  528. break;
  529. case ConsoleKey.PageDown:
  530. keyCode = KeyCode.PageDown;
  531. break;
  532. case ConsoleKey.Home:
  533. keyCode = KeyCode.Home;
  534. break;
  535. case ConsoleKey.End:
  536. keyCode = KeyCode.End;
  537. break;
  538. case ConsoleKey.Insert:
  539. keyCode = KeyCode.Insert;
  540. break;
  541. case ConsoleKey.F1:
  542. keyCode = KeyCode.F1;
  543. break;
  544. case ConsoleKey.F2:
  545. keyCode = KeyCode.F2;
  546. break;
  547. case ConsoleKey.F3:
  548. keyCode = KeyCode.F3;
  549. break;
  550. case ConsoleKey.F4:
  551. keyCode = KeyCode.F4;
  552. break;
  553. case ConsoleKey.F5:
  554. keyCode = KeyCode.F5;
  555. break;
  556. case ConsoleKey.F6:
  557. keyCode = KeyCode.F6;
  558. break;
  559. case ConsoleKey.F7:
  560. keyCode = KeyCode.F7;
  561. break;
  562. case ConsoleKey.F8:
  563. keyCode = KeyCode.F8;
  564. break;
  565. case ConsoleKey.F9:
  566. keyCode = KeyCode.F9;
  567. break;
  568. case ConsoleKey.F10:
  569. keyCode = KeyCode.F10;
  570. break;
  571. case ConsoleKey.F11:
  572. keyCode = KeyCode.F11;
  573. break;
  574. case ConsoleKey.F12:
  575. keyCode = KeyCode.F12;
  576. break;
  577. case ConsoleKey.F13:
  578. keyCode = KeyCode.F13;
  579. break;
  580. case ConsoleKey.F14:
  581. keyCode = KeyCode.F14;
  582. break;
  583. case ConsoleKey.F15:
  584. keyCode = KeyCode.F15;
  585. break;
  586. case ConsoleKey.F16:
  587. keyCode = KeyCode.F16;
  588. break;
  589. case ConsoleKey.F17:
  590. keyCode = KeyCode.F17;
  591. break;
  592. case ConsoleKey.F18:
  593. keyCode = KeyCode.F18;
  594. break;
  595. case ConsoleKey.F19:
  596. keyCode = KeyCode.F19;
  597. break;
  598. case ConsoleKey.F20:
  599. keyCode = KeyCode.F20;
  600. break;
  601. case ConsoleKey.F21:
  602. keyCode = KeyCode.F21;
  603. break;
  604. case ConsoleKey.F22:
  605. keyCode = KeyCode.F22;
  606. break;
  607. case ConsoleKey.F23:
  608. keyCode = KeyCode.F23;
  609. break;
  610. case ConsoleKey.F24:
  611. keyCode = KeyCode.F24;
  612. break;
  613. case ConsoleKey.Tab:
  614. keyCode = KeyCode.Tab;
  615. break;
  616. default:
  617. keyCode = (KeyCode)consoleKeyInfo.KeyChar;
  618. break;
  619. }
  620. keyCode |= MapToKeyCodeModifiers (consoleKeyInfo.Modifiers, keyCode);
  621. return keyCode;
  622. }
  623. /// <summary>
  624. /// Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.
  625. /// </summary>
  626. /// <param name="modifiers">The console modifiers.</param>
  627. /// <param name="key">The key code.</param>
  628. /// <returns>The <see cref="KeyCode"/> with <see cref="ConsoleModifiers"/> or the <paramref name="key"/></returns>
  629. public static KeyCode MapToKeyCodeModifiers (ConsoleModifiers modifiers, KeyCode key)
  630. {
  631. var keyMod = new KeyCode ();
  632. if ((modifiers & ConsoleModifiers.Shift) != 0) {
  633. keyMod = KeyCode.ShiftMask;
  634. }
  635. if ((modifiers & ConsoleModifiers.Control) != 0) {
  636. keyMod |= KeyCode.CtrlMask;
  637. }
  638. if ((modifiers & ConsoleModifiers.Alt) != 0) {
  639. keyMod |= KeyCode.AltMask;
  640. }
  641. return keyMod != KeyCode.Null ? keyMod | key : key;
  642. }
  643. /// <summary>
  644. /// Generated from winuser.h. See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
  645. /// </summary>
  646. public enum VK : ushort {
  647. /// <summary>
  648. /// Left mouse button.
  649. /// </summary>
  650. LBUTTON = 0x01,
  651. /// <summary>
  652. /// Right mouse button.
  653. /// </summary>
  654. RBUTTON = 0x02,
  655. /// <summary>
  656. /// Control-break processing.
  657. /// </summary>
  658. CANCEL = 0x03,
  659. /// <summary>
  660. /// Middle mouse button (three-button mouse).
  661. /// </summary>
  662. MBUTTON = 0x04,
  663. /// <summary>
  664. /// X1 mouse button.
  665. /// </summary>
  666. XBUTTON1 = 0x05,
  667. /// <summary>
  668. /// X2 mouse button.
  669. /// </summary>
  670. XBUTTON2 = 0x06,
  671. /// <summary>
  672. /// BACKSPACE key.
  673. /// </summary>
  674. BACK = 0x08,
  675. /// <summary>
  676. /// TAB key.
  677. /// </summary>
  678. TAB = 0x09,
  679. /// <summary>
  680. /// CLEAR key.
  681. /// </summary>
  682. CLEAR = 0x0C,
  683. /// <summary>
  684. /// ENTER key.
  685. /// </summary>
  686. RETURN = 0x0D,
  687. /// <summary>
  688. /// SHIFT key.
  689. /// </summary>
  690. SHIFT = 0x10,
  691. /// <summary>
  692. /// CTRL key.
  693. /// </summary>
  694. CONTROL = 0x11,
  695. /// <summary>
  696. /// ALT key.
  697. /// </summary>
  698. MENU = 0x12,
  699. /// <summary>
  700. /// PAUSE key.
  701. /// </summary>
  702. PAUSE = 0x13,
  703. /// <summary>
  704. /// CAPS LOCK key.
  705. /// </summary>
  706. CAPITAL = 0x14,
  707. /// <summary>
  708. /// IME Kana mode.
  709. /// </summary>
  710. KANA = 0x15,
  711. /// <summary>
  712. /// IME Hangul mode.
  713. /// </summary>
  714. HANGUL = 0x15,
  715. /// <summary>
  716. /// IME Junja mode.
  717. /// </summary>
  718. JUNJA = 0x17,
  719. /// <summary>
  720. /// IME final mode.
  721. /// </summary>
  722. FINAL = 0x18,
  723. /// <summary>
  724. /// IME Hanja mode.
  725. /// </summary>
  726. HANJA = 0x19,
  727. /// <summary>
  728. /// IME Kanji mode.
  729. /// </summary>
  730. KANJI = 0x19,
  731. /// <summary>
  732. /// ESC key.
  733. /// </summary>
  734. ESCAPE = 0x1B,
  735. /// <summary>
  736. /// IME convert.
  737. /// </summary>
  738. CONVERT = 0x1C,
  739. /// <summary>
  740. /// IME nonconvert.
  741. /// </summary>
  742. NONCONVERT = 0x1D,
  743. /// <summary>
  744. /// IME accept.
  745. /// </summary>
  746. ACCEPT = 0x1E,
  747. /// <summary>
  748. /// IME mode change request.
  749. /// </summary>
  750. MODECHANGE = 0x1F,
  751. /// <summary>
  752. /// SPACEBAR.
  753. /// </summary>
  754. SPACE = 0x20,
  755. /// <summary>
  756. /// PAGE UP key.
  757. /// </summary>
  758. PRIOR = 0x21,
  759. /// <summary>
  760. /// PAGE DOWN key.
  761. /// </summary>
  762. NEXT = 0x22,
  763. /// <summary>
  764. /// END key.
  765. /// </summary>
  766. END = 0x23,
  767. /// <summary>
  768. /// HOME key.
  769. /// </summary>
  770. HOME = 0x24,
  771. /// <summary>
  772. /// LEFT ARROW key.
  773. /// </summary>
  774. LEFT = 0x25,
  775. /// <summary>
  776. /// UP ARROW key.
  777. /// </summary>
  778. UP = 0x26,
  779. /// <summary>
  780. /// RIGHT ARROW key.
  781. /// </summary>
  782. RIGHT = 0x27,
  783. /// <summary>
  784. /// DOWN ARROW key.
  785. /// </summary>
  786. DOWN = 0x28,
  787. /// <summary>
  788. /// SELECT key.
  789. /// </summary>
  790. SELECT = 0x29,
  791. /// <summary>
  792. /// PRINT key.
  793. /// </summary>
  794. PRINT = 0x2A,
  795. /// <summary>
  796. /// EXECUTE key
  797. /// </summary>
  798. EXECUTE = 0x2B,
  799. /// <summary>
  800. /// PRINT SCREEN key
  801. /// </summary>
  802. SNAPSHOT = 0x2C,
  803. /// <summary>
  804. /// INS key
  805. /// </summary>
  806. INSERT = 0x2D,
  807. /// <summary>
  808. /// DEL key
  809. /// </summary>
  810. DELETE = 0x2E,
  811. /// <summary>
  812. /// HELP key
  813. /// </summary>
  814. HELP = 0x2F,
  815. /// <summary>
  816. /// Left Windows key (Natural keyboard)
  817. /// </summary>
  818. LWIN = 0x5B,
  819. /// <summary>
  820. /// Right Windows key (Natural keyboard)
  821. /// </summary>
  822. RWIN = 0x5C,
  823. /// <summary>
  824. /// Applications key (Natural keyboard)
  825. /// </summary>
  826. APPS = 0x5D,
  827. /// <summary>
  828. /// Computer Sleep key
  829. /// </summary>
  830. SLEEP = 0x5F,
  831. /// <summary>
  832. /// Numeric keypad 0 key
  833. /// </summary>
  834. NUMPAD0 = 0x60,
  835. /// <summary>
  836. /// Numeric keypad 1 key
  837. /// </summary>
  838. NUMPAD1 = 0x61,
  839. /// <summary>
  840. /// Numeric keypad 2 key
  841. /// </summary>
  842. NUMPAD2 = 0x62,
  843. /// <summary>
  844. /// Numeric keypad 3 key
  845. /// </summary>
  846. NUMPAD3 = 0x63,
  847. /// <summary>
  848. /// Numeric keypad 4 key
  849. /// </summary>
  850. NUMPAD4 = 0x64,
  851. /// <summary>
  852. /// Numeric keypad 5 key
  853. /// </summary>
  854. NUMPAD5 = 0x65,
  855. /// <summary>
  856. /// Numeric keypad 6 key
  857. /// </summary>
  858. NUMPAD6 = 0x66,
  859. /// <summary>
  860. /// Numeric keypad 7 key
  861. /// </summary>
  862. NUMPAD7 = 0x67,
  863. /// <summary>
  864. /// Numeric keypad 8 key
  865. /// </summary>
  866. NUMPAD8 = 0x68,
  867. /// <summary>
  868. /// Numeric keypad 9 key
  869. /// </summary>
  870. NUMPAD9 = 0x69,
  871. /// <summary>
  872. /// Multiply key
  873. /// </summary>
  874. MULTIPLY = 0x6A,
  875. /// <summary>
  876. /// Add key
  877. /// </summary>
  878. ADD = 0x6B,
  879. /// <summary>
  880. /// Separator key
  881. /// </summary>
  882. SEPARATOR = 0x6C,
  883. /// <summary>
  884. /// Subtract key
  885. /// </summary>
  886. SUBTRACT = 0x6D,
  887. /// <summary>
  888. /// Decimal key
  889. /// </summary>
  890. DECIMAL = 0x6E,
  891. /// <summary>
  892. /// Divide key
  893. /// </summary>
  894. DIVIDE = 0x6F,
  895. /// <summary>
  896. /// F1 key
  897. /// </summary>
  898. F1 = 0x70,
  899. /// <summary>
  900. /// F2 key
  901. /// </summary>
  902. F2 = 0x71,
  903. /// <summary>
  904. /// F3 key
  905. /// </summary>
  906. F3 = 0x72,
  907. /// <summary>
  908. /// F4 key
  909. /// </summary>
  910. F4 = 0x73,
  911. /// <summary>
  912. /// F5 key
  913. /// </summary>
  914. F5 = 0x74,
  915. /// <summary>
  916. /// F6 key
  917. /// </summary>
  918. F6 = 0x75,
  919. /// <summary>
  920. /// F7 key
  921. /// </summary>
  922. F7 = 0x76,
  923. /// <summary>
  924. /// F8 key
  925. /// </summary>
  926. F8 = 0x77,
  927. /// <summary>
  928. /// F9 key
  929. /// </summary>
  930. F9 = 0x78,
  931. /// <summary>
  932. /// F10 key
  933. /// </summary>
  934. F10 = 0x79,
  935. /// <summary>
  936. /// F11 key
  937. /// </summary>
  938. F11 = 0x7A,
  939. /// <summary>
  940. /// F12 key
  941. /// </summary>
  942. F12 = 0x7B,
  943. /// <summary>
  944. /// F13 key
  945. /// </summary>
  946. F13 = 0x7C,
  947. /// <summary>
  948. /// F14 key
  949. /// </summary>
  950. F14 = 0x7D,
  951. /// <summary>
  952. /// F15 key
  953. /// </summary>
  954. F15 = 0x7E,
  955. /// <summary>
  956. /// F16 key
  957. /// </summary>
  958. F16 = 0x7F,
  959. /// <summary>
  960. /// F17 key
  961. /// </summary>
  962. F17 = 0x80,
  963. /// <summary>
  964. /// F18 key
  965. /// </summary>
  966. F18 = 0x81,
  967. /// <summary>
  968. /// F19 key
  969. /// </summary>
  970. F19 = 0x82,
  971. /// <summary>
  972. /// F20 key
  973. /// </summary>
  974. F20 = 0x83,
  975. /// <summary>
  976. /// F21 key
  977. /// </summary>
  978. F21 = 0x84,
  979. /// <summary>
  980. /// F22 key
  981. /// </summary>
  982. F22 = 0x85,
  983. /// <summary>
  984. /// F23 key
  985. /// </summary>
  986. F23 = 0x86,
  987. /// <summary>
  988. /// F24 key
  989. /// </summary>
  990. F24 = 0x87,
  991. /// <summary>
  992. /// NUM LOCK key
  993. /// </summary>
  994. NUMLOCK = 0x90,
  995. /// <summary>
  996. /// SCROLL LOCK key
  997. /// </summary>
  998. SCROLL = 0x91,
  999. /// <summary>
  1000. /// NEC PC-9800 kbd definition: '=' key on numpad
  1001. /// </summary>
  1002. OEM_NEC_EQUAL = 0x92,
  1003. /// <summary>
  1004. /// Fujitsu/OASYS kbd definition: 'Dictionary' key
  1005. /// </summary>
  1006. OEM_FJ_JISHO = 0x92,
  1007. /// <summary>
  1008. /// Fujitsu/OASYS kbd definition: 'Unregister word' key
  1009. /// </summary>
  1010. OEM_FJ_MASSHOU = 0x93,
  1011. /// <summary>
  1012. /// Fujitsu/OASYS kbd definition: 'Register word' key
  1013. /// </summary>
  1014. OEM_FJ_TOUROKU = 0x94,
  1015. /// <summary>
  1016. /// Fujitsu/OASYS kbd definition: 'Left OYAYUBI' key
  1017. /// </summary>
  1018. OEM_FJ_LOYA = 0x95,
  1019. /// <summary>
  1020. /// Fujitsu/OASYS kbd definition: 'Right OYAYUBI' key
  1021. /// </summary>
  1022. OEM_FJ_ROYA = 0x96,
  1023. /// <summary>
  1024. /// Left SHIFT key
  1025. /// </summary>
  1026. LSHIFT = 0xA0,
  1027. /// <summary>
  1028. /// Right SHIFT key
  1029. /// </summary>
  1030. RSHIFT = 0xA1,
  1031. /// <summary>
  1032. /// Left CONTROL key
  1033. /// </summary>
  1034. LCONTROL = 0xA2,
  1035. /// <summary>
  1036. /// Right CONTROL key
  1037. /// </summary>
  1038. RCONTROL = 0xA3,
  1039. /// <summary>
  1040. /// Left MENU key (Left Alt key)
  1041. /// </summary>
  1042. LMENU = 0xA4,
  1043. /// <summary>
  1044. /// Right MENU key (Right Alt key)
  1045. /// </summary>
  1046. RMENU = 0xA5,
  1047. /// <summary>
  1048. /// Browser Back key
  1049. /// </summary>
  1050. BROWSER_BACK = 0xA6,
  1051. /// <summary>
  1052. /// Browser Forward key
  1053. /// </summary>
  1054. BROWSER_FORWARD = 0xA7,
  1055. /// <summary>
  1056. /// Browser Refresh key
  1057. /// </summary>
  1058. BROWSER_REFRESH = 0xA8,
  1059. /// <summary>
  1060. /// Browser Stop key
  1061. /// </summary>
  1062. BROWSER_STOP = 0xA9,
  1063. /// <summary>
  1064. /// Browser Search key
  1065. /// </summary>
  1066. BROWSER_SEARCH = 0xAA,
  1067. /// <summary>
  1068. /// Browser Favorites key
  1069. /// </summary>
  1070. BROWSER_FAVORITES = 0xAB,
  1071. /// <summary>
  1072. /// Browser Home key
  1073. /// </summary>
  1074. BROWSER_HOME = 0xAC,
  1075. /// <summary>
  1076. /// Volume Mute key
  1077. /// </summary>
  1078. VOLUME_MUTE = 0xAD,
  1079. /// <summary>
  1080. /// Volume Down key
  1081. /// </summary>
  1082. VOLUME_DOWN = 0xAE,
  1083. /// <summary>
  1084. /// Volume Up key
  1085. /// </summary>
  1086. VOLUME_UP = 0xAF,
  1087. /// <summary>
  1088. /// Next Track key
  1089. /// </summary>
  1090. MEDIA_NEXT_TRACK = 0xB0,
  1091. /// <summary>
  1092. /// Previous Track key
  1093. /// </summary>
  1094. MEDIA_PREV_TRACK = 0xB1,
  1095. /// <summary>
  1096. /// Stop Media key
  1097. /// </summary>
  1098. MEDIA_STOP = 0xB2,
  1099. /// <summary>
  1100. /// Play/Pause Media key
  1101. /// </summary>
  1102. MEDIA_PLAY_PAUSE = 0xB3,
  1103. /// <summary>
  1104. /// Start Mail key
  1105. /// </summary>
  1106. LAUNCH_MAIL = 0xB4,
  1107. /// <summary>
  1108. /// Select Media key
  1109. /// </summary>
  1110. LAUNCH_MEDIA_SELECT = 0xB5,
  1111. /// <summary>
  1112. /// Start Application 1 key
  1113. /// </summary>
  1114. LAUNCH_APP1 = 0xB6,
  1115. /// <summary>
  1116. /// Start Application 2 key
  1117. /// </summary>
  1118. LAUNCH_APP2 = 0xB7,
  1119. /// <summary>
  1120. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key
  1121. /// </summary>
  1122. OEM_1 = 0xBA,
  1123. /// <summary>
  1124. /// For any country/region, the '+' key
  1125. /// </summary>
  1126. OEM_PLUS = 0xBB,
  1127. /// <summary>
  1128. /// For any country/region, the ',' key
  1129. /// </summary>
  1130. OEM_COMMA = 0xBC,
  1131. /// <summary>
  1132. /// For any country/region, the '-' key
  1133. /// </summary>
  1134. OEM_MINUS = 0xBD,
  1135. /// <summary>
  1136. /// For any country/region, the '.' key
  1137. /// </summary>
  1138. OEM_PERIOD = 0xBE,
  1139. /// <summary>
  1140. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key
  1141. /// </summary>
  1142. OEM_2 = 0xBF,
  1143. /// <summary>
  1144. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key
  1145. /// </summary>
  1146. OEM_3 = 0xC0,
  1147. /// <summary>
  1148. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key
  1149. /// </summary>
  1150. OEM_4 = 0xDB,
  1151. /// <summary>
  1152. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key
  1153. /// </summary>
  1154. OEM_5 = 0xDC,
  1155. /// <summary>
  1156. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key
  1157. /// </summary>
  1158. OEM_6 = 0xDD,
  1159. /// <summary>
  1160. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the 'single-quote/double-quote' key
  1161. /// </summary>
  1162. OEM_7 = 0xDE,
  1163. /// <summary>
  1164. /// Used for miscellaneous characters; it can vary by keyboard.
  1165. /// </summary>
  1166. OEM_8 = 0xDF,
  1167. /// <summary>
  1168. /// 'AX' key on Japanese AX kbd
  1169. /// </summary>
  1170. OEM_AX = 0xE1,
  1171. /// <summary>
  1172. /// Either the angle bracket key or the backslash key on the RT 102-key keyboard
  1173. /// </summary>
  1174. OEM_102 = 0xE2,
  1175. /// <summary>
  1176. /// Help key on ICO
  1177. /// </summary>
  1178. ICO_HELP = 0xE3,
  1179. /// <summary>
  1180. /// 00 key on ICO
  1181. /// </summary>
  1182. ICO_00 = 0xE4,
  1183. /// <summary>
  1184. /// Process key
  1185. /// </summary>
  1186. PROCESSKEY = 0xE5,
  1187. /// <summary>
  1188. /// Clear key on ICO
  1189. /// </summary>
  1190. ICO_CLEAR = 0xE6,
  1191. /// <summary>
  1192. /// Packet key to be used to pass Unicode characters as if they were keystrokes
  1193. /// </summary>
  1194. PACKET = 0xE7,
  1195. /// <summary>
  1196. /// Reset key
  1197. /// </summary>
  1198. OEM_RESET = 0xE9,
  1199. /// <summary>
  1200. /// Jump key
  1201. /// </summary>
  1202. OEM_JUMP = 0xEA,
  1203. /// <summary>
  1204. /// PA1 key
  1205. /// </summary>
  1206. OEM_PA1 = 0xEB,
  1207. /// <summary>
  1208. /// PA2 key
  1209. /// </summary>
  1210. OEM_PA2 = 0xEC,
  1211. /// <summary>
  1212. /// PA3 key
  1213. /// </summary>
  1214. OEM_PA3 = 0xED,
  1215. /// <summary>
  1216. /// WsCtrl key
  1217. /// </summary>
  1218. OEM_WSCTRL = 0xEE,
  1219. /// <summary>
  1220. /// CuSel key
  1221. /// </summary>
  1222. OEM_CUSEL = 0xEF,
  1223. /// <summary>
  1224. /// Attn key
  1225. /// </summary>
  1226. OEM_ATTN = 0xF0,
  1227. /// <summary>
  1228. /// Finish key
  1229. /// </summary>
  1230. OEM_FINISH = 0xF1,
  1231. /// <summary>
  1232. /// Copy key
  1233. /// </summary>
  1234. OEM_COPY = 0xF2,
  1235. /// <summary>
  1236. /// Auto key
  1237. /// </summary>
  1238. OEM_AUTO = 0xF3,
  1239. /// <summary>
  1240. /// Enlw key
  1241. /// </summary>
  1242. OEM_ENLW = 0xF4,
  1243. /// <summary>
  1244. /// BackTab key
  1245. /// </summary>
  1246. OEM_BACKTAB = 0xF5,
  1247. /// <summary>
  1248. /// Attn key
  1249. /// </summary>
  1250. ATTN = 0xF6,
  1251. /// <summary>
  1252. /// CrSel key
  1253. /// </summary>
  1254. CRSEL = 0xF7,
  1255. /// <summary>
  1256. /// ExSel key
  1257. /// </summary>
  1258. EXSEL = 0xF8,
  1259. /// <summary>
  1260. /// Erase EOF key
  1261. /// </summary>
  1262. EREOF = 0xF9,
  1263. /// <summary>
  1264. /// Play key
  1265. /// </summary>
  1266. PLAY = 0xFA,
  1267. /// <summary>
  1268. /// Zoom key
  1269. /// </summary>
  1270. ZOOM = 0xFB,
  1271. /// <summary>
  1272. /// Reserved
  1273. /// </summary>
  1274. NONAME = 0xFC,
  1275. /// <summary>
  1276. /// PA1 key
  1277. /// </summary>
  1278. PA1 = 0xFD,
  1279. /// <summary>
  1280. /// Clear key
  1281. /// </summary>
  1282. OEM_CLEAR = 0xFE
  1283. }
  1284. // BUGBUG: This database makes no sense. It is not possible to map a VK code to a character without knowing the keyboard layout
  1285. // It should be deleted.
  1286. static HashSet<ScanCodeMapping> _scanCodes = new HashSet<ScanCodeMapping> {
  1287. new (1, VK.ESCAPE, 0, '\u001B'), // Escape
  1288. new (1, VK.ESCAPE, ConsoleModifiers.Shift, '\u001B'),
  1289. new (2, (VK)'1', 0, '1'), // D1
  1290. new (2, (VK)'1', ConsoleModifiers.Shift, '!'),
  1291. new (3, (VK)'2', 0, '2'), // D2
  1292. new (3, (VK)'2', ConsoleModifiers.Shift, '\"'), // BUGBUG: This is true for Portugese keyboard, but not ENG (@) or DEU (")
  1293. new (3, (VK)'2', ConsoleModifiers.Alt | ConsoleModifiers.Control, '@'),
  1294. new (4, (VK)'3', 0, '3'), // D3
  1295. new (4, (VK)'3', ConsoleModifiers.Shift, '#'),
  1296. new (4, (VK)'3', ConsoleModifiers.Alt | ConsoleModifiers.Control, '£'),
  1297. new (5, (VK)'4', 0, '4'), // D4
  1298. new (5, (VK)'4', ConsoleModifiers.Shift, '$'),
  1299. new (5, (VK)'4', ConsoleModifiers.Alt | ConsoleModifiers.Control, '§'),
  1300. new (6, (VK)'5', 0, '5'), // D5
  1301. new (6, (VK)'5', ConsoleModifiers.Shift, '%'),
  1302. new (6, (VK)'5', ConsoleModifiers.Alt | ConsoleModifiers.Control, '€'),
  1303. new (7, (VK)'6', 0, '6'), // D6
  1304. new (7, (VK)'6', ConsoleModifiers.Shift, '&'),
  1305. new (8, (VK)'7', 0, '7'), // D7
  1306. new (8, (VK)'7', ConsoleModifiers.Shift, '/'),
  1307. new (8, (VK)'7', ConsoleModifiers.Alt | ConsoleModifiers.Control, '{'),
  1308. new (9, (VK)'8', 0, '8'), // D8
  1309. new (9, (VK)'8', ConsoleModifiers.Shift, '('),
  1310. new (9, (VK)'8', ConsoleModifiers.Alt | ConsoleModifiers.Control, '['),
  1311. new (10, (VK)'9', 0, '9'), // D9
  1312. new (10, (VK)'9', ConsoleModifiers.Shift, ')'),
  1313. new (10, (VK)'9', ConsoleModifiers.Alt | ConsoleModifiers.Control, ']'),
  1314. new (11, (VK)'0', 0, '0'), // D0
  1315. new (11, (VK)'0', ConsoleModifiers.Shift, '='),
  1316. new (11, (VK)'0', ConsoleModifiers.Alt | ConsoleModifiers.Control, '}'),
  1317. new (12, VK.OEM_4, 0, '\''), // Oem4
  1318. new (12, VK.OEM_4, ConsoleModifiers.Shift, '?'),
  1319. new (13, VK.OEM_6, 0, '+'), // Oem6
  1320. new (13, VK.OEM_6, ConsoleModifiers.Shift, '*'),
  1321. new (14, VK.BACK, 0, '\u0008'), // Backspace
  1322. new (14, VK.BACK, ConsoleModifiers.Shift, '\u0008'),
  1323. new (15, VK.TAB, 0, '\u0009'), // Tab
  1324. new (15, VK.TAB, ConsoleModifiers.Shift, '\u000F'),
  1325. new (16, (VK)'Q', 0, 'q'), // Q
  1326. new (16, (VK)'Q', ConsoleModifiers.Shift, 'Q'),
  1327. new (17, (VK)'W', 0, 'w'), // W
  1328. new (17, (VK)'W', ConsoleModifiers.Shift, 'W'),
  1329. new (18, (VK)'E', 0, 'e'), // E
  1330. new (18, (VK)'E', ConsoleModifiers.Shift, 'E'),
  1331. new (19, (VK)'R', 0, 'r'), // R
  1332. new (19, (VK)'R', ConsoleModifiers.Shift, 'R'),
  1333. new (20, (VK)'T', 0, 't'), // T
  1334. new (20, (VK)'T', ConsoleModifiers.Shift, 'T'),
  1335. new (21, (VK)'Y', 0, 'y'), // Y
  1336. new (21, (VK)'Y', ConsoleModifiers.Shift, 'Y'),
  1337. new (22, (VK)'U', 0, 'u'), // U
  1338. new (22, (VK)'U', ConsoleModifiers.Shift, 'U'),
  1339. new (23, (VK)'I', 0, 'i'), // I
  1340. new (23, (VK)'I', ConsoleModifiers.Shift, 'I'),
  1341. new (24, (VK)'O', 0, 'o'), // O
  1342. new (24, (VK)'O', ConsoleModifiers.Shift, 'O'),
  1343. new (25, (VK)'P', 0, 'p'), // P
  1344. new (25, (VK)'P', ConsoleModifiers.Shift, 'P'),
  1345. new (26, VK.OEM_PLUS, 0, '+'), // OemPlus
  1346. new (26, VK.OEM_PLUS, ConsoleModifiers.Shift, '*'),
  1347. new (26, VK.OEM_PLUS, ConsoleModifiers.Alt | ConsoleModifiers.Control, '¨'),
  1348. new (27, VK.OEM_1, 0, '´'), // Oem1
  1349. new (27, VK.OEM_1, ConsoleModifiers.Shift, '`'),
  1350. new (28, VK.RETURN, 0, '\u000D'), // Enter
  1351. new (28, VK.RETURN, ConsoleModifiers.Shift, '\u000D'),
  1352. new (29, VK.CONTROL, 0, '\0'), // Control
  1353. new (29, VK.CONTROL, ConsoleModifiers.Shift, '\0'),
  1354. new (30, (VK)'A', 0, 'a'), // A
  1355. new (30, (VK)'A', ConsoleModifiers.Shift, 'A'),
  1356. new (31, (VK)'S', 0, 's'), // S
  1357. new (31, (VK)'S', ConsoleModifiers.Shift, 'S'),
  1358. new (32, (VK)'D', 0, 'd'), // D
  1359. new (32, (VK)'D', ConsoleModifiers.Shift, 'D'),
  1360. new (33, (VK)'F', 0, 'f'), // F
  1361. new (33, (VK)'F', ConsoleModifiers.Shift, 'F'),
  1362. new (34, (VK)'G', 0, 'g'), // G
  1363. new (34, (VK)'G', ConsoleModifiers.Shift, 'G'),
  1364. new (35, (VK)'H', 0, 'h'), // H
  1365. new (35, (VK)'H', ConsoleModifiers.Shift, 'H'),
  1366. new (36, (VK)'J', 0, 'j'), // J
  1367. new (36, (VK)'J', ConsoleModifiers.Shift, 'J'),
  1368. new (37, (VK)'K', 0, 'k'), // K
  1369. new (37, (VK)'K', ConsoleModifiers.Shift, 'K'),
  1370. new (38, (VK)'L', 0, 'l'), // L
  1371. new (38, (VK)'L', ConsoleModifiers.Shift, 'L'),
  1372. new (39, VK.OEM_3, 0, '`'), // Oem3 (Backtick/Grave)
  1373. new (39, VK.OEM_3, ConsoleModifiers.Shift, '~'),
  1374. new (40, VK.OEM_7, 0, '\''), // Oem7 (Single Quote)
  1375. new (40, VK.OEM_7, ConsoleModifiers.Shift, '\"'),
  1376. new (41, VK.OEM_5, 0, '\\'), // Oem5 (Backslash)
  1377. new (41, VK.OEM_5, ConsoleModifiers.Shift, '|'),
  1378. new (42, VK.LSHIFT, 0, '\0'), // Left Shift
  1379. new (42, VK.LSHIFT, ConsoleModifiers.Shift, '\0'),
  1380. new (43, VK.OEM_2, 0, '/'), // Oem2 (Forward Slash)
  1381. new (43, VK.OEM_2, ConsoleModifiers.Shift, '?'),
  1382. new (44, (VK)'Z', 0, 'z'), // Z
  1383. new (44, (VK)'Z', ConsoleModifiers.Shift, 'Z'),
  1384. new (45, (VK)'X', 0, 'x'), // X
  1385. new (45, (VK)'X', ConsoleModifiers.Shift, 'X'),
  1386. new (46, (VK)'C', 0, 'c'), // C
  1387. new (46, (VK)'C', ConsoleModifiers.Shift, 'C'),
  1388. new (47, (VK)'V', 0, 'v'), // V
  1389. new (47, (VK)'V', ConsoleModifiers.Shift, 'V'),
  1390. new (48, (VK)'B', 0, 'b'), // B
  1391. new (48, (VK)'B', ConsoleModifiers.Shift, 'B'),
  1392. new (49, (VK)'N', 0, 'n'), // N
  1393. new (49, (VK)'N', ConsoleModifiers.Shift, 'N'),
  1394. new (50, (VK)'M', 0, 'm'), // M
  1395. new (50, (VK)'M', ConsoleModifiers.Shift, 'M'),
  1396. new (51, VK.OEM_COMMA, 0, ','), // OemComma
  1397. new (51, VK.OEM_COMMA, ConsoleModifiers.Shift, '<'),
  1398. new (52, VK.OEM_PERIOD, 0, '.'), // OemPeriod
  1399. new (52, VK.OEM_PERIOD, ConsoleModifiers.Shift, '>'),
  1400. new (53, VK.OEM_MINUS, 0, '-'), // OemMinus
  1401. new (53, VK.OEM_MINUS, ConsoleModifiers.Shift, '_'),
  1402. new (54, VK.RSHIFT, 0, '\0'), // Right Shift
  1403. new (54, VK.RSHIFT, ConsoleModifiers.Shift, '\0'),
  1404. new (55, VK.PRINT, 0, '\0'), // Print Screen
  1405. new (55, VK.PRINT, ConsoleModifiers.Shift, '\0'),
  1406. new (56, VK.LMENU, 0, '\0'), // Alt
  1407. new (56, VK.LMENU, ConsoleModifiers.Shift, '\0'),
  1408. new (57, VK.SPACE, 0, ' '), // Spacebar
  1409. new (57, VK.SPACE, ConsoleModifiers.Shift, ' '),
  1410. new (58, VK.CAPITAL, 0, '\0'), // Caps Lock
  1411. new (58, VK.CAPITAL, ConsoleModifiers.Shift, '\0'),
  1412. new (59, VK.F1, 0, '\0'), // F1
  1413. new (59, VK.F1, ConsoleModifiers.Shift, '\0'),
  1414. new (60, VK.F2, 0, '\0'), // F2
  1415. new (60, VK.F2, ConsoleModifiers.Shift, '\0'),
  1416. new (61, VK.F3, 0, '\0'), // F3
  1417. new (61, VK.F3, ConsoleModifiers.Shift, '\0'),
  1418. new (62, VK.F4, 0, '\0'), // F4
  1419. new (62, VK.F4, ConsoleModifiers.Shift, '\0'),
  1420. new (63, VK.F5, 0, '\0'), // F5
  1421. new (63, VK.F5, ConsoleModifiers.Shift, '\0'),
  1422. new (64, VK.F6, 0, '\0'), // F6
  1423. new (64, VK.F6, ConsoleModifiers.Shift, '\0'),
  1424. new (65, VK.F7, 0, '\0'), // F7
  1425. new (65, VK.F7, ConsoleModifiers.Shift, '\0'),
  1426. new (66, VK.F8, 0, '\0'), // F8
  1427. new (66, VK.F8, ConsoleModifiers.Shift, '\0'),
  1428. new (67, VK.F9, 0, '\0'), // F9
  1429. new (67, VK.F9, ConsoleModifiers.Shift, '\0'),
  1430. new (68, VK.F10, 0, '\0'), // F10
  1431. new (68, VK.F10, ConsoleModifiers.Shift, '\0'),
  1432. new (69, VK.NUMLOCK, 0, '\0'), // Num Lock
  1433. new (69, VK.NUMLOCK, ConsoleModifiers.Shift, '\0'),
  1434. new (70, VK.SCROLL, 0, '\0'), // Scroll Lock
  1435. new (70, VK.SCROLL, ConsoleModifiers.Shift, '\0'),
  1436. new (71, VK.HOME, 0, '\0'), // Home
  1437. new (71, VK.HOME, ConsoleModifiers.Shift, '\0'),
  1438. new (72, VK.UP, 0, '\0'), // Up Arrow
  1439. new (72, VK.UP, ConsoleModifiers.Shift, '\0'),
  1440. new (73, VK.PRIOR, 0, '\0'), // Page Up
  1441. new (73, VK.PRIOR, ConsoleModifiers.Shift, '\0'),
  1442. new (74, VK.SUBTRACT, 0, '-'), // Subtract (Num Pad '-')
  1443. new (74, VK.SUBTRACT, ConsoleModifiers.Shift, '-'),
  1444. new (75, VK.LEFT, 0, '\0'), // Left Arrow
  1445. new (75, VK.LEFT, ConsoleModifiers.Shift, '\0'),
  1446. new (76, VK.CLEAR, 0, '\0'), // Center key (Num Pad 5 with Num Lock off)
  1447. new (76, VK.CLEAR, ConsoleModifiers.Shift, '\0'),
  1448. new (77, VK.RIGHT, 0, '\0'), // Right Arrow
  1449. new (77, VK.RIGHT, ConsoleModifiers.Shift, '\0'),
  1450. new (78, VK.ADD, 0, '+'), // Add (Num Pad '+')
  1451. new (78, VK.ADD, ConsoleModifiers.Shift, '+'),
  1452. new (79, VK.END, 0, '\0'), // End
  1453. new (79, VK.END, ConsoleModifiers.Shift, '\0'),
  1454. new (80, VK.DOWN, 0, '\0'), // Down Arrow
  1455. new (80, VK.DOWN, ConsoleModifiers.Shift, '\0'),
  1456. new (81, VK.NEXT, 0, '\0'), // Page Down
  1457. new (81, VK.NEXT, ConsoleModifiers.Shift, '\0'),
  1458. new (82, VK.INSERT, 0, '\0'), // Insert
  1459. new (82, VK.INSERT, ConsoleModifiers.Shift, '\0'),
  1460. new (83, VK.DELETE, 0, '\0'), // Delete
  1461. new (83, VK.DELETE, ConsoleModifiers.Shift, '\0'),
  1462. new (86, VK.OEM_102, 0, '<'), // OEM 102 (Typically '<' or '|' key next to Left Shift)
  1463. new (86, VK.OEM_102, ConsoleModifiers.Shift, '>'),
  1464. new (87, VK.F11, 0, '\0'), // F11
  1465. new (87, VK.F11, ConsoleModifiers.Shift, '\0'),
  1466. new (88, VK.F12, 0, '\0'), // F12
  1467. new (88, VK.F12, ConsoleModifiers.Shift, '\0')
  1468. };
  1469. /// <summary>
  1470. /// Decode a <see cref="ConsoleKeyInfo"/> that is using <see cref="ConsoleKey.Packet"/>.
  1471. /// </summary>
  1472. /// <param name="consoleKeyInfo">The console key info.</param>
  1473. /// <returns>The decoded <see cref="ConsoleKeyInfo"/> or the <paramref name="consoleKeyInfo"/>.</returns>
  1474. /// <remarks>If it's a <see cref="ConsoleKey.Packet"/> the <see cref="ConsoleKeyInfo.KeyChar"/> may be
  1475. /// a <see cref="ConsoleKeyInfo.Key"/> or a <see cref="ConsoleKeyInfo.KeyChar"/> value.
  1476. /// </remarks>
  1477. public static ConsoleKeyInfo DecodeVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  1478. {
  1479. if (consoleKeyInfo.Key != ConsoleKey.Packet) {
  1480. return consoleKeyInfo;
  1481. }
  1482. return GetConsoleKeyInfoFromKeyChar (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out _);
  1483. }
  1484. /// <summary>
  1485. /// Encode the <see cref="ConsoleKeyInfo.KeyChar"/> with the <see cref="ConsoleKeyInfo.Key"/>
  1486. /// if the first a byte length, otherwise only the KeyChar is considered and searched on the database.
  1487. /// </summary>
  1488. /// <param name="consoleKeyInfo">The console key info.</param>
  1489. /// <returns>The encoded KeyChar with the Key if both can be shifted, otherwise only the KeyChar.</returns>
  1490. /// <remarks>This is useful to use with the <see cref="ConsoleKey.Packet"/>.</remarks>
  1491. public static char EncodeKeyCharForVKPacket (ConsoleKeyInfo consoleKeyInfo)
  1492. {
  1493. char keyChar = consoleKeyInfo.KeyChar;
  1494. ConsoleKey consoleKey = consoleKeyInfo.Key;
  1495. if (keyChar != 0 && consoleKeyInfo.KeyChar < byte.MaxValue && consoleKey == ConsoleKey.None) {
  1496. // try to get the ConsoleKey
  1497. var scode = _scanCodes.FirstOrDefault ((e) => e.UnicodeChar == keyChar);
  1498. if (scode != null) {
  1499. consoleKey = (ConsoleKey)scode.VirtualKey;
  1500. }
  1501. }
  1502. if (keyChar < byte.MaxValue && consoleKey != ConsoleKey.None) {
  1503. keyChar = (char)(consoleKeyInfo.KeyChar << 8 | (byte)consoleKey);
  1504. }
  1505. return keyChar;
  1506. }
  1507. }