ConsoleKeyMapping.cs 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  1. using System.Globalization;
  2. using System.Runtime.InteropServices;
  3. namespace Terminal.Gui.ConsoleDrivers;
  4. /// <summary>Helper class to handle the scan code and virtual key from a <see cref="ConsoleKey"/>.</summary>
  5. public static class ConsoleKeyMapping
  6. {
  7. #if !WT_ISSUE_8871_FIXED // https://github.com/microsoft/terminal/issues/8871
  8. /// <summary>
  9. /// Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a
  10. /// virtual-key code.
  11. /// </summary>
  12. /// <param name="vk"></param>
  13. /// <param name="uMapType">
  14. /// If MAPVK_VK_TO_CHAR (2) - The uCode parameter is a virtual-key code and is translated into an
  15. /// un-shifted character value in the low order word of the return value.
  16. /// </param>
  17. /// <param name="dwhkl"></param>
  18. /// <returns>
  19. /// An un-shifted character value in the low order word of the return value. Dead keys (diacritics) are indicated
  20. /// by setting the top bit of the return value. If there is no translation, the function returns 0. See Remarks.
  21. /// </returns>
  22. [DllImport ("user32.dll", EntryPoint = "MapVirtualKeyExW", CharSet = CharSet.Unicode)]
  23. private static extern uint MapVirtualKeyEx (VK vk, uint uMapType, nint dwhkl);
  24. /// <summary>Retrieves the active input locale identifier (formerly called the keyboard layout).</summary>
  25. /// <param name="idThread">0 for current thread</param>
  26. /// <returns>
  27. /// The return value is the input locale identifier for the thread. The low word contains a Language Identifier
  28. /// for the input language and the high word contains a device handle to the physical layout of the keyboard.
  29. /// </returns>
  30. [DllImport ("user32.dll", EntryPoint = "GetKeyboardLayout", CharSet = CharSet.Unicode)]
  31. private static extern nint GetKeyboardLayout (nint idThread);
  32. //[DllImport ("user32.dll", EntryPoint = "GetKeyboardLayoutNameW", CharSet = CharSet.Unicode)]
  33. //extern static uint GetKeyboardLayoutName (uint idThread);
  34. [DllImport ("user32.dll")]
  35. private static extern nint GetForegroundWindow ();
  36. [DllImport ("user32.dll")]
  37. private static extern nint GetWindowThreadProcessId (nint hWnd, nint ProcessId);
  38. /// <summary>
  39. /// Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or
  40. /// characters using the Win32 API MapVirtualKey.
  41. /// </summary>
  42. /// <param name="vk"></param>
  43. /// <returns>
  44. /// An un-shifted character value in the low order word of the return value. Dead keys (diacritics) are indicated
  45. /// by setting the top bit of the return value. If there is no translation, the function returns 0.
  46. /// </returns>
  47. public static uint MapVKtoChar (VK vk)
  48. {
  49. if (Environment.OSVersion.Platform != PlatformID.Win32NT)
  50. {
  51. return 0;
  52. }
  53. nint tid = GetWindowThreadProcessId (GetForegroundWindow (), 0);
  54. nint hkl = GetKeyboardLayout (tid);
  55. return MapVirtualKeyEx (vk, 2, hkl);
  56. }
  57. #else
  58. /// <summary>
  59. /// Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
  60. /// </summary>
  61. /// <param name="vk"></param>
  62. /// <param name="uMapType">
  63. /// If MAPVK_VK_TO_CHAR (2) - The uCode parameter is a virtual-key code and is translated into an unshifted
  64. /// character value in the low order word of the return value.
  65. /// </param>
  66. /// <returns>An unshifted character value in the low order word of the return value. Dead keys (diacritics)
  67. /// are indicated by setting the top bit of the return value. If there is no translation,
  68. /// the function returns 0. See Remarks.</returns>
  69. [DllImport ("user32.dll", EntryPoint = "MapVirtualKeyW", CharSet = CharSet.Unicode)]
  70. extern static uint MapVirtualKey (VK vk, uint uMapType = 2);
  71. uint MapVKtoChar (VK vk) => MapVirtualKeyToCharEx (vk);
  72. #endif
  73. /// <summary>
  74. /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the calling
  75. /// thread.
  76. /// </summary>
  77. /// <param name="pwszKLID"></param>
  78. /// <returns></returns>
  79. [DllImport ("user32.dll")]
  80. private static extern bool GetKeyboardLayoutName ([Out] StringBuilder pwszKLID);
  81. /// <summary>
  82. /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the calling
  83. /// thread.
  84. /// </summary>
  85. /// <returns></returns>
  86. public static string GetKeyboardLayoutName ()
  87. {
  88. if (Environment.OSVersion.Platform != PlatformID.Win32NT)
  89. {
  90. return "none";
  91. }
  92. var klidSB = new StringBuilder ();
  93. GetKeyboardLayoutName (klidSB);
  94. return klidSB.ToString ();
  95. }
  96. private class ScanCodeMapping : IEquatable<ScanCodeMapping>
  97. {
  98. public readonly ConsoleModifiers Modifiers;
  99. public readonly uint ScanCode;
  100. public readonly uint UnicodeChar;
  101. public readonly VK VirtualKey;
  102. public ScanCodeMapping (uint scanCode, VK virtualKey, ConsoleModifiers modifiers, uint unicodeChar)
  103. {
  104. ScanCode = scanCode;
  105. VirtualKey = virtualKey;
  106. Modifiers = modifiers;
  107. UnicodeChar = unicodeChar;
  108. }
  109. public bool Equals (ScanCodeMapping other)
  110. {
  111. return ScanCode.Equals (other.ScanCode)
  112. && VirtualKey.Equals (other.VirtualKey)
  113. && Modifiers.Equals (other.Modifiers)
  114. && UnicodeChar.Equals (other.UnicodeChar);
  115. }
  116. }
  117. private static ConsoleModifiers GetModifiers (ConsoleModifiers modifiers)
  118. {
  119. if (modifiers.HasFlag (ConsoleModifiers.Shift)
  120. && !modifiers.HasFlag (ConsoleModifiers.Alt)
  121. && !modifiers.HasFlag (ConsoleModifiers.Control))
  122. {
  123. return ConsoleModifiers.Shift;
  124. }
  125. if (modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control))
  126. {
  127. return modifiers;
  128. }
  129. return 0;
  130. }
  131. private static ScanCodeMapping GetScanCode (string propName, uint keyValue, ConsoleModifiers modifiers)
  132. {
  133. switch (propName)
  134. {
  135. case "UnicodeChar":
  136. ScanCodeMapping sCode =
  137. _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyValue && e.Modifiers == modifiers);
  138. if (sCode is null && modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control))
  139. {
  140. return _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyValue && e.Modifiers == 0);
  141. }
  142. return sCode;
  143. case "VirtualKey":
  144. sCode = _scanCodes.FirstOrDefault (e => e.VirtualKey == (VK)keyValue && e.Modifiers == modifiers);
  145. if (sCode is null && modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control))
  146. {
  147. return _scanCodes.FirstOrDefault (e => e.VirtualKey == (VK)keyValue && e.Modifiers == 0);
  148. }
  149. return sCode;
  150. }
  151. return null;
  152. }
  153. // BUGBUG: This API is not correct. It is only used by WindowsDriver in VKPacket scenarios
  154. /// <summary>Get the scan code from a <see cref="ConsoleKeyInfo"/>.</summary>
  155. /// <param name="consoleKeyInfo">The console key info.</param>
  156. /// <returns>The value if apply.</returns>
  157. public static uint GetScanCodeFromConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  158. {
  159. ConsoleModifiers mod = GetModifiers (consoleKeyInfo.Modifiers);
  160. ScanCodeMapping scode = GetScanCode ("VirtualKey", (uint)consoleKeyInfo.Key, mod);
  161. if (scode is { })
  162. {
  163. return scode.ScanCode;
  164. }
  165. return 0;
  166. }
  167. // BUGBUG: This API is not correct. It is only used by FakeDriver and VkeyPacketSimulator
  168. /// <summary>Gets the <see cref="ConsoleKeyInfo"/> from the provided <see cref="KeyCode"/>.</summary>
  169. /// <param name="key">The key code.</param>
  170. /// <returns>The console key info.</returns>
  171. public static ConsoleKeyInfo GetConsoleKeyInfoFromKeyCode (KeyCode key)
  172. {
  173. ConsoleModifiers modifiers = MapToConsoleModifiers (key);
  174. uint keyValue = MapKeyCodeToConsoleKey (key, out bool isConsoleKey);
  175. if (isConsoleKey)
  176. {
  177. ConsoleModifiers mod = GetModifiers (modifiers);
  178. ScanCodeMapping scode = GetScanCode ("VirtualKey", keyValue, mod);
  179. if (scode is { })
  180. {
  181. return new ConsoleKeyInfo (
  182. (char)scode.UnicodeChar,
  183. (ConsoleKey)scode.VirtualKey,
  184. modifiers.HasFlag (ConsoleModifiers.Shift),
  185. modifiers.HasFlag (ConsoleModifiers.Alt),
  186. modifiers.HasFlag (ConsoleModifiers.Control)
  187. );
  188. }
  189. }
  190. else
  191. {
  192. uint keyChar = GetKeyCharFromUnicodeChar (keyValue, modifiers, out uint consoleKey, out _, isConsoleKey);
  193. if (consoleKey != 0)
  194. {
  195. return new ConsoleKeyInfo (
  196. (char)keyChar,
  197. (ConsoleKey)consoleKey,
  198. modifiers.HasFlag (ConsoleModifiers.Shift),
  199. modifiers.HasFlag (ConsoleModifiers.Alt),
  200. modifiers.HasFlag (ConsoleModifiers.Control)
  201. );
  202. }
  203. }
  204. return new ConsoleKeyInfo (
  205. (char)keyValue,
  206. ConsoleKey.None,
  207. modifiers.HasFlag (ConsoleModifiers.Shift),
  208. modifiers.HasFlag (ConsoleModifiers.Alt),
  209. modifiers.HasFlag (ConsoleModifiers.Control)
  210. );
  211. }
  212. /// <summary>Map existing <see cref="KeyCode"/> modifiers to <see cref="ConsoleModifiers"/>.</summary>
  213. /// <param name="key">The key code.</param>
  214. /// <returns>The console modifiers.</returns>
  215. public static ConsoleModifiers MapToConsoleModifiers (KeyCode key)
  216. {
  217. var modifiers = new ConsoleModifiers ();
  218. if (key.HasFlag (KeyCode.ShiftMask))
  219. {
  220. modifiers |= ConsoleModifiers.Shift;
  221. }
  222. if (key.HasFlag (KeyCode.AltMask))
  223. {
  224. modifiers |= ConsoleModifiers.Alt;
  225. }
  226. if (key.HasFlag (KeyCode.CtrlMask))
  227. {
  228. modifiers |= ConsoleModifiers.Control;
  229. }
  230. return modifiers;
  231. }
  232. /// <summary>Gets <see cref="ConsoleModifiers"/> from <see cref="bool"/> modifiers.</summary>
  233. /// <param name="shift">The shift key.</param>
  234. /// <param name="alt">The alt key.</param>
  235. /// <param name="control">The control key.</param>
  236. /// <returns>The console modifiers.</returns>
  237. public static ConsoleModifiers GetModifiers (bool shift, bool alt, bool control)
  238. {
  239. var modifiers = new ConsoleModifiers ();
  240. if (shift)
  241. {
  242. modifiers |= ConsoleModifiers.Shift;
  243. }
  244. if (alt)
  245. {
  246. modifiers |= ConsoleModifiers.Alt;
  247. }
  248. if (control)
  249. {
  250. modifiers |= ConsoleModifiers.Control;
  251. }
  252. return modifiers;
  253. }
  254. /// <summary>
  255. /// Get the <see cref="ConsoleKeyInfo"/> from a unicode character and modifiers (e.g. (Key)'a' and
  256. /// (Key)Key.CtrlMask).
  257. /// </summary>
  258. /// <param name="keyValue">The key as a unicode codepoint.</param>
  259. /// <param name="modifiers">The modifier keys.</param>
  260. /// <param name="scanCode">The resulting scan code.</param>
  261. /// <returns>The <see cref="ConsoleKeyInfo"/>.</returns>
  262. private static ConsoleKeyInfo GetConsoleKeyInfoFromKeyChar (
  263. uint keyValue,
  264. ConsoleModifiers modifiers,
  265. out uint scanCode
  266. )
  267. {
  268. scanCode = 0;
  269. if (keyValue == 0)
  270. {
  271. return new ConsoleKeyInfo (
  272. (char)keyValue,
  273. ConsoleKey.None,
  274. modifiers.HasFlag (ConsoleModifiers.Shift),
  275. modifiers.HasFlag (ConsoleModifiers.Alt),
  276. modifiers.HasFlag (ConsoleModifiers.Control)
  277. );
  278. }
  279. uint outputChar = keyValue;
  280. uint consoleKey;
  281. if (keyValue > byte.MaxValue)
  282. {
  283. ScanCodeMapping sCode = _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyValue);
  284. if (sCode is null)
  285. {
  286. consoleKey = (byte)(keyValue & byte.MaxValue);
  287. sCode = _scanCodes.FirstOrDefault (e => e.VirtualKey == (VK)consoleKey);
  288. if (sCode is null)
  289. {
  290. consoleKey = 0;
  291. outputChar = keyValue;
  292. }
  293. else
  294. {
  295. outputChar = (char)(keyValue >> 8);
  296. }
  297. }
  298. else
  299. {
  300. consoleKey = (byte)sCode.VirtualKey;
  301. outputChar = keyValue;
  302. }
  303. }
  304. else
  305. {
  306. consoleKey = (byte)keyValue;
  307. outputChar = '\0';
  308. }
  309. return new ConsoleKeyInfo (
  310. (char)outputChar,
  311. (ConsoleKey)consoleKey,
  312. modifiers.HasFlag (ConsoleModifiers.Shift),
  313. modifiers.HasFlag (ConsoleModifiers.Alt),
  314. modifiers.HasFlag (ConsoleModifiers.Control)
  315. );
  316. }
  317. // Used only by unit tests
  318. internal static uint GetKeyChar (uint keyValue, ConsoleModifiers modifiers)
  319. {
  320. if (modifiers == ConsoleModifiers.Shift && keyValue - 32 is >= 'A' and <= 'Z')
  321. {
  322. return keyValue - 32;
  323. }
  324. if (modifiers == ConsoleModifiers.None && keyValue is >= 'A' and <= 'Z')
  325. {
  326. return keyValue + 32;
  327. }
  328. if (modifiers == ConsoleModifiers.Shift && keyValue - 32 is >= 'À' and <= 'Ý')
  329. {
  330. return keyValue - 32;
  331. }
  332. if (modifiers == ConsoleModifiers.None && keyValue is >= 'À' and <= 'Ý')
  333. {
  334. return keyValue + 32;
  335. }
  336. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '0')
  337. {
  338. return keyValue + 13;
  339. }
  340. if (modifiers == ConsoleModifiers.None && keyValue - 13 is '0')
  341. {
  342. return keyValue - 13;
  343. }
  344. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is >= '1' and <= '9' and not '7')
  345. {
  346. return keyValue - 16;
  347. }
  348. if (modifiers == ConsoleModifiers.None && keyValue + 16 is >= '1' and <= '9' and not '7')
  349. {
  350. return keyValue + 16;
  351. }
  352. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '7')
  353. {
  354. return keyValue - 8;
  355. }
  356. if (modifiers == ConsoleModifiers.None && keyValue + 8 is '7')
  357. {
  358. return keyValue + 8;
  359. }
  360. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '\'')
  361. {
  362. return keyValue + 24;
  363. }
  364. if (modifiers == ConsoleModifiers.None && keyValue - 24 is '\'')
  365. {
  366. return keyValue - 24;
  367. }
  368. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '«')
  369. {
  370. return keyValue + 16;
  371. }
  372. if (modifiers == ConsoleModifiers.None && keyValue - 16 is '«')
  373. {
  374. return keyValue - 16;
  375. }
  376. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '\\')
  377. {
  378. return keyValue + 32;
  379. }
  380. if (modifiers == ConsoleModifiers.None && keyValue - 32 is '\\')
  381. {
  382. return keyValue - 32;
  383. }
  384. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '+')
  385. {
  386. return keyValue - 1;
  387. }
  388. if (modifiers == ConsoleModifiers.None && keyValue + 1 is '+')
  389. {
  390. return keyValue + 1;
  391. }
  392. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '´')
  393. {
  394. return keyValue - 84;
  395. }
  396. if (modifiers == ConsoleModifiers.None && keyValue + 84 is '´')
  397. {
  398. return keyValue + 84;
  399. }
  400. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is 'º')
  401. {
  402. return keyValue - 16;
  403. }
  404. if (modifiers == ConsoleModifiers.None && keyValue + 16 is 'º')
  405. {
  406. return keyValue + 16;
  407. }
  408. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '~')
  409. {
  410. return keyValue - 32;
  411. }
  412. if (modifiers == ConsoleModifiers.None && keyValue + 32 is '~')
  413. {
  414. return keyValue + 32;
  415. }
  416. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '<')
  417. {
  418. return keyValue + 2;
  419. }
  420. if (modifiers == ConsoleModifiers.None && keyValue - 2 is '<')
  421. {
  422. return keyValue - 2;
  423. }
  424. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is ',')
  425. {
  426. return keyValue + 15;
  427. }
  428. if (modifiers == ConsoleModifiers.None && keyValue - 15 is ',')
  429. {
  430. return keyValue - 15;
  431. }
  432. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '.')
  433. {
  434. return keyValue + 12;
  435. }
  436. if (modifiers == ConsoleModifiers.None && keyValue - 12 is '.')
  437. {
  438. return keyValue - 12;
  439. }
  440. if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '-')
  441. {
  442. return keyValue + 50;
  443. }
  444. if (modifiers == ConsoleModifiers.None && keyValue - 50 is '-')
  445. {
  446. return keyValue - 50;
  447. }
  448. return keyValue;
  449. }
  450. /// <summary>
  451. /// Get the output character from the <see cref="GetConsoleKeyInfoFromKeyCode"/>, with the correct
  452. /// <see cref="ConsoleKey"/> and the scan code used on <see cref="WindowsDriver"/>.
  453. /// </summary>
  454. /// <param name="unicodeChar">The unicode character.</param>
  455. /// <param name="modifiers">The modifiers keys.</param>
  456. /// <param name="consoleKey">The resulting console key.</param>
  457. /// <param name="scanCode">The resulting scan code.</param>
  458. /// <param name="isConsoleKey">Indicates if the <paramref name="unicodeChar"/> is a <see cref="ConsoleKey"/>.</param>
  459. /// <returns>The output character or the <paramref name="consoleKey"/>.</returns>
  460. /// <remarks>This is only used by the <see cref="GetConsoleKeyInfoFromKeyCode"/> and by unit tests.</remarks>
  461. internal static uint GetKeyCharFromUnicodeChar (
  462. uint unicodeChar,
  463. ConsoleModifiers modifiers,
  464. out uint consoleKey,
  465. out uint scanCode,
  466. bool isConsoleKey = false
  467. )
  468. {
  469. uint decodedChar = unicodeChar >> 8 == 0xff ? unicodeChar & 0xff : unicodeChar;
  470. uint keyChar = decodedChar;
  471. consoleKey = 0;
  472. ConsoleModifiers mod = GetModifiers (modifiers);
  473. scanCode = 0;
  474. ScanCodeMapping scode = null;
  475. if (unicodeChar != 0 && unicodeChar >> 8 != 0xff && isConsoleKey)
  476. {
  477. scode = GetScanCode ("VirtualKey", decodedChar, mod);
  478. }
  479. if (isConsoleKey && scode is { })
  480. {
  481. consoleKey = (uint)scode.VirtualKey;
  482. keyChar = scode.UnicodeChar;
  483. scanCode = scode.ScanCode;
  484. }
  485. if (scode is null)
  486. {
  487. scode = unicodeChar != 0 ? GetScanCode ("UnicodeChar", decodedChar, mod) : null;
  488. if (scode is { })
  489. {
  490. consoleKey = (uint)scode.VirtualKey;
  491. keyChar = scode.UnicodeChar;
  492. scanCode = scode.ScanCode;
  493. }
  494. }
  495. if (decodedChar != 0 && scanCode == 0 && char.IsLetter ((char)decodedChar))
  496. {
  497. string stFormD = ((char)decodedChar).ToString ().Normalize (NormalizationForm.FormD);
  498. for (var i = 0; i < stFormD.Length; i++)
  499. {
  500. UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory (stFormD [i]);
  501. if (uc != UnicodeCategory.NonSpacingMark && uc != UnicodeCategory.OtherLetter)
  502. {
  503. consoleKey = char.ToUpper (stFormD [i]);
  504. scode = GetScanCode ("VirtualKey", char.ToUpper (stFormD [i]), 0);
  505. if (scode is { })
  506. {
  507. scanCode = scode.ScanCode;
  508. }
  509. }
  510. }
  511. }
  512. if (keyChar < 255 && consoleKey == 0 && scanCode == 0)
  513. {
  514. scode = GetScanCode ("VirtualKey", keyChar, mod);
  515. if (scode is { })
  516. {
  517. consoleKey = (uint)scode.VirtualKey;
  518. keyChar = scode.UnicodeChar;
  519. scanCode = scode.ScanCode;
  520. }
  521. }
  522. return keyChar;
  523. }
  524. /// <summary>Maps a unicode character (e.g. (Key)'a') to a uint representing a <see cref="ConsoleKey"/>.</summary>
  525. /// <param name="keyValue">The key value.</param>
  526. /// <param name="isConsoleKey">
  527. /// Indicates if the <paramref name="keyValue"/> is a <see cref="ConsoleKey"/>.
  528. /// <see langword="true"/> means the return value is in the ConsoleKey enum. <see langword="false"/> means the return
  529. /// value can be mapped to a valid unicode character.
  530. /// </param>
  531. /// <returns>The <see cref="ConsoleKey"/> or the <paramref name="keyValue"/>.</returns>
  532. /// <remarks>This is only used by the <see cref="GetConsoleKeyInfoFromKeyCode"/> and by unit tests.</remarks>
  533. internal static uint MapKeyCodeToConsoleKey (KeyCode keyValue, out bool isConsoleKey)
  534. {
  535. isConsoleKey = true;
  536. keyValue = keyValue & ~KeyCode.CtrlMask & ~KeyCode.ShiftMask & ~KeyCode.AltMask;
  537. switch (keyValue)
  538. {
  539. case KeyCode.Enter:
  540. return (uint)ConsoleKey.Enter;
  541. case KeyCode.CursorUp:
  542. return (uint)ConsoleKey.UpArrow;
  543. case KeyCode.CursorDown:
  544. return (uint)ConsoleKey.DownArrow;
  545. case KeyCode.CursorLeft:
  546. return (uint)ConsoleKey.LeftArrow;
  547. case KeyCode.CursorRight:
  548. return (uint)ConsoleKey.RightArrow;
  549. case KeyCode.PageUp:
  550. return (uint)ConsoleKey.PageUp;
  551. case KeyCode.PageDown:
  552. return (uint)ConsoleKey.PageDown;
  553. case KeyCode.Home:
  554. return (uint)ConsoleKey.Home;
  555. case KeyCode.End:
  556. return (uint)ConsoleKey.End;
  557. case KeyCode.Insert:
  558. return (uint)ConsoleKey.Insert;
  559. case KeyCode.Delete:
  560. return (uint)ConsoleKey.Delete;
  561. case KeyCode.F1:
  562. return (uint)ConsoleKey.F1;
  563. case KeyCode.F2:
  564. return (uint)ConsoleKey.F2;
  565. case KeyCode.F3:
  566. return (uint)ConsoleKey.F3;
  567. case KeyCode.F4:
  568. return (uint)ConsoleKey.F4;
  569. case KeyCode.F5:
  570. return (uint)ConsoleKey.F5;
  571. case KeyCode.F6:
  572. return (uint)ConsoleKey.F6;
  573. case KeyCode.F7:
  574. return (uint)ConsoleKey.F7;
  575. case KeyCode.F8:
  576. return (uint)ConsoleKey.F8;
  577. case KeyCode.F9:
  578. return (uint)ConsoleKey.F9;
  579. case KeyCode.F10:
  580. return (uint)ConsoleKey.F10;
  581. case KeyCode.F11:
  582. return (uint)ConsoleKey.F11;
  583. case KeyCode.F12:
  584. return (uint)ConsoleKey.F12;
  585. case KeyCode.F13:
  586. return (uint)ConsoleKey.F13;
  587. case KeyCode.F14:
  588. return (uint)ConsoleKey.F14;
  589. case KeyCode.F15:
  590. return (uint)ConsoleKey.F15;
  591. case KeyCode.F16:
  592. return (uint)ConsoleKey.F16;
  593. case KeyCode.F17:
  594. return (uint)ConsoleKey.F17;
  595. case KeyCode.F18:
  596. return (uint)ConsoleKey.F18;
  597. case KeyCode.F19:
  598. return (uint)ConsoleKey.F19;
  599. case KeyCode.F20:
  600. return (uint)ConsoleKey.F20;
  601. case KeyCode.F21:
  602. return (uint)ConsoleKey.F21;
  603. case KeyCode.F22:
  604. return (uint)ConsoleKey.F22;
  605. case KeyCode.F23:
  606. return (uint)ConsoleKey.F23;
  607. case KeyCode.F24:
  608. return (uint)ConsoleKey.F24;
  609. case KeyCode.Tab | KeyCode.ShiftMask:
  610. return (uint)ConsoleKey.Tab;
  611. case KeyCode.Space:
  612. return (uint)ConsoleKey.Spacebar;
  613. default:
  614. uint c = (char)keyValue;
  615. if (c is >= (char)ConsoleKey.A and <= (char)ConsoleKey.Z)
  616. {
  617. return c;
  618. }
  619. if ((c - 32) is >= (char)ConsoleKey.A and <= (char)ConsoleKey.Z)
  620. {
  621. return (c - 32);
  622. }
  623. if (Enum.IsDefined (typeof (ConsoleKey), keyValue.ToString ()))
  624. {
  625. return (uint)keyValue;
  626. }
  627. // DEL
  628. if ((uint)keyValue == 127)
  629. {
  630. return (uint)ConsoleKey.Backspace;
  631. }
  632. break;
  633. }
  634. isConsoleKey = false;
  635. return (uint)keyValue;
  636. }
  637. /// <summary>Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.</summary>
  638. /// <param name="consoleKeyInfo">The console key.</param>
  639. /// <returns>The <see cref="KeyCode"/> or the <paramref name="consoleKeyInfo"/>.</returns>
  640. public static KeyCode MapConsoleKeyInfoToKeyCode (ConsoleKeyInfo consoleKeyInfo)
  641. {
  642. KeyCode keyCode;
  643. switch (consoleKeyInfo.Key)
  644. {
  645. case ConsoleKey.Enter:
  646. keyCode = KeyCode.Enter;
  647. break;
  648. case ConsoleKey.Delete:
  649. keyCode = KeyCode.Delete;
  650. break;
  651. case ConsoleKey.UpArrow:
  652. keyCode = KeyCode.CursorUp;
  653. break;
  654. case ConsoleKey.DownArrow:
  655. keyCode = KeyCode.CursorDown;
  656. break;
  657. case ConsoleKey.LeftArrow:
  658. keyCode = KeyCode.CursorLeft;
  659. break;
  660. case ConsoleKey.RightArrow:
  661. keyCode = KeyCode.CursorRight;
  662. break;
  663. case ConsoleKey.PageUp:
  664. keyCode = KeyCode.PageUp;
  665. break;
  666. case ConsoleKey.PageDown:
  667. keyCode = KeyCode.PageDown;
  668. break;
  669. case ConsoleKey.Home:
  670. keyCode = KeyCode.Home;
  671. break;
  672. case ConsoleKey.End:
  673. keyCode = KeyCode.End;
  674. break;
  675. case ConsoleKey.Insert:
  676. keyCode = KeyCode.Insert;
  677. break;
  678. case ConsoleKey.F1:
  679. keyCode = KeyCode.F1;
  680. break;
  681. case ConsoleKey.F2:
  682. keyCode = KeyCode.F2;
  683. break;
  684. case ConsoleKey.F3:
  685. keyCode = KeyCode.F3;
  686. break;
  687. case ConsoleKey.F4:
  688. keyCode = KeyCode.F4;
  689. break;
  690. case ConsoleKey.F5:
  691. keyCode = KeyCode.F5;
  692. break;
  693. case ConsoleKey.F6:
  694. keyCode = KeyCode.F6;
  695. break;
  696. case ConsoleKey.F7:
  697. keyCode = KeyCode.F7;
  698. break;
  699. case ConsoleKey.F8:
  700. keyCode = KeyCode.F8;
  701. break;
  702. case ConsoleKey.F9:
  703. keyCode = KeyCode.F9;
  704. break;
  705. case ConsoleKey.F10:
  706. keyCode = KeyCode.F10;
  707. break;
  708. case ConsoleKey.F11:
  709. keyCode = KeyCode.F11;
  710. break;
  711. case ConsoleKey.F12:
  712. keyCode = KeyCode.F12;
  713. break;
  714. case ConsoleKey.F13:
  715. keyCode = KeyCode.F13;
  716. break;
  717. case ConsoleKey.F14:
  718. keyCode = KeyCode.F14;
  719. break;
  720. case ConsoleKey.F15:
  721. keyCode = KeyCode.F15;
  722. break;
  723. case ConsoleKey.F16:
  724. keyCode = KeyCode.F16;
  725. break;
  726. case ConsoleKey.F17:
  727. keyCode = KeyCode.F17;
  728. break;
  729. case ConsoleKey.F18:
  730. keyCode = KeyCode.F18;
  731. break;
  732. case ConsoleKey.F19:
  733. keyCode = KeyCode.F19;
  734. break;
  735. case ConsoleKey.F20:
  736. keyCode = KeyCode.F20;
  737. break;
  738. case ConsoleKey.F21:
  739. keyCode = KeyCode.F21;
  740. break;
  741. case ConsoleKey.F22:
  742. keyCode = KeyCode.F22;
  743. break;
  744. case ConsoleKey.F23:
  745. keyCode = KeyCode.F23;
  746. break;
  747. case ConsoleKey.F24:
  748. keyCode = KeyCode.F24;
  749. break;
  750. case ConsoleKey.Clear:
  751. keyCode = KeyCode.Clear;
  752. break;
  753. case ConsoleKey.Tab:
  754. keyCode = KeyCode.Tab;
  755. break;
  756. case ConsoleKey.Spacebar:
  757. keyCode = KeyCode.Space;
  758. break;
  759. case ConsoleKey.Backspace:
  760. keyCode = KeyCode.Backspace;
  761. break;
  762. default:
  763. if ((int)consoleKeyInfo.KeyChar is >= 1 and <= 26)
  764. {
  765. keyCode = (KeyCode)(consoleKeyInfo.KeyChar + 64);
  766. }
  767. else
  768. {
  769. keyCode = (KeyCode)consoleKeyInfo.KeyChar;
  770. }
  771. break;
  772. }
  773. keyCode |= MapToKeyCodeModifiers (consoleKeyInfo.Modifiers, keyCode);
  774. return keyCode;
  775. }
  776. /// <summary>Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.</summary>
  777. /// <param name="modifiers">The console modifiers.</param>
  778. /// <param name="key">The key code.</param>
  779. /// <returns>The <see cref="KeyCode"/> with <see cref="ConsoleModifiers"/> or the <paramref name="key"/></returns>
  780. public static KeyCode MapToKeyCodeModifiers (ConsoleModifiers modifiers, KeyCode key)
  781. {
  782. var keyMod = new KeyCode ();
  783. if ((modifiers & ConsoleModifiers.Shift) != 0)
  784. {
  785. keyMod = KeyCode.ShiftMask;
  786. }
  787. if ((modifiers & ConsoleModifiers.Control) != 0)
  788. {
  789. keyMod |= KeyCode.CtrlMask;
  790. }
  791. if ((modifiers & ConsoleModifiers.Alt) != 0)
  792. {
  793. keyMod |= KeyCode.AltMask;
  794. }
  795. return keyMod != KeyCode.Null ? keyMod | key : key;
  796. }
  797. /// <summary>Generated from winuser.h. See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes</summary>
  798. public enum VK : ushort
  799. {
  800. /// <summary>Left mouse button.</summary>
  801. LBUTTON = 0x01,
  802. /// <summary>Right mouse button.</summary>
  803. RBUTTON = 0x02,
  804. /// <summary>Control-break processing.</summary>
  805. CANCEL = 0x03,
  806. /// <summary>Middle mouse button (three-button mouse).</summary>
  807. MBUTTON = 0x04,
  808. /// <summary>X1 mouse button.</summary>
  809. XBUTTON1 = 0x05,
  810. /// <summary>X2 mouse button.</summary>
  811. XBUTTON2 = 0x06,
  812. /// <summary>BACKSPACE key.</summary>
  813. BACK = 0x08,
  814. /// <summary>TAB key.</summary>
  815. TAB = 0x09,
  816. /// <summary>CLEAR key.</summary>
  817. CLEAR = 0x0C,
  818. /// <summary>ENTER key.</summary>
  819. RETURN = 0x0D,
  820. /// <summary>SHIFT key.</summary>
  821. SHIFT = 0x10,
  822. /// <summary>CTRL key.</summary>
  823. CONTROL = 0x11,
  824. /// <summary>ALT key.</summary>
  825. MENU = 0x12,
  826. /// <summary>PAUSE key.</summary>
  827. PAUSE = 0x13,
  828. /// <summary>CAPS LOCK key.</summary>
  829. CAPITAL = 0x14,
  830. /// <summary>IME Kana mode.</summary>
  831. KANA = 0x15,
  832. /// <summary>IME Hangul mode.</summary>
  833. HANGUL = 0x15,
  834. /// <summary>IME Junja mode.</summary>
  835. JUNJA = 0x17,
  836. /// <summary>IME final mode.</summary>
  837. FINAL = 0x18,
  838. /// <summary>IME Hanja mode.</summary>
  839. HANJA = 0x19,
  840. /// <summary>IME Kanji mode.</summary>
  841. KANJI = 0x19,
  842. /// <summary>ESC key.</summary>
  843. ESCAPE = 0x1B,
  844. /// <summary>IME convert.</summary>
  845. CONVERT = 0x1C,
  846. /// <summary>IME nonconvert.</summary>
  847. NONCONVERT = 0x1D,
  848. /// <summary>IME accept.</summary>
  849. ACCEPT = 0x1E,
  850. /// <summary>IME mode change request.</summary>
  851. MODECHANGE = 0x1F,
  852. /// <summary>SPACEBAR.</summary>
  853. SPACE = 0x20,
  854. /// <summary>PAGE UP key.</summary>
  855. PRIOR = 0x21,
  856. /// <summary>PAGE DOWN key.</summary>
  857. NEXT = 0x22,
  858. /// <summary>END key.</summary>
  859. END = 0x23,
  860. /// <summary>HOME key.</summary>
  861. HOME = 0x24,
  862. /// <summary>LEFT ARROW key.</summary>
  863. LEFT = 0x25,
  864. /// <summary>UP ARROW key.</summary>
  865. UP = 0x26,
  866. /// <summary>RIGHT ARROW key.</summary>
  867. RIGHT = 0x27,
  868. /// <summary>DOWN ARROW key.</summary>
  869. DOWN = 0x28,
  870. /// <summary>SELECT key.</summary>
  871. SELECT = 0x29,
  872. /// <summary>PRINT key.</summary>
  873. PRINT = 0x2A,
  874. /// <summary>EXECUTE key</summary>
  875. EXECUTE = 0x2B,
  876. /// <summary>PRINT SCREEN key</summary>
  877. SNAPSHOT = 0x2C,
  878. /// <summary>INS key</summary>
  879. INSERT = 0x2D,
  880. /// <summary>DEL key</summary>
  881. DELETE = 0x2E,
  882. /// <summary>HELP key</summary>
  883. HELP = 0x2F,
  884. /// <summary>Left Windows key (Natural keyboard)</summary>
  885. LWIN = 0x5B,
  886. /// <summary>Right Windows key (Natural keyboard)</summary>
  887. RWIN = 0x5C,
  888. /// <summary>Applications key (Natural keyboard)</summary>
  889. APPS = 0x5D,
  890. /// <summary>Computer Sleep key</summary>
  891. SLEEP = 0x5F,
  892. /// <summary>Numeric keypad 0 key</summary>
  893. NUMPAD0 = 0x60,
  894. /// <summary>Numeric keypad 1 key</summary>
  895. NUMPAD1 = 0x61,
  896. /// <summary>Numeric keypad 2 key</summary>
  897. NUMPAD2 = 0x62,
  898. /// <summary>Numeric keypad 3 key</summary>
  899. NUMPAD3 = 0x63,
  900. /// <summary>Numeric keypad 4 key</summary>
  901. NUMPAD4 = 0x64,
  902. /// <summary>Numeric keypad 5 key</summary>
  903. NUMPAD5 = 0x65,
  904. /// <summary>Numeric keypad 6 key</summary>
  905. NUMPAD6 = 0x66,
  906. /// <summary>Numeric keypad 7 key</summary>
  907. NUMPAD7 = 0x67,
  908. /// <summary>Numeric keypad 8 key</summary>
  909. NUMPAD8 = 0x68,
  910. /// <summary>Numeric keypad 9 key</summary>
  911. NUMPAD9 = 0x69,
  912. /// <summary>Multiply key</summary>
  913. MULTIPLY = 0x6A,
  914. /// <summary>Add key</summary>
  915. ADD = 0x6B,
  916. /// <summary>Separator key</summary>
  917. SEPARATOR = 0x6C,
  918. /// <summary>Subtract key</summary>
  919. SUBTRACT = 0x6D,
  920. /// <summary>Decimal key</summary>
  921. DECIMAL = 0x6E,
  922. /// <summary>Divide key</summary>
  923. DIVIDE = 0x6F,
  924. /// <summary>F1 key</summary>
  925. F1 = 0x70,
  926. /// <summary>F2 key</summary>
  927. F2 = 0x71,
  928. /// <summary>F3 key</summary>
  929. F3 = 0x72,
  930. /// <summary>F4 key</summary>
  931. F4 = 0x73,
  932. /// <summary>F5 key</summary>
  933. F5 = 0x74,
  934. /// <summary>F6 key</summary>
  935. F6 = 0x75,
  936. /// <summary>F7 key</summary>
  937. F7 = 0x76,
  938. /// <summary>F8 key</summary>
  939. F8 = 0x77,
  940. /// <summary>F9 key</summary>
  941. F9 = 0x78,
  942. /// <summary>F10 key</summary>
  943. F10 = 0x79,
  944. /// <summary>F11 key</summary>
  945. F11 = 0x7A,
  946. /// <summary>F12 key</summary>
  947. F12 = 0x7B,
  948. /// <summary>F13 key</summary>
  949. F13 = 0x7C,
  950. /// <summary>F14 key</summary>
  951. F14 = 0x7D,
  952. /// <summary>F15 key</summary>
  953. F15 = 0x7E,
  954. /// <summary>F16 key</summary>
  955. F16 = 0x7F,
  956. /// <summary>F17 key</summary>
  957. F17 = 0x80,
  958. /// <summary>F18 key</summary>
  959. F18 = 0x81,
  960. /// <summary>F19 key</summary>
  961. F19 = 0x82,
  962. /// <summary>F20 key</summary>
  963. F20 = 0x83,
  964. /// <summary>F21 key</summary>
  965. F21 = 0x84,
  966. /// <summary>F22 key</summary>
  967. F22 = 0x85,
  968. /// <summary>F23 key</summary>
  969. F23 = 0x86,
  970. /// <summary>F24 key</summary>
  971. F24 = 0x87,
  972. /// <summary>NUM LOCK key</summary>
  973. NUMLOCK = 0x90,
  974. /// <summary>SCROLL LOCK key</summary>
  975. SCROLL = 0x91,
  976. /// <summary>NEC PC-9800 kbd definition: '=' key on numpad</summary>
  977. OEM_NEC_EQUAL = 0x92,
  978. /// <summary>Fujitsu/OASYS kbd definition: 'Dictionary' key</summary>
  979. OEM_FJ_JISHO = 0x92,
  980. /// <summary>Fujitsu/OASYS kbd definition: 'Unregister word' key</summary>
  981. OEM_FJ_MASSHOU = 0x93,
  982. /// <summary>Fujitsu/OASYS kbd definition: 'Register word' key</summary>
  983. OEM_FJ_TOUROKU = 0x94,
  984. /// <summary>Fujitsu/OASYS kbd definition: 'Left OYAYUBI' key</summary>
  985. OEM_FJ_LOYA = 0x95,
  986. /// <summary>Fujitsu/OASYS kbd definition: 'Right OYAYUBI' key</summary>
  987. OEM_FJ_ROYA = 0x96,
  988. /// <summary>Left SHIFT key</summary>
  989. LSHIFT = 0xA0,
  990. /// <summary>Right SHIFT key</summary>
  991. RSHIFT = 0xA1,
  992. /// <summary>Left CONTROL key</summary>
  993. LCONTROL = 0xA2,
  994. /// <summary>Right CONTROL key</summary>
  995. RCONTROL = 0xA3,
  996. /// <summary>Left MENU key (Left Alt key)</summary>
  997. LMENU = 0xA4,
  998. /// <summary>Right MENU key (Right Alt key)</summary>
  999. RMENU = 0xA5,
  1000. /// <summary>Browser Back key</summary>
  1001. BROWSER_BACK = 0xA6,
  1002. /// <summary>Browser Forward key</summary>
  1003. BROWSER_FORWARD = 0xA7,
  1004. /// <summary>Browser Refresh key</summary>
  1005. BROWSER_REFRESH = 0xA8,
  1006. /// <summary>Browser Stop key</summary>
  1007. BROWSER_STOP = 0xA9,
  1008. /// <summary>Browser Search key</summary>
  1009. BROWSER_SEARCH = 0xAA,
  1010. /// <summary>Browser Favorites key</summary>
  1011. BROWSER_FAVORITES = 0xAB,
  1012. /// <summary>Browser Home key</summary>
  1013. BROWSER_HOME = 0xAC,
  1014. /// <summary>Volume Mute key</summary>
  1015. VOLUME_MUTE = 0xAD,
  1016. /// <summary>Volume Down key</summary>
  1017. VOLUME_DOWN = 0xAE,
  1018. /// <summary>Volume Up key</summary>
  1019. VOLUME_UP = 0xAF,
  1020. /// <summary>Next Track key</summary>
  1021. MEDIA_NEXT_TRACK = 0xB0,
  1022. /// <summary>Previous Track key</summary>
  1023. MEDIA_PREV_TRACK = 0xB1,
  1024. /// <summary>Stop Media key</summary>
  1025. MEDIA_STOP = 0xB2,
  1026. /// <summary>Play/Pause Media key</summary>
  1027. MEDIA_PLAY_PAUSE = 0xB3,
  1028. /// <summary>Start Mail key</summary>
  1029. LAUNCH_MAIL = 0xB4,
  1030. /// <summary>Select Media key</summary>
  1031. LAUNCH_MEDIA_SELECT = 0xB5,
  1032. /// <summary>Start Application 1 key</summary>
  1033. LAUNCH_APP1 = 0xB6,
  1034. /// <summary>Start Application 2 key</summary>
  1035. LAUNCH_APP2 = 0xB7,
  1036. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key</summary>
  1037. OEM_1 = 0xBA,
  1038. /// <summary>For any country/region, the '+' key</summary>
  1039. OEM_PLUS = 0xBB,
  1040. /// <summary>For any country/region, the ',' key</summary>
  1041. OEM_COMMA = 0xBC,
  1042. /// <summary>For any country/region, the '-' key</summary>
  1043. OEM_MINUS = 0xBD,
  1044. /// <summary>For any country/region, the '.' key</summary>
  1045. OEM_PERIOD = 0xBE,
  1046. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key</summary>
  1047. OEM_2 = 0xBF,
  1048. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key</summary>
  1049. OEM_3 = 0xC0,
  1050. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key</summary>
  1051. OEM_4 = 0xDB,
  1052. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key</summary>
  1053. OEM_5 = 0xDC,
  1054. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key</summary>
  1055. OEM_6 = 0xDD,
  1056. /// <summary>
  1057. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the
  1058. /// 'single-quote/double-quote' key
  1059. /// </summary>
  1060. OEM_7 = 0xDE,
  1061. /// <summary>Used for miscellaneous characters; it can vary by keyboard.</summary>
  1062. OEM_8 = 0xDF,
  1063. /// <summary>'AX' key on Japanese AX kbd</summary>
  1064. OEM_AX = 0xE1,
  1065. /// <summary>Either the angle bracket key or the backslash key on the RT 102-key keyboard</summary>
  1066. OEM_102 = 0xE2,
  1067. /// <summary>Help key on ICO</summary>
  1068. ICO_HELP = 0xE3,
  1069. /// <summary>00 key on ICO</summary>
  1070. ICO_00 = 0xE4,
  1071. /// <summary>Process key</summary>
  1072. PROCESSKEY = 0xE5,
  1073. /// <summary>Clear key on ICO</summary>
  1074. ICO_CLEAR = 0xE6,
  1075. /// <summary>Packet key to be used to pass Unicode characters as if they were keystrokes</summary>
  1076. PACKET = 0xE7,
  1077. /// <summary>Reset key</summary>
  1078. OEM_RESET = 0xE9,
  1079. /// <summary>Jump key</summary>
  1080. OEM_JUMP = 0xEA,
  1081. /// <summary>PA1 key</summary>
  1082. OEM_PA1 = 0xEB,
  1083. /// <summary>PA2 key</summary>
  1084. OEM_PA2 = 0xEC,
  1085. /// <summary>PA3 key</summary>
  1086. OEM_PA3 = 0xED,
  1087. /// <summary>WsCtrl key</summary>
  1088. OEM_WSCTRL = 0xEE,
  1089. /// <summary>CuSel key</summary>
  1090. OEM_CUSEL = 0xEF,
  1091. /// <summary>Attn key</summary>
  1092. OEM_ATTN = 0xF0,
  1093. /// <summary>Finish key</summary>
  1094. OEM_FINISH = 0xF1,
  1095. /// <summary>Copy key</summary>
  1096. OEM_COPY = 0xF2,
  1097. /// <summary>Auto key</summary>
  1098. OEM_AUTO = 0xF3,
  1099. /// <summary>Enlw key</summary>
  1100. OEM_ENLW = 0xF4,
  1101. /// <summary>BackTab key</summary>
  1102. OEM_BACKTAB = 0xF5,
  1103. /// <summary>Attn key</summary>
  1104. ATTN = 0xF6,
  1105. /// <summary>CrSel key</summary>
  1106. CRSEL = 0xF7,
  1107. /// <summary>ExSel key</summary>
  1108. EXSEL = 0xF8,
  1109. /// <summary>Erase EOF key</summary>
  1110. EREOF = 0xF9,
  1111. /// <summary>Play key</summary>
  1112. PLAY = 0xFA,
  1113. /// <summary>Zoom key</summary>
  1114. ZOOM = 0xFB,
  1115. /// <summary>Reserved</summary>
  1116. NONAME = 0xFC,
  1117. /// <summary>PA1 key</summary>
  1118. PA1 = 0xFD,
  1119. /// <summary>Clear key</summary>
  1120. OEM_CLEAR = 0xFE
  1121. }
  1122. // BUGBUG: This database makes no sense. It is not possible to map a VK code to a character without knowing the keyboard layout
  1123. // It should be deleted.
  1124. private static readonly HashSet<ScanCodeMapping> _scanCodes = new ()
  1125. {
  1126. new ScanCodeMapping (
  1127. 1,
  1128. VK.ESCAPE,
  1129. 0,
  1130. '\u001B'
  1131. ), // Escape
  1132. new ScanCodeMapping (
  1133. 1,
  1134. VK.ESCAPE,
  1135. ConsoleModifiers.Shift,
  1136. '\u001B'
  1137. ),
  1138. new ScanCodeMapping (
  1139. 2,
  1140. (VK)'1',
  1141. 0,
  1142. '1'
  1143. ), // D1
  1144. new ScanCodeMapping (
  1145. 2,
  1146. (VK)'1',
  1147. ConsoleModifiers.Shift,
  1148. '!'
  1149. ),
  1150. new ScanCodeMapping (
  1151. 3,
  1152. (VK)'2',
  1153. 0,
  1154. '2'
  1155. ), // D2
  1156. new ScanCodeMapping (
  1157. 3,
  1158. (VK)'2',
  1159. ConsoleModifiers.Shift,
  1160. '\"'
  1161. ), // BUGBUG: This is true for Portuguese keyboard, but not ENG (@) or DEU (")
  1162. new ScanCodeMapping (
  1163. 3,
  1164. (VK)'2',
  1165. ConsoleModifiers.Alt
  1166. | ConsoleModifiers.Control,
  1167. '@'
  1168. ),
  1169. new ScanCodeMapping (
  1170. 4,
  1171. (VK)'3',
  1172. 0,
  1173. '3'
  1174. ), // D3
  1175. new ScanCodeMapping (
  1176. 4,
  1177. (VK)'3',
  1178. ConsoleModifiers.Shift,
  1179. '#'
  1180. ),
  1181. new ScanCodeMapping (
  1182. 4,
  1183. (VK)'3',
  1184. ConsoleModifiers.Alt
  1185. | ConsoleModifiers.Control,
  1186. '£'
  1187. ),
  1188. new ScanCodeMapping (
  1189. 5,
  1190. (VK)'4',
  1191. 0,
  1192. '4'
  1193. ), // D4
  1194. new ScanCodeMapping (
  1195. 5,
  1196. (VK)'4',
  1197. ConsoleModifiers.Shift,
  1198. '$'
  1199. ),
  1200. new ScanCodeMapping (
  1201. 5,
  1202. (VK)'4',
  1203. ConsoleModifiers.Alt
  1204. | ConsoleModifiers.Control,
  1205. '§'
  1206. ),
  1207. new ScanCodeMapping (
  1208. 6,
  1209. (VK)'5',
  1210. 0,
  1211. '5'
  1212. ), // D5
  1213. new ScanCodeMapping (
  1214. 6,
  1215. (VK)'5',
  1216. ConsoleModifiers.Shift,
  1217. '%'
  1218. ),
  1219. new ScanCodeMapping (
  1220. 6,
  1221. (VK)'5',
  1222. ConsoleModifiers.Alt
  1223. | ConsoleModifiers.Control,
  1224. '€'
  1225. ),
  1226. new ScanCodeMapping (
  1227. 7,
  1228. (VK)'6',
  1229. 0,
  1230. '6'
  1231. ), // D6
  1232. new ScanCodeMapping (
  1233. 7,
  1234. (VK)'6',
  1235. ConsoleModifiers.Shift,
  1236. '&'
  1237. ),
  1238. new ScanCodeMapping (
  1239. 8,
  1240. (VK)'7',
  1241. 0,
  1242. '7'
  1243. ), // D7
  1244. new ScanCodeMapping (
  1245. 8,
  1246. (VK)'7',
  1247. ConsoleModifiers.Shift,
  1248. '/'
  1249. ),
  1250. new ScanCodeMapping (
  1251. 8,
  1252. (VK)'7',
  1253. ConsoleModifiers.Alt
  1254. | ConsoleModifiers.Control,
  1255. '{'
  1256. ),
  1257. new ScanCodeMapping (
  1258. 9,
  1259. (VK)'8',
  1260. 0,
  1261. '8'
  1262. ), // D8
  1263. new ScanCodeMapping (
  1264. 9,
  1265. (VK)'8',
  1266. ConsoleModifiers.Shift,
  1267. '('
  1268. ),
  1269. new ScanCodeMapping (
  1270. 9,
  1271. (VK)'8',
  1272. ConsoleModifiers.Alt
  1273. | ConsoleModifiers.Control,
  1274. '['
  1275. ),
  1276. new ScanCodeMapping (
  1277. 10,
  1278. (VK)'9',
  1279. 0,
  1280. '9'
  1281. ), // D9
  1282. new ScanCodeMapping (
  1283. 10,
  1284. (VK)'9',
  1285. ConsoleModifiers.Shift,
  1286. ')'
  1287. ),
  1288. new ScanCodeMapping (
  1289. 10,
  1290. (VK)'9',
  1291. ConsoleModifiers.Alt
  1292. | ConsoleModifiers.Control,
  1293. ']'
  1294. ),
  1295. new ScanCodeMapping (
  1296. 11,
  1297. (VK)'0',
  1298. 0,
  1299. '0'
  1300. ), // D0
  1301. new ScanCodeMapping (
  1302. 11,
  1303. (VK)'0',
  1304. ConsoleModifiers.Shift,
  1305. '='
  1306. ),
  1307. new ScanCodeMapping (
  1308. 11,
  1309. (VK)'0',
  1310. ConsoleModifiers.Alt
  1311. | ConsoleModifiers.Control,
  1312. '}'
  1313. ),
  1314. new ScanCodeMapping (
  1315. 12,
  1316. VK.OEM_4,
  1317. 0,
  1318. '\''
  1319. ), // Oem4
  1320. new ScanCodeMapping (
  1321. 12,
  1322. VK.OEM_4,
  1323. ConsoleModifiers.Shift,
  1324. '?'
  1325. ),
  1326. new ScanCodeMapping (
  1327. 13,
  1328. VK.OEM_6,
  1329. 0,
  1330. '+'
  1331. ), // Oem6
  1332. new ScanCodeMapping (
  1333. 13,
  1334. VK.OEM_6,
  1335. ConsoleModifiers.Shift,
  1336. '*'
  1337. ),
  1338. new ScanCodeMapping (
  1339. 14,
  1340. VK.BACK,
  1341. 0,
  1342. '\u0008'
  1343. ), // Backspace
  1344. new ScanCodeMapping (
  1345. 14,
  1346. VK.BACK,
  1347. ConsoleModifiers.Shift,
  1348. '\u0008'
  1349. ),
  1350. new ScanCodeMapping (
  1351. 15,
  1352. VK.TAB,
  1353. 0,
  1354. '\u0009'
  1355. ), // Tab
  1356. new ScanCodeMapping (
  1357. 15,
  1358. VK.TAB,
  1359. ConsoleModifiers.Shift,
  1360. '\u000F'
  1361. ),
  1362. new ScanCodeMapping (
  1363. 16,
  1364. (VK)'Q',
  1365. 0,
  1366. 'q'
  1367. ), // Q
  1368. new ScanCodeMapping (
  1369. 16,
  1370. (VK)'Q',
  1371. ConsoleModifiers.Shift,
  1372. 'Q'
  1373. ),
  1374. new ScanCodeMapping (
  1375. 17,
  1376. (VK)'W',
  1377. 0,
  1378. 'w'
  1379. ), // W
  1380. new ScanCodeMapping (
  1381. 17,
  1382. (VK)'W',
  1383. ConsoleModifiers.Shift,
  1384. 'W'
  1385. ),
  1386. new ScanCodeMapping (
  1387. 18,
  1388. (VK)'E',
  1389. 0,
  1390. 'e'
  1391. ), // E
  1392. new ScanCodeMapping (
  1393. 18,
  1394. (VK)'E',
  1395. ConsoleModifiers.Shift,
  1396. 'E'
  1397. ),
  1398. new ScanCodeMapping (
  1399. 19,
  1400. (VK)'R',
  1401. 0,
  1402. 'r'
  1403. ), // R
  1404. new ScanCodeMapping (
  1405. 19,
  1406. (VK)'R',
  1407. ConsoleModifiers.Shift,
  1408. 'R'
  1409. ),
  1410. new ScanCodeMapping (
  1411. 20,
  1412. (VK)'T',
  1413. 0,
  1414. 't'
  1415. ), // T
  1416. new ScanCodeMapping (
  1417. 20,
  1418. (VK)'T',
  1419. ConsoleModifiers.Shift,
  1420. 'T'
  1421. ),
  1422. new ScanCodeMapping (
  1423. 21,
  1424. (VK)'Y',
  1425. 0,
  1426. 'y'
  1427. ), // Y
  1428. new ScanCodeMapping (
  1429. 21,
  1430. (VK)'Y',
  1431. ConsoleModifiers.Shift,
  1432. 'Y'
  1433. ),
  1434. new ScanCodeMapping (
  1435. 22,
  1436. (VK)'U',
  1437. 0,
  1438. 'u'
  1439. ), // U
  1440. new ScanCodeMapping (
  1441. 22,
  1442. (VK)'U',
  1443. ConsoleModifiers.Shift,
  1444. 'U'
  1445. ),
  1446. new ScanCodeMapping (
  1447. 23,
  1448. (VK)'I',
  1449. 0,
  1450. 'i'
  1451. ), // I
  1452. new ScanCodeMapping (
  1453. 23,
  1454. (VK)'I',
  1455. ConsoleModifiers.Shift,
  1456. 'I'
  1457. ),
  1458. new ScanCodeMapping (
  1459. 24,
  1460. (VK)'O',
  1461. 0,
  1462. 'o'
  1463. ), // O
  1464. new ScanCodeMapping (
  1465. 24,
  1466. (VK)'O',
  1467. ConsoleModifiers.Shift,
  1468. 'O'
  1469. ),
  1470. new ScanCodeMapping (
  1471. 25,
  1472. (VK)'P',
  1473. 0,
  1474. 'p'
  1475. ), // P
  1476. new ScanCodeMapping (
  1477. 25,
  1478. (VK)'P',
  1479. ConsoleModifiers.Shift,
  1480. 'P'
  1481. ),
  1482. new ScanCodeMapping (
  1483. 26,
  1484. VK.OEM_PLUS,
  1485. 0,
  1486. '+'
  1487. ), // OemPlus
  1488. new ScanCodeMapping (
  1489. 26,
  1490. VK.OEM_PLUS,
  1491. ConsoleModifiers.Shift,
  1492. '*'
  1493. ),
  1494. new ScanCodeMapping (
  1495. 26,
  1496. VK.OEM_PLUS,
  1497. ConsoleModifiers.Alt
  1498. | ConsoleModifiers.Control,
  1499. '¨'
  1500. ),
  1501. new ScanCodeMapping (
  1502. 27,
  1503. VK.OEM_1,
  1504. 0,
  1505. '´'
  1506. ), // Oem1
  1507. new ScanCodeMapping (
  1508. 27,
  1509. VK.OEM_1,
  1510. ConsoleModifiers.Shift,
  1511. '`'
  1512. ),
  1513. new ScanCodeMapping (
  1514. 28,
  1515. VK.RETURN,
  1516. 0,
  1517. '\u000D'
  1518. ), // Enter
  1519. new ScanCodeMapping (
  1520. 28,
  1521. VK.RETURN,
  1522. ConsoleModifiers.Shift,
  1523. '\u000D'
  1524. ),
  1525. new ScanCodeMapping (
  1526. 29,
  1527. VK.CONTROL,
  1528. 0,
  1529. '\0'
  1530. ), // Control
  1531. new ScanCodeMapping (
  1532. 29,
  1533. VK.CONTROL,
  1534. ConsoleModifiers.Shift,
  1535. '\0'
  1536. ),
  1537. new ScanCodeMapping (
  1538. 30,
  1539. (VK)'A',
  1540. 0,
  1541. 'a'
  1542. ), // A
  1543. new ScanCodeMapping (
  1544. 30,
  1545. (VK)'A',
  1546. ConsoleModifiers.Shift,
  1547. 'A'
  1548. ),
  1549. new ScanCodeMapping (
  1550. 31,
  1551. (VK)'S',
  1552. 0,
  1553. 's'
  1554. ), // S
  1555. new ScanCodeMapping (
  1556. 31,
  1557. (VK)'S',
  1558. ConsoleModifiers.Shift,
  1559. 'S'
  1560. ),
  1561. new ScanCodeMapping (
  1562. 32,
  1563. (VK)'D',
  1564. 0,
  1565. 'd'
  1566. ), // D
  1567. new ScanCodeMapping (
  1568. 32,
  1569. (VK)'D',
  1570. ConsoleModifiers.Shift,
  1571. 'D'
  1572. ),
  1573. new ScanCodeMapping (
  1574. 33,
  1575. (VK)'F',
  1576. 0,
  1577. 'f'
  1578. ), // F
  1579. new ScanCodeMapping (
  1580. 33,
  1581. (VK)'F',
  1582. ConsoleModifiers.Shift,
  1583. 'F'
  1584. ),
  1585. new ScanCodeMapping (
  1586. 34,
  1587. (VK)'G',
  1588. 0,
  1589. 'g'
  1590. ), // G
  1591. new ScanCodeMapping (
  1592. 34,
  1593. (VK)'G',
  1594. ConsoleModifiers.Shift,
  1595. 'G'
  1596. ),
  1597. new ScanCodeMapping (
  1598. 35,
  1599. (VK)'H',
  1600. 0,
  1601. 'h'
  1602. ), // H
  1603. new ScanCodeMapping (
  1604. 35,
  1605. (VK)'H',
  1606. ConsoleModifiers.Shift,
  1607. 'H'
  1608. ),
  1609. new ScanCodeMapping (
  1610. 36,
  1611. (VK)'J',
  1612. 0,
  1613. 'j'
  1614. ), // J
  1615. new ScanCodeMapping (
  1616. 36,
  1617. (VK)'J',
  1618. ConsoleModifiers.Shift,
  1619. 'J'
  1620. ),
  1621. new ScanCodeMapping (
  1622. 37,
  1623. (VK)'K',
  1624. 0,
  1625. 'k'
  1626. ), // K
  1627. new ScanCodeMapping (
  1628. 37,
  1629. (VK)'K',
  1630. ConsoleModifiers.Shift,
  1631. 'K'
  1632. ),
  1633. new ScanCodeMapping (
  1634. 38,
  1635. (VK)'L',
  1636. 0,
  1637. 'l'
  1638. ), // L
  1639. new ScanCodeMapping (
  1640. 38,
  1641. (VK)'L',
  1642. ConsoleModifiers.Shift,
  1643. 'L'
  1644. ),
  1645. new ScanCodeMapping (
  1646. 39,
  1647. VK.OEM_3,
  1648. 0,
  1649. '`'
  1650. ), // Oem3 (Backtick/Grave)
  1651. new ScanCodeMapping (
  1652. 39,
  1653. VK.OEM_3,
  1654. ConsoleModifiers.Shift,
  1655. '~'
  1656. ),
  1657. new ScanCodeMapping (
  1658. 40,
  1659. VK.OEM_7,
  1660. 0,
  1661. '\''
  1662. ), // Oem7 (Single Quote)
  1663. new ScanCodeMapping (
  1664. 40,
  1665. VK.OEM_7,
  1666. ConsoleModifiers.Shift,
  1667. '\"'
  1668. ),
  1669. new ScanCodeMapping (
  1670. 41,
  1671. VK.OEM_5,
  1672. 0,
  1673. '\\'
  1674. ), // Oem5 (Backslash)
  1675. new ScanCodeMapping (
  1676. 41,
  1677. VK.OEM_5,
  1678. ConsoleModifiers.Shift,
  1679. '|'
  1680. ),
  1681. new ScanCodeMapping (
  1682. 42,
  1683. VK.LSHIFT,
  1684. 0,
  1685. '\0'
  1686. ), // Left Shift
  1687. new ScanCodeMapping (
  1688. 42,
  1689. VK.LSHIFT,
  1690. ConsoleModifiers.Shift,
  1691. '\0'
  1692. ),
  1693. new ScanCodeMapping (
  1694. 43,
  1695. VK.OEM_2,
  1696. 0,
  1697. '/'
  1698. ), // Oem2 (Forward Slash)
  1699. new ScanCodeMapping (
  1700. 43,
  1701. VK.OEM_2,
  1702. ConsoleModifiers.Shift,
  1703. '?'
  1704. ),
  1705. new ScanCodeMapping (
  1706. 44,
  1707. (VK)'Z',
  1708. 0,
  1709. 'z'
  1710. ), // Z
  1711. new ScanCodeMapping (
  1712. 44,
  1713. (VK)'Z',
  1714. ConsoleModifiers.Shift,
  1715. 'Z'
  1716. ),
  1717. new ScanCodeMapping (
  1718. 45,
  1719. (VK)'X',
  1720. 0,
  1721. 'x'
  1722. ), // X
  1723. new ScanCodeMapping (
  1724. 45,
  1725. (VK)'X',
  1726. ConsoleModifiers.Shift,
  1727. 'X'
  1728. ),
  1729. new ScanCodeMapping (
  1730. 46,
  1731. (VK)'C',
  1732. 0,
  1733. 'c'
  1734. ), // C
  1735. new ScanCodeMapping (
  1736. 46,
  1737. (VK)'C',
  1738. ConsoleModifiers.Shift,
  1739. 'C'
  1740. ),
  1741. new ScanCodeMapping (
  1742. 47,
  1743. (VK)'V',
  1744. 0,
  1745. 'v'
  1746. ), // V
  1747. new ScanCodeMapping (
  1748. 47,
  1749. (VK)'V',
  1750. ConsoleModifiers.Shift,
  1751. 'V'
  1752. ),
  1753. new ScanCodeMapping (
  1754. 48,
  1755. (VK)'B',
  1756. 0,
  1757. 'b'
  1758. ), // B
  1759. new ScanCodeMapping (
  1760. 48,
  1761. (VK)'B',
  1762. ConsoleModifiers.Shift,
  1763. 'B'
  1764. ),
  1765. new ScanCodeMapping (
  1766. 49,
  1767. (VK)'N',
  1768. 0,
  1769. 'n'
  1770. ), // N
  1771. new ScanCodeMapping (
  1772. 49,
  1773. (VK)'N',
  1774. ConsoleModifiers.Shift,
  1775. 'N'
  1776. ),
  1777. new ScanCodeMapping (
  1778. 50,
  1779. (VK)'M',
  1780. 0,
  1781. 'm'
  1782. ), // M
  1783. new ScanCodeMapping (
  1784. 50,
  1785. (VK)'M',
  1786. ConsoleModifiers.Shift,
  1787. 'M'
  1788. ),
  1789. new ScanCodeMapping (
  1790. 51,
  1791. VK.OEM_COMMA,
  1792. 0,
  1793. ','
  1794. ), // OemComma
  1795. new ScanCodeMapping (
  1796. 51,
  1797. VK.OEM_COMMA,
  1798. ConsoleModifiers.Shift,
  1799. '<'
  1800. ),
  1801. new ScanCodeMapping (
  1802. 52,
  1803. VK.OEM_PERIOD,
  1804. 0,
  1805. '.'
  1806. ), // OemPeriod
  1807. new ScanCodeMapping (
  1808. 52,
  1809. VK.OEM_PERIOD,
  1810. ConsoleModifiers.Shift,
  1811. '>'
  1812. ),
  1813. new ScanCodeMapping (
  1814. 53,
  1815. VK.OEM_MINUS,
  1816. 0,
  1817. '-'
  1818. ), // OemMinus
  1819. new ScanCodeMapping (
  1820. 53,
  1821. VK.OEM_MINUS,
  1822. ConsoleModifiers.Shift,
  1823. '_'
  1824. ),
  1825. new ScanCodeMapping (
  1826. 54,
  1827. VK.RSHIFT,
  1828. 0,
  1829. '\0'
  1830. ), // Right Shift
  1831. new ScanCodeMapping (
  1832. 54,
  1833. VK.RSHIFT,
  1834. ConsoleModifiers.Shift,
  1835. '\0'
  1836. ),
  1837. new ScanCodeMapping (
  1838. 55,
  1839. VK.PRINT,
  1840. 0,
  1841. '\0'
  1842. ), // Print Screen
  1843. new ScanCodeMapping (
  1844. 55,
  1845. VK.PRINT,
  1846. ConsoleModifiers.Shift,
  1847. '\0'
  1848. ),
  1849. new ScanCodeMapping (
  1850. 56,
  1851. VK.LMENU,
  1852. 0,
  1853. '\0'
  1854. ), // Alt
  1855. new ScanCodeMapping (
  1856. 56,
  1857. VK.LMENU,
  1858. ConsoleModifiers.Shift,
  1859. '\0'
  1860. ),
  1861. new ScanCodeMapping (
  1862. 57,
  1863. VK.SPACE,
  1864. 0,
  1865. ' '
  1866. ), // Spacebar
  1867. new ScanCodeMapping (
  1868. 57,
  1869. VK.SPACE,
  1870. ConsoleModifiers.Shift,
  1871. ' '
  1872. ),
  1873. new ScanCodeMapping (
  1874. 58,
  1875. VK.CAPITAL,
  1876. 0,
  1877. '\0'
  1878. ), // Caps Lock
  1879. new ScanCodeMapping (
  1880. 58,
  1881. VK.CAPITAL,
  1882. ConsoleModifiers.Shift,
  1883. '\0'
  1884. ),
  1885. new ScanCodeMapping (
  1886. 59,
  1887. VK.F1,
  1888. 0,
  1889. '\0'
  1890. ), // F1
  1891. new ScanCodeMapping (
  1892. 59,
  1893. VK.F1,
  1894. ConsoleModifiers.Shift,
  1895. '\0'
  1896. ),
  1897. new ScanCodeMapping (
  1898. 60,
  1899. VK.F2,
  1900. 0,
  1901. '\0'
  1902. ), // F2
  1903. new ScanCodeMapping (
  1904. 60,
  1905. VK.F2,
  1906. ConsoleModifiers.Shift,
  1907. '\0'
  1908. ),
  1909. new ScanCodeMapping (
  1910. 61,
  1911. VK.F3,
  1912. 0,
  1913. '\0'
  1914. ), // F3
  1915. new ScanCodeMapping (
  1916. 61,
  1917. VK.F3,
  1918. ConsoleModifiers.Shift,
  1919. '\0'
  1920. ),
  1921. new ScanCodeMapping (
  1922. 62,
  1923. VK.F4,
  1924. 0,
  1925. '\0'
  1926. ), // F4
  1927. new ScanCodeMapping (
  1928. 62,
  1929. VK.F4,
  1930. ConsoleModifiers.Shift,
  1931. '\0'
  1932. ),
  1933. new ScanCodeMapping (
  1934. 63,
  1935. VK.F5,
  1936. 0,
  1937. '\0'
  1938. ), // F5
  1939. new ScanCodeMapping (
  1940. 63,
  1941. VK.F5,
  1942. ConsoleModifiers.Shift,
  1943. '\0'
  1944. ),
  1945. new ScanCodeMapping (
  1946. 64,
  1947. VK.F6,
  1948. 0,
  1949. '\0'
  1950. ), // F6
  1951. new ScanCodeMapping (
  1952. 64,
  1953. VK.F6,
  1954. ConsoleModifiers.Shift,
  1955. '\0'
  1956. ),
  1957. new ScanCodeMapping (
  1958. 65,
  1959. VK.F7,
  1960. 0,
  1961. '\0'
  1962. ), // F7
  1963. new ScanCodeMapping (
  1964. 65,
  1965. VK.F7,
  1966. ConsoleModifiers.Shift,
  1967. '\0'
  1968. ),
  1969. new ScanCodeMapping (
  1970. 66,
  1971. VK.F8,
  1972. 0,
  1973. '\0'
  1974. ), // F8
  1975. new ScanCodeMapping (
  1976. 66,
  1977. VK.F8,
  1978. ConsoleModifiers.Shift,
  1979. '\0'
  1980. ),
  1981. new ScanCodeMapping (
  1982. 67,
  1983. VK.F9,
  1984. 0,
  1985. '\0'
  1986. ), // F9
  1987. new ScanCodeMapping (
  1988. 67,
  1989. VK.F9,
  1990. ConsoleModifiers.Shift,
  1991. '\0'
  1992. ),
  1993. new ScanCodeMapping (
  1994. 68,
  1995. VK.F10,
  1996. 0,
  1997. '\0'
  1998. ), // F10
  1999. new ScanCodeMapping (
  2000. 68,
  2001. VK.F10,
  2002. ConsoleModifiers.Shift,
  2003. '\0'
  2004. ),
  2005. new ScanCodeMapping (
  2006. 69,
  2007. VK.NUMLOCK,
  2008. 0,
  2009. '\0'
  2010. ), // Num Lock
  2011. new ScanCodeMapping (
  2012. 69,
  2013. VK.NUMLOCK,
  2014. ConsoleModifiers.Shift,
  2015. '\0'
  2016. ),
  2017. new ScanCodeMapping (
  2018. 70,
  2019. VK.SCROLL,
  2020. 0,
  2021. '\0'
  2022. ), // Scroll Lock
  2023. new ScanCodeMapping (
  2024. 70,
  2025. VK.SCROLL,
  2026. ConsoleModifiers.Shift,
  2027. '\0'
  2028. ),
  2029. new ScanCodeMapping (
  2030. 71,
  2031. VK.HOME,
  2032. 0,
  2033. '\0'
  2034. ), // Home
  2035. new ScanCodeMapping (
  2036. 71,
  2037. VK.HOME,
  2038. ConsoleModifiers.Shift,
  2039. '\0'
  2040. ),
  2041. new ScanCodeMapping (
  2042. 72,
  2043. VK.UP,
  2044. 0,
  2045. '\0'
  2046. ), // Up Arrow
  2047. new ScanCodeMapping (
  2048. 72,
  2049. VK.UP,
  2050. ConsoleModifiers.Shift,
  2051. '\0'
  2052. ),
  2053. new ScanCodeMapping (
  2054. 73,
  2055. VK.PRIOR,
  2056. 0,
  2057. '\0'
  2058. ), // Page Up
  2059. new ScanCodeMapping (
  2060. 73,
  2061. VK.PRIOR,
  2062. ConsoleModifiers.Shift,
  2063. '\0'
  2064. ),
  2065. new ScanCodeMapping (
  2066. 74,
  2067. VK.SUBTRACT,
  2068. 0,
  2069. '-'
  2070. ), // Subtract (Num Pad '-')
  2071. new ScanCodeMapping (
  2072. 74,
  2073. VK.SUBTRACT,
  2074. ConsoleModifiers.Shift,
  2075. '-'
  2076. ),
  2077. new ScanCodeMapping (
  2078. 75,
  2079. VK.LEFT,
  2080. 0,
  2081. '\0'
  2082. ), // Left Arrow
  2083. new ScanCodeMapping (
  2084. 75,
  2085. VK.LEFT,
  2086. ConsoleModifiers.Shift,
  2087. '\0'
  2088. ),
  2089. new ScanCodeMapping (
  2090. 76,
  2091. VK.CLEAR,
  2092. 0,
  2093. '\0'
  2094. ), // Center key (Num Pad 5 with Num Lock off)
  2095. new ScanCodeMapping (
  2096. 76,
  2097. VK.CLEAR,
  2098. ConsoleModifiers.Shift,
  2099. '\0'
  2100. ),
  2101. new ScanCodeMapping (
  2102. 77,
  2103. VK.RIGHT,
  2104. 0,
  2105. '\0'
  2106. ), // Right Arrow
  2107. new ScanCodeMapping (
  2108. 77,
  2109. VK.RIGHT,
  2110. ConsoleModifiers.Shift,
  2111. '\0'
  2112. ),
  2113. new ScanCodeMapping (
  2114. 78,
  2115. VK.ADD,
  2116. 0,
  2117. '+'
  2118. ), // Add (Num Pad '+')
  2119. new ScanCodeMapping (
  2120. 78,
  2121. VK.ADD,
  2122. ConsoleModifiers.Shift,
  2123. '+'
  2124. ),
  2125. new ScanCodeMapping (
  2126. 79,
  2127. VK.END,
  2128. 0,
  2129. '\0'
  2130. ), // End
  2131. new ScanCodeMapping (
  2132. 79,
  2133. VK.END,
  2134. ConsoleModifiers.Shift,
  2135. '\0'
  2136. ),
  2137. new ScanCodeMapping (
  2138. 80,
  2139. VK.DOWN,
  2140. 0,
  2141. '\0'
  2142. ), // Down Arrow
  2143. new ScanCodeMapping (
  2144. 80,
  2145. VK.DOWN,
  2146. ConsoleModifiers.Shift,
  2147. '\0'
  2148. ),
  2149. new ScanCodeMapping (
  2150. 81,
  2151. VK.NEXT,
  2152. 0,
  2153. '\0'
  2154. ), // Page Down
  2155. new ScanCodeMapping (
  2156. 81,
  2157. VK.NEXT,
  2158. ConsoleModifiers.Shift,
  2159. '\0'
  2160. ),
  2161. new ScanCodeMapping (
  2162. 82,
  2163. VK.INSERT,
  2164. 0,
  2165. '\0'
  2166. ), // Insert
  2167. new ScanCodeMapping (
  2168. 82,
  2169. VK.INSERT,
  2170. ConsoleModifiers.Shift,
  2171. '\0'
  2172. ),
  2173. new ScanCodeMapping (
  2174. 83,
  2175. VK.DELETE,
  2176. 0,
  2177. '\0'
  2178. ), // Delete
  2179. new ScanCodeMapping (
  2180. 83,
  2181. VK.DELETE,
  2182. ConsoleModifiers.Shift,
  2183. '\0'
  2184. ),
  2185. new ScanCodeMapping (
  2186. 86,
  2187. VK.OEM_102,
  2188. 0,
  2189. '<'
  2190. ), // OEM 102 (Typically '<' or '|' key next to Left Shift)
  2191. new ScanCodeMapping (
  2192. 86,
  2193. VK.OEM_102,
  2194. ConsoleModifiers.Shift,
  2195. '>'
  2196. ),
  2197. new ScanCodeMapping (
  2198. 87,
  2199. VK.F11,
  2200. 0,
  2201. '\0'
  2202. ), // F11
  2203. new ScanCodeMapping (
  2204. 87,
  2205. VK.F11,
  2206. ConsoleModifiers.Shift,
  2207. '\0'
  2208. ),
  2209. new ScanCodeMapping (
  2210. 88,
  2211. VK.F12,
  2212. 0,
  2213. '\0'
  2214. ), // F12
  2215. new ScanCodeMapping (
  2216. 88,
  2217. VK.F12,
  2218. ConsoleModifiers.Shift,
  2219. '\0'
  2220. )
  2221. };
  2222. /// <summary>Decode a <see cref="ConsoleKeyInfo"/> that is using <see cref="ConsoleKey.Packet"/>.</summary>
  2223. /// <param name="consoleKeyInfo">The console key info.</param>
  2224. /// <returns>The decoded <see cref="ConsoleKeyInfo"/> or the <paramref name="consoleKeyInfo"/>.</returns>
  2225. /// <remarks>
  2226. /// If it's a <see cref="ConsoleKey.Packet"/> the <see cref="ConsoleKeyInfo.KeyChar"/> may be a
  2227. /// <see cref="ConsoleKeyInfo.Key"/> or a <see cref="ConsoleKeyInfo.KeyChar"/> value.
  2228. /// </remarks>
  2229. public static ConsoleKeyInfo DecodeVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  2230. {
  2231. if (consoleKeyInfo.Key != ConsoleKey.Packet)
  2232. {
  2233. return consoleKeyInfo;
  2234. }
  2235. return GetConsoleKeyInfoFromKeyChar (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out _);
  2236. }
  2237. /// <summary>
  2238. /// Encode the <see cref="ConsoleKeyInfo.KeyChar"/> with the <see cref="ConsoleKeyInfo.Key"/> if the first a byte
  2239. /// length, otherwise only the KeyChar is considered and searched on the database.
  2240. /// </summary>
  2241. /// <param name="consoleKeyInfo">The console key info.</param>
  2242. /// <returns>The encoded KeyChar with the Key if both can be shifted, otherwise only the KeyChar.</returns>
  2243. /// <remarks>This is useful to use with the <see cref="ConsoleKey.Packet"/>.</remarks>
  2244. public static char EncodeKeyCharForVKPacket (ConsoleKeyInfo consoleKeyInfo)
  2245. {
  2246. char keyChar = consoleKeyInfo.KeyChar;
  2247. ConsoleKey consoleKey = consoleKeyInfo.Key;
  2248. if (keyChar != 0 && consoleKeyInfo.KeyChar < byte.MaxValue && consoleKey == ConsoleKey.None)
  2249. {
  2250. // try to get the ConsoleKey
  2251. ScanCodeMapping scode = _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyChar);
  2252. if (scode is { })
  2253. {
  2254. consoleKey = (ConsoleKey)scode.VirtualKey;
  2255. }
  2256. }
  2257. if (keyChar < byte.MaxValue && consoleKey != ConsoleKey.None)
  2258. {
  2259. keyChar = (char)((consoleKeyInfo.KeyChar << 8) | (byte)consoleKey);
  2260. }
  2261. return keyChar;
  2262. }
  2263. }