ConsoleKeyMapping.cs 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  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. }
  612. isConsoleKey = false;
  613. return (uint)keyValue;
  614. }
  615. /// <summary>Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.</summary>
  616. /// <param name="consoleKeyInfo">The console key.</param>
  617. /// <returns>The <see cref="KeyCode"/> or the <paramref name="consoleKeyInfo"/>.</returns>
  618. public static KeyCode MapConsoleKeyInfoToKeyCode (ConsoleKeyInfo consoleKeyInfo)
  619. {
  620. KeyCode keyCode;
  621. switch (consoleKeyInfo.Key)
  622. {
  623. case ConsoleKey.Enter:
  624. keyCode = KeyCode.Enter;
  625. break;
  626. case ConsoleKey.Delete:
  627. keyCode = KeyCode.Delete;
  628. break;
  629. case ConsoleKey.UpArrow:
  630. keyCode = KeyCode.CursorUp;
  631. break;
  632. case ConsoleKey.DownArrow:
  633. keyCode = KeyCode.CursorDown;
  634. break;
  635. case ConsoleKey.LeftArrow:
  636. keyCode = KeyCode.CursorLeft;
  637. break;
  638. case ConsoleKey.RightArrow:
  639. keyCode = KeyCode.CursorRight;
  640. break;
  641. case ConsoleKey.PageUp:
  642. keyCode = KeyCode.PageUp;
  643. break;
  644. case ConsoleKey.PageDown:
  645. keyCode = KeyCode.PageDown;
  646. break;
  647. case ConsoleKey.Home:
  648. keyCode = KeyCode.Home;
  649. break;
  650. case ConsoleKey.End:
  651. keyCode = KeyCode.End;
  652. break;
  653. case ConsoleKey.Insert:
  654. keyCode = KeyCode.Insert;
  655. break;
  656. case ConsoleKey.F1:
  657. keyCode = KeyCode.F1;
  658. break;
  659. case ConsoleKey.F2:
  660. keyCode = KeyCode.F2;
  661. break;
  662. case ConsoleKey.F3:
  663. keyCode = KeyCode.F3;
  664. break;
  665. case ConsoleKey.F4:
  666. keyCode = KeyCode.F4;
  667. break;
  668. case ConsoleKey.F5:
  669. keyCode = KeyCode.F5;
  670. break;
  671. case ConsoleKey.F6:
  672. keyCode = KeyCode.F6;
  673. break;
  674. case ConsoleKey.F7:
  675. keyCode = KeyCode.F7;
  676. break;
  677. case ConsoleKey.F8:
  678. keyCode = KeyCode.F8;
  679. break;
  680. case ConsoleKey.F9:
  681. keyCode = KeyCode.F9;
  682. break;
  683. case ConsoleKey.F10:
  684. keyCode = KeyCode.F10;
  685. break;
  686. case ConsoleKey.F11:
  687. keyCode = KeyCode.F11;
  688. break;
  689. case ConsoleKey.F12:
  690. keyCode = KeyCode.F12;
  691. break;
  692. case ConsoleKey.F13:
  693. keyCode = KeyCode.F13;
  694. break;
  695. case ConsoleKey.F14:
  696. keyCode = KeyCode.F14;
  697. break;
  698. case ConsoleKey.F15:
  699. keyCode = KeyCode.F15;
  700. break;
  701. case ConsoleKey.F16:
  702. keyCode = KeyCode.F16;
  703. break;
  704. case ConsoleKey.F17:
  705. keyCode = KeyCode.F17;
  706. break;
  707. case ConsoleKey.F18:
  708. keyCode = KeyCode.F18;
  709. break;
  710. case ConsoleKey.F19:
  711. keyCode = KeyCode.F19;
  712. break;
  713. case ConsoleKey.F20:
  714. keyCode = KeyCode.F20;
  715. break;
  716. case ConsoleKey.F21:
  717. keyCode = KeyCode.F21;
  718. break;
  719. case ConsoleKey.F22:
  720. keyCode = KeyCode.F22;
  721. break;
  722. case ConsoleKey.F23:
  723. keyCode = KeyCode.F23;
  724. break;
  725. case ConsoleKey.F24:
  726. keyCode = KeyCode.F24;
  727. break;
  728. case ConsoleKey.Clear:
  729. keyCode = KeyCode.Clear;
  730. break;
  731. case ConsoleKey.Tab:
  732. keyCode = KeyCode.Tab;
  733. break;
  734. default:
  735. if ((int)consoleKeyInfo.KeyChar is >= 1 and <= 26)
  736. {
  737. keyCode = (KeyCode)(consoleKeyInfo.KeyChar + 64);
  738. }
  739. else
  740. {
  741. keyCode = (KeyCode)consoleKeyInfo.KeyChar;
  742. }
  743. break;
  744. }
  745. keyCode |= MapToKeyCodeModifiers (consoleKeyInfo.Modifiers, keyCode);
  746. return keyCode;
  747. }
  748. /// <summary>Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.</summary>
  749. /// <param name="modifiers">The console modifiers.</param>
  750. /// <param name="key">The key code.</param>
  751. /// <returns>The <see cref="KeyCode"/> with <see cref="ConsoleModifiers"/> or the <paramref name="key"/></returns>
  752. public static KeyCode MapToKeyCodeModifiers (ConsoleModifiers modifiers, KeyCode key)
  753. {
  754. var keyMod = new KeyCode ();
  755. if ((modifiers & ConsoleModifiers.Shift) != 0)
  756. {
  757. keyMod = KeyCode.ShiftMask;
  758. }
  759. if ((modifiers & ConsoleModifiers.Control) != 0)
  760. {
  761. keyMod |= KeyCode.CtrlMask;
  762. }
  763. if ((modifiers & ConsoleModifiers.Alt) != 0)
  764. {
  765. keyMod |= KeyCode.AltMask;
  766. }
  767. return keyMod != KeyCode.Null ? keyMod | key : key;
  768. }
  769. /// <summary>Generated from winuser.h. See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes</summary>
  770. public enum VK : ushort
  771. {
  772. /// <summary>Left mouse button.</summary>
  773. LBUTTON = 0x01,
  774. /// <summary>Right mouse button.</summary>
  775. RBUTTON = 0x02,
  776. /// <summary>Control-break processing.</summary>
  777. CANCEL = 0x03,
  778. /// <summary>Middle mouse button (three-button mouse).</summary>
  779. MBUTTON = 0x04,
  780. /// <summary>X1 mouse button.</summary>
  781. XBUTTON1 = 0x05,
  782. /// <summary>X2 mouse button.</summary>
  783. XBUTTON2 = 0x06,
  784. /// <summary>BACKSPACE key.</summary>
  785. BACK = 0x08,
  786. /// <summary>TAB key.</summary>
  787. TAB = 0x09,
  788. /// <summary>CLEAR key.</summary>
  789. CLEAR = 0x0C,
  790. /// <summary>ENTER key.</summary>
  791. RETURN = 0x0D,
  792. /// <summary>SHIFT key.</summary>
  793. SHIFT = 0x10,
  794. /// <summary>CTRL key.</summary>
  795. CONTROL = 0x11,
  796. /// <summary>ALT key.</summary>
  797. MENU = 0x12,
  798. /// <summary>PAUSE key.</summary>
  799. PAUSE = 0x13,
  800. /// <summary>CAPS LOCK key.</summary>
  801. CAPITAL = 0x14,
  802. /// <summary>IME Kana mode.</summary>
  803. KANA = 0x15,
  804. /// <summary>IME Hangul mode.</summary>
  805. HANGUL = 0x15,
  806. /// <summary>IME Junja mode.</summary>
  807. JUNJA = 0x17,
  808. /// <summary>IME final mode.</summary>
  809. FINAL = 0x18,
  810. /// <summary>IME Hanja mode.</summary>
  811. HANJA = 0x19,
  812. /// <summary>IME Kanji mode.</summary>
  813. KANJI = 0x19,
  814. /// <summary>ESC key.</summary>
  815. ESCAPE = 0x1B,
  816. /// <summary>IME convert.</summary>
  817. CONVERT = 0x1C,
  818. /// <summary>IME nonconvert.</summary>
  819. NONCONVERT = 0x1D,
  820. /// <summary>IME accept.</summary>
  821. ACCEPT = 0x1E,
  822. /// <summary>IME mode change request.</summary>
  823. MODECHANGE = 0x1F,
  824. /// <summary>SPACEBAR.</summary>
  825. SPACE = 0x20,
  826. /// <summary>PAGE UP key.</summary>
  827. PRIOR = 0x21,
  828. /// <summary>PAGE DOWN key.</summary>
  829. NEXT = 0x22,
  830. /// <summary>END key.</summary>
  831. END = 0x23,
  832. /// <summary>HOME key.</summary>
  833. HOME = 0x24,
  834. /// <summary>LEFT ARROW key.</summary>
  835. LEFT = 0x25,
  836. /// <summary>UP ARROW key.</summary>
  837. UP = 0x26,
  838. /// <summary>RIGHT ARROW key.</summary>
  839. RIGHT = 0x27,
  840. /// <summary>DOWN ARROW key.</summary>
  841. DOWN = 0x28,
  842. /// <summary>SELECT key.</summary>
  843. SELECT = 0x29,
  844. /// <summary>PRINT key.</summary>
  845. PRINT = 0x2A,
  846. /// <summary>EXECUTE key</summary>
  847. EXECUTE = 0x2B,
  848. /// <summary>PRINT SCREEN key</summary>
  849. SNAPSHOT = 0x2C,
  850. /// <summary>INS key</summary>
  851. INSERT = 0x2D,
  852. /// <summary>DEL key</summary>
  853. DELETE = 0x2E,
  854. /// <summary>HELP key</summary>
  855. HELP = 0x2F,
  856. /// <summary>Left Windows key (Natural keyboard)</summary>
  857. LWIN = 0x5B,
  858. /// <summary>Right Windows key (Natural keyboard)</summary>
  859. RWIN = 0x5C,
  860. /// <summary>Applications key (Natural keyboard)</summary>
  861. APPS = 0x5D,
  862. /// <summary>Computer Sleep key</summary>
  863. SLEEP = 0x5F,
  864. /// <summary>Numeric keypad 0 key</summary>
  865. NUMPAD0 = 0x60,
  866. /// <summary>Numeric keypad 1 key</summary>
  867. NUMPAD1 = 0x61,
  868. /// <summary>Numeric keypad 2 key</summary>
  869. NUMPAD2 = 0x62,
  870. /// <summary>Numeric keypad 3 key</summary>
  871. NUMPAD3 = 0x63,
  872. /// <summary>Numeric keypad 4 key</summary>
  873. NUMPAD4 = 0x64,
  874. /// <summary>Numeric keypad 5 key</summary>
  875. NUMPAD5 = 0x65,
  876. /// <summary>Numeric keypad 6 key</summary>
  877. NUMPAD6 = 0x66,
  878. /// <summary>Numeric keypad 7 key</summary>
  879. NUMPAD7 = 0x67,
  880. /// <summary>Numeric keypad 8 key</summary>
  881. NUMPAD8 = 0x68,
  882. /// <summary>Numeric keypad 9 key</summary>
  883. NUMPAD9 = 0x69,
  884. /// <summary>Multiply key</summary>
  885. MULTIPLY = 0x6A,
  886. /// <summary>Add key</summary>
  887. ADD = 0x6B,
  888. /// <summary>Separator key</summary>
  889. SEPARATOR = 0x6C,
  890. /// <summary>Subtract key</summary>
  891. SUBTRACT = 0x6D,
  892. /// <summary>Decimal key</summary>
  893. DECIMAL = 0x6E,
  894. /// <summary>Divide key</summary>
  895. DIVIDE = 0x6F,
  896. /// <summary>F1 key</summary>
  897. F1 = 0x70,
  898. /// <summary>F2 key</summary>
  899. F2 = 0x71,
  900. /// <summary>F3 key</summary>
  901. F3 = 0x72,
  902. /// <summary>F4 key</summary>
  903. F4 = 0x73,
  904. /// <summary>F5 key</summary>
  905. F5 = 0x74,
  906. /// <summary>F6 key</summary>
  907. F6 = 0x75,
  908. /// <summary>F7 key</summary>
  909. F7 = 0x76,
  910. /// <summary>F8 key</summary>
  911. F8 = 0x77,
  912. /// <summary>F9 key</summary>
  913. F9 = 0x78,
  914. /// <summary>F10 key</summary>
  915. F10 = 0x79,
  916. /// <summary>F11 key</summary>
  917. F11 = 0x7A,
  918. /// <summary>F12 key</summary>
  919. F12 = 0x7B,
  920. /// <summary>F13 key</summary>
  921. F13 = 0x7C,
  922. /// <summary>F14 key</summary>
  923. F14 = 0x7D,
  924. /// <summary>F15 key</summary>
  925. F15 = 0x7E,
  926. /// <summary>F16 key</summary>
  927. F16 = 0x7F,
  928. /// <summary>F17 key</summary>
  929. F17 = 0x80,
  930. /// <summary>F18 key</summary>
  931. F18 = 0x81,
  932. /// <summary>F19 key</summary>
  933. F19 = 0x82,
  934. /// <summary>F20 key</summary>
  935. F20 = 0x83,
  936. /// <summary>F21 key</summary>
  937. F21 = 0x84,
  938. /// <summary>F22 key</summary>
  939. F22 = 0x85,
  940. /// <summary>F23 key</summary>
  941. F23 = 0x86,
  942. /// <summary>F24 key</summary>
  943. F24 = 0x87,
  944. /// <summary>NUM LOCK key</summary>
  945. NUMLOCK = 0x90,
  946. /// <summary>SCROLL LOCK key</summary>
  947. SCROLL = 0x91,
  948. /// <summary>NEC PC-9800 kbd definition: '=' key on numpad</summary>
  949. OEM_NEC_EQUAL = 0x92,
  950. /// <summary>Fujitsu/OASYS kbd definition: 'Dictionary' key</summary>
  951. OEM_FJ_JISHO = 0x92,
  952. /// <summary>Fujitsu/OASYS kbd definition: 'Unregister word' key</summary>
  953. OEM_FJ_MASSHOU = 0x93,
  954. /// <summary>Fujitsu/OASYS kbd definition: 'Register word' key</summary>
  955. OEM_FJ_TOUROKU = 0x94,
  956. /// <summary>Fujitsu/OASYS kbd definition: 'Left OYAYUBI' key</summary>
  957. OEM_FJ_LOYA = 0x95,
  958. /// <summary>Fujitsu/OASYS kbd definition: 'Right OYAYUBI' key</summary>
  959. OEM_FJ_ROYA = 0x96,
  960. /// <summary>Left SHIFT key</summary>
  961. LSHIFT = 0xA0,
  962. /// <summary>Right SHIFT key</summary>
  963. RSHIFT = 0xA1,
  964. /// <summary>Left CONTROL key</summary>
  965. LCONTROL = 0xA2,
  966. /// <summary>Right CONTROL key</summary>
  967. RCONTROL = 0xA3,
  968. /// <summary>Left MENU key (Left Alt key)</summary>
  969. LMENU = 0xA4,
  970. /// <summary>Right MENU key (Right Alt key)</summary>
  971. RMENU = 0xA5,
  972. /// <summary>Browser Back key</summary>
  973. BROWSER_BACK = 0xA6,
  974. /// <summary>Browser Forward key</summary>
  975. BROWSER_FORWARD = 0xA7,
  976. /// <summary>Browser Refresh key</summary>
  977. BROWSER_REFRESH = 0xA8,
  978. /// <summary>Browser Stop key</summary>
  979. BROWSER_STOP = 0xA9,
  980. /// <summary>Browser Search key</summary>
  981. BROWSER_SEARCH = 0xAA,
  982. /// <summary>Browser Favorites key</summary>
  983. BROWSER_FAVORITES = 0xAB,
  984. /// <summary>Browser Home key</summary>
  985. BROWSER_HOME = 0xAC,
  986. /// <summary>Volume Mute key</summary>
  987. VOLUME_MUTE = 0xAD,
  988. /// <summary>Volume Down key</summary>
  989. VOLUME_DOWN = 0xAE,
  990. /// <summary>Volume Up key</summary>
  991. VOLUME_UP = 0xAF,
  992. /// <summary>Next Track key</summary>
  993. MEDIA_NEXT_TRACK = 0xB0,
  994. /// <summary>Previous Track key</summary>
  995. MEDIA_PREV_TRACK = 0xB1,
  996. /// <summary>Stop Media key</summary>
  997. MEDIA_STOP = 0xB2,
  998. /// <summary>Play/Pause Media key</summary>
  999. MEDIA_PLAY_PAUSE = 0xB3,
  1000. /// <summary>Start Mail key</summary>
  1001. LAUNCH_MAIL = 0xB4,
  1002. /// <summary>Select Media key</summary>
  1003. LAUNCH_MEDIA_SELECT = 0xB5,
  1004. /// <summary>Start Application 1 key</summary>
  1005. LAUNCH_APP1 = 0xB6,
  1006. /// <summary>Start Application 2 key</summary>
  1007. LAUNCH_APP2 = 0xB7,
  1008. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key</summary>
  1009. OEM_1 = 0xBA,
  1010. /// <summary>For any country/region, the '+' key</summary>
  1011. OEM_PLUS = 0xBB,
  1012. /// <summary>For any country/region, the ',' key</summary>
  1013. OEM_COMMA = 0xBC,
  1014. /// <summary>For any country/region, the '-' key</summary>
  1015. OEM_MINUS = 0xBD,
  1016. /// <summary>For any country/region, the '.' key</summary>
  1017. OEM_PERIOD = 0xBE,
  1018. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key</summary>
  1019. OEM_2 = 0xBF,
  1020. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key</summary>
  1021. OEM_3 = 0xC0,
  1022. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key</summary>
  1023. OEM_4 = 0xDB,
  1024. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key</summary>
  1025. OEM_5 = 0xDC,
  1026. /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key</summary>
  1027. OEM_6 = 0xDD,
  1028. /// <summary>
  1029. /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the
  1030. /// 'single-quote/double-quote' key
  1031. /// </summary>
  1032. OEM_7 = 0xDE,
  1033. /// <summary>Used for miscellaneous characters; it can vary by keyboard.</summary>
  1034. OEM_8 = 0xDF,
  1035. /// <summary>'AX' key on Japanese AX kbd</summary>
  1036. OEM_AX = 0xE1,
  1037. /// <summary>Either the angle bracket key or the backslash key on the RT 102-key keyboard</summary>
  1038. OEM_102 = 0xE2,
  1039. /// <summary>Help key on ICO</summary>
  1040. ICO_HELP = 0xE3,
  1041. /// <summary>00 key on ICO</summary>
  1042. ICO_00 = 0xE4,
  1043. /// <summary>Process key</summary>
  1044. PROCESSKEY = 0xE5,
  1045. /// <summary>Clear key on ICO</summary>
  1046. ICO_CLEAR = 0xE6,
  1047. /// <summary>Packet key to be used to pass Unicode characters as if they were keystrokes</summary>
  1048. PACKET = 0xE7,
  1049. /// <summary>Reset key</summary>
  1050. OEM_RESET = 0xE9,
  1051. /// <summary>Jump key</summary>
  1052. OEM_JUMP = 0xEA,
  1053. /// <summary>PA1 key</summary>
  1054. OEM_PA1 = 0xEB,
  1055. /// <summary>PA2 key</summary>
  1056. OEM_PA2 = 0xEC,
  1057. /// <summary>PA3 key</summary>
  1058. OEM_PA3 = 0xED,
  1059. /// <summary>WsCtrl key</summary>
  1060. OEM_WSCTRL = 0xEE,
  1061. /// <summary>CuSel key</summary>
  1062. OEM_CUSEL = 0xEF,
  1063. /// <summary>Attn key</summary>
  1064. OEM_ATTN = 0xF0,
  1065. /// <summary>Finish key</summary>
  1066. OEM_FINISH = 0xF1,
  1067. /// <summary>Copy key</summary>
  1068. OEM_COPY = 0xF2,
  1069. /// <summary>Auto key</summary>
  1070. OEM_AUTO = 0xF3,
  1071. /// <summary>Enlw key</summary>
  1072. OEM_ENLW = 0xF4,
  1073. /// <summary>BackTab key</summary>
  1074. OEM_BACKTAB = 0xF5,
  1075. /// <summary>Attn key</summary>
  1076. ATTN = 0xF6,
  1077. /// <summary>CrSel key</summary>
  1078. CRSEL = 0xF7,
  1079. /// <summary>ExSel key</summary>
  1080. EXSEL = 0xF8,
  1081. /// <summary>Erase EOF key</summary>
  1082. EREOF = 0xF9,
  1083. /// <summary>Play key</summary>
  1084. PLAY = 0xFA,
  1085. /// <summary>Zoom key</summary>
  1086. ZOOM = 0xFB,
  1087. /// <summary>Reserved</summary>
  1088. NONAME = 0xFC,
  1089. /// <summary>PA1 key</summary>
  1090. PA1 = 0xFD,
  1091. /// <summary>Clear key</summary>
  1092. OEM_CLEAR = 0xFE
  1093. }
  1094. // BUGBUG: This database makes no sense. It is not possible to map a VK code to a character without knowing the keyboard layout
  1095. // It should be deleted.
  1096. private static readonly HashSet<ScanCodeMapping> _scanCodes = new ()
  1097. {
  1098. new ScanCodeMapping (
  1099. 1,
  1100. VK.ESCAPE,
  1101. 0,
  1102. '\u001B'
  1103. ), // Escape
  1104. new ScanCodeMapping (
  1105. 1,
  1106. VK.ESCAPE,
  1107. ConsoleModifiers.Shift,
  1108. '\u001B'
  1109. ),
  1110. new ScanCodeMapping (
  1111. 2,
  1112. (VK)'1',
  1113. 0,
  1114. '1'
  1115. ), // D1
  1116. new ScanCodeMapping (
  1117. 2,
  1118. (VK)'1',
  1119. ConsoleModifiers.Shift,
  1120. '!'
  1121. ),
  1122. new ScanCodeMapping (
  1123. 3,
  1124. (VK)'2',
  1125. 0,
  1126. '2'
  1127. ), // D2
  1128. new ScanCodeMapping (
  1129. 3,
  1130. (VK)'2',
  1131. ConsoleModifiers.Shift,
  1132. '\"'
  1133. ), // BUGBUG: This is true for Portuguese keyboard, but not ENG (@) or DEU (")
  1134. new ScanCodeMapping (
  1135. 3,
  1136. (VK)'2',
  1137. ConsoleModifiers.Alt
  1138. | ConsoleModifiers.Control,
  1139. '@'
  1140. ),
  1141. new ScanCodeMapping (
  1142. 4,
  1143. (VK)'3',
  1144. 0,
  1145. '3'
  1146. ), // D3
  1147. new ScanCodeMapping (
  1148. 4,
  1149. (VK)'3',
  1150. ConsoleModifiers.Shift,
  1151. '#'
  1152. ),
  1153. new ScanCodeMapping (
  1154. 4,
  1155. (VK)'3',
  1156. ConsoleModifiers.Alt
  1157. | ConsoleModifiers.Control,
  1158. '£'
  1159. ),
  1160. new ScanCodeMapping (
  1161. 5,
  1162. (VK)'4',
  1163. 0,
  1164. '4'
  1165. ), // D4
  1166. new ScanCodeMapping (
  1167. 5,
  1168. (VK)'4',
  1169. ConsoleModifiers.Shift,
  1170. '$'
  1171. ),
  1172. new ScanCodeMapping (
  1173. 5,
  1174. (VK)'4',
  1175. ConsoleModifiers.Alt
  1176. | ConsoleModifiers.Control,
  1177. '§'
  1178. ),
  1179. new ScanCodeMapping (
  1180. 6,
  1181. (VK)'5',
  1182. 0,
  1183. '5'
  1184. ), // D5
  1185. new ScanCodeMapping (
  1186. 6,
  1187. (VK)'5',
  1188. ConsoleModifiers.Shift,
  1189. '%'
  1190. ),
  1191. new ScanCodeMapping (
  1192. 6,
  1193. (VK)'5',
  1194. ConsoleModifiers.Alt
  1195. | ConsoleModifiers.Control,
  1196. '€'
  1197. ),
  1198. new ScanCodeMapping (
  1199. 7,
  1200. (VK)'6',
  1201. 0,
  1202. '6'
  1203. ), // D6
  1204. new ScanCodeMapping (
  1205. 7,
  1206. (VK)'6',
  1207. ConsoleModifiers.Shift,
  1208. '&'
  1209. ),
  1210. new ScanCodeMapping (
  1211. 8,
  1212. (VK)'7',
  1213. 0,
  1214. '7'
  1215. ), // D7
  1216. new ScanCodeMapping (
  1217. 8,
  1218. (VK)'7',
  1219. ConsoleModifiers.Shift,
  1220. '/'
  1221. ),
  1222. new ScanCodeMapping (
  1223. 8,
  1224. (VK)'7',
  1225. ConsoleModifiers.Alt
  1226. | ConsoleModifiers.Control,
  1227. '{'
  1228. ),
  1229. new ScanCodeMapping (
  1230. 9,
  1231. (VK)'8',
  1232. 0,
  1233. '8'
  1234. ), // D8
  1235. new ScanCodeMapping (
  1236. 9,
  1237. (VK)'8',
  1238. ConsoleModifiers.Shift,
  1239. '('
  1240. ),
  1241. new ScanCodeMapping (
  1242. 9,
  1243. (VK)'8',
  1244. ConsoleModifiers.Alt
  1245. | ConsoleModifiers.Control,
  1246. '['
  1247. ),
  1248. new ScanCodeMapping (
  1249. 10,
  1250. (VK)'9',
  1251. 0,
  1252. '9'
  1253. ), // D9
  1254. new ScanCodeMapping (
  1255. 10,
  1256. (VK)'9',
  1257. ConsoleModifiers.Shift,
  1258. ')'
  1259. ),
  1260. new ScanCodeMapping (
  1261. 10,
  1262. (VK)'9',
  1263. ConsoleModifiers.Alt
  1264. | ConsoleModifiers.Control,
  1265. ']'
  1266. ),
  1267. new ScanCodeMapping (
  1268. 11,
  1269. (VK)'0',
  1270. 0,
  1271. '0'
  1272. ), // D0
  1273. new ScanCodeMapping (
  1274. 11,
  1275. (VK)'0',
  1276. ConsoleModifiers.Shift,
  1277. '='
  1278. ),
  1279. new ScanCodeMapping (
  1280. 11,
  1281. (VK)'0',
  1282. ConsoleModifiers.Alt
  1283. | ConsoleModifiers.Control,
  1284. '}'
  1285. ),
  1286. new ScanCodeMapping (
  1287. 12,
  1288. VK.OEM_4,
  1289. 0,
  1290. '\''
  1291. ), // Oem4
  1292. new ScanCodeMapping (
  1293. 12,
  1294. VK.OEM_4,
  1295. ConsoleModifiers.Shift,
  1296. '?'
  1297. ),
  1298. new ScanCodeMapping (
  1299. 13,
  1300. VK.OEM_6,
  1301. 0,
  1302. '+'
  1303. ), // Oem6
  1304. new ScanCodeMapping (
  1305. 13,
  1306. VK.OEM_6,
  1307. ConsoleModifiers.Shift,
  1308. '*'
  1309. ),
  1310. new ScanCodeMapping (
  1311. 14,
  1312. VK.BACK,
  1313. 0,
  1314. '\u0008'
  1315. ), // Backspace
  1316. new ScanCodeMapping (
  1317. 14,
  1318. VK.BACK,
  1319. ConsoleModifiers.Shift,
  1320. '\u0008'
  1321. ),
  1322. new ScanCodeMapping (
  1323. 15,
  1324. VK.TAB,
  1325. 0,
  1326. '\u0009'
  1327. ), // Tab
  1328. new ScanCodeMapping (
  1329. 15,
  1330. VK.TAB,
  1331. ConsoleModifiers.Shift,
  1332. '\u000F'
  1333. ),
  1334. new ScanCodeMapping (
  1335. 16,
  1336. (VK)'Q',
  1337. 0,
  1338. 'q'
  1339. ), // Q
  1340. new ScanCodeMapping (
  1341. 16,
  1342. (VK)'Q',
  1343. ConsoleModifiers.Shift,
  1344. 'Q'
  1345. ),
  1346. new ScanCodeMapping (
  1347. 17,
  1348. (VK)'W',
  1349. 0,
  1350. 'w'
  1351. ), // W
  1352. new ScanCodeMapping (
  1353. 17,
  1354. (VK)'W',
  1355. ConsoleModifiers.Shift,
  1356. 'W'
  1357. ),
  1358. new ScanCodeMapping (
  1359. 18,
  1360. (VK)'E',
  1361. 0,
  1362. 'e'
  1363. ), // E
  1364. new ScanCodeMapping (
  1365. 18,
  1366. (VK)'E',
  1367. ConsoleModifiers.Shift,
  1368. 'E'
  1369. ),
  1370. new ScanCodeMapping (
  1371. 19,
  1372. (VK)'R',
  1373. 0,
  1374. 'r'
  1375. ), // R
  1376. new ScanCodeMapping (
  1377. 19,
  1378. (VK)'R',
  1379. ConsoleModifiers.Shift,
  1380. 'R'
  1381. ),
  1382. new ScanCodeMapping (
  1383. 20,
  1384. (VK)'T',
  1385. 0,
  1386. 't'
  1387. ), // T
  1388. new ScanCodeMapping (
  1389. 20,
  1390. (VK)'T',
  1391. ConsoleModifiers.Shift,
  1392. 'T'
  1393. ),
  1394. new ScanCodeMapping (
  1395. 21,
  1396. (VK)'Y',
  1397. 0,
  1398. 'y'
  1399. ), // Y
  1400. new ScanCodeMapping (
  1401. 21,
  1402. (VK)'Y',
  1403. ConsoleModifiers.Shift,
  1404. 'Y'
  1405. ),
  1406. new ScanCodeMapping (
  1407. 22,
  1408. (VK)'U',
  1409. 0,
  1410. 'u'
  1411. ), // U
  1412. new ScanCodeMapping (
  1413. 22,
  1414. (VK)'U',
  1415. ConsoleModifiers.Shift,
  1416. 'U'
  1417. ),
  1418. new ScanCodeMapping (
  1419. 23,
  1420. (VK)'I',
  1421. 0,
  1422. 'i'
  1423. ), // I
  1424. new ScanCodeMapping (
  1425. 23,
  1426. (VK)'I',
  1427. ConsoleModifiers.Shift,
  1428. 'I'
  1429. ),
  1430. new ScanCodeMapping (
  1431. 24,
  1432. (VK)'O',
  1433. 0,
  1434. 'o'
  1435. ), // O
  1436. new ScanCodeMapping (
  1437. 24,
  1438. (VK)'O',
  1439. ConsoleModifiers.Shift,
  1440. 'O'
  1441. ),
  1442. new ScanCodeMapping (
  1443. 25,
  1444. (VK)'P',
  1445. 0,
  1446. 'p'
  1447. ), // P
  1448. new ScanCodeMapping (
  1449. 25,
  1450. (VK)'P',
  1451. ConsoleModifiers.Shift,
  1452. 'P'
  1453. ),
  1454. new ScanCodeMapping (
  1455. 26,
  1456. VK.OEM_PLUS,
  1457. 0,
  1458. '+'
  1459. ), // OemPlus
  1460. new ScanCodeMapping (
  1461. 26,
  1462. VK.OEM_PLUS,
  1463. ConsoleModifiers.Shift,
  1464. '*'
  1465. ),
  1466. new ScanCodeMapping (
  1467. 26,
  1468. VK.OEM_PLUS,
  1469. ConsoleModifiers.Alt
  1470. | ConsoleModifiers.Control,
  1471. '¨'
  1472. ),
  1473. new ScanCodeMapping (
  1474. 27,
  1475. VK.OEM_1,
  1476. 0,
  1477. '´'
  1478. ), // Oem1
  1479. new ScanCodeMapping (
  1480. 27,
  1481. VK.OEM_1,
  1482. ConsoleModifiers.Shift,
  1483. '`'
  1484. ),
  1485. new ScanCodeMapping (
  1486. 28,
  1487. VK.RETURN,
  1488. 0,
  1489. '\u000D'
  1490. ), // Enter
  1491. new ScanCodeMapping (
  1492. 28,
  1493. VK.RETURN,
  1494. ConsoleModifiers.Shift,
  1495. '\u000D'
  1496. ),
  1497. new ScanCodeMapping (
  1498. 29,
  1499. VK.CONTROL,
  1500. 0,
  1501. '\0'
  1502. ), // Control
  1503. new ScanCodeMapping (
  1504. 29,
  1505. VK.CONTROL,
  1506. ConsoleModifiers.Shift,
  1507. '\0'
  1508. ),
  1509. new ScanCodeMapping (
  1510. 30,
  1511. (VK)'A',
  1512. 0,
  1513. 'a'
  1514. ), // A
  1515. new ScanCodeMapping (
  1516. 30,
  1517. (VK)'A',
  1518. ConsoleModifiers.Shift,
  1519. 'A'
  1520. ),
  1521. new ScanCodeMapping (
  1522. 31,
  1523. (VK)'S',
  1524. 0,
  1525. 's'
  1526. ), // S
  1527. new ScanCodeMapping (
  1528. 31,
  1529. (VK)'S',
  1530. ConsoleModifiers.Shift,
  1531. 'S'
  1532. ),
  1533. new ScanCodeMapping (
  1534. 32,
  1535. (VK)'D',
  1536. 0,
  1537. 'd'
  1538. ), // D
  1539. new ScanCodeMapping (
  1540. 32,
  1541. (VK)'D',
  1542. ConsoleModifiers.Shift,
  1543. 'D'
  1544. ),
  1545. new ScanCodeMapping (
  1546. 33,
  1547. (VK)'F',
  1548. 0,
  1549. 'f'
  1550. ), // F
  1551. new ScanCodeMapping (
  1552. 33,
  1553. (VK)'F',
  1554. ConsoleModifiers.Shift,
  1555. 'F'
  1556. ),
  1557. new ScanCodeMapping (
  1558. 34,
  1559. (VK)'G',
  1560. 0,
  1561. 'g'
  1562. ), // G
  1563. new ScanCodeMapping (
  1564. 34,
  1565. (VK)'G',
  1566. ConsoleModifiers.Shift,
  1567. 'G'
  1568. ),
  1569. new ScanCodeMapping (
  1570. 35,
  1571. (VK)'H',
  1572. 0,
  1573. 'h'
  1574. ), // H
  1575. new ScanCodeMapping (
  1576. 35,
  1577. (VK)'H',
  1578. ConsoleModifiers.Shift,
  1579. 'H'
  1580. ),
  1581. new ScanCodeMapping (
  1582. 36,
  1583. (VK)'J',
  1584. 0,
  1585. 'j'
  1586. ), // J
  1587. new ScanCodeMapping (
  1588. 36,
  1589. (VK)'J',
  1590. ConsoleModifiers.Shift,
  1591. 'J'
  1592. ),
  1593. new ScanCodeMapping (
  1594. 37,
  1595. (VK)'K',
  1596. 0,
  1597. 'k'
  1598. ), // K
  1599. new ScanCodeMapping (
  1600. 37,
  1601. (VK)'K',
  1602. ConsoleModifiers.Shift,
  1603. 'K'
  1604. ),
  1605. new ScanCodeMapping (
  1606. 38,
  1607. (VK)'L',
  1608. 0,
  1609. 'l'
  1610. ), // L
  1611. new ScanCodeMapping (
  1612. 38,
  1613. (VK)'L',
  1614. ConsoleModifiers.Shift,
  1615. 'L'
  1616. ),
  1617. new ScanCodeMapping (
  1618. 39,
  1619. VK.OEM_3,
  1620. 0,
  1621. '`'
  1622. ), // Oem3 (Backtick/Grave)
  1623. new ScanCodeMapping (
  1624. 39,
  1625. VK.OEM_3,
  1626. ConsoleModifiers.Shift,
  1627. '~'
  1628. ),
  1629. new ScanCodeMapping (
  1630. 40,
  1631. VK.OEM_7,
  1632. 0,
  1633. '\''
  1634. ), // Oem7 (Single Quote)
  1635. new ScanCodeMapping (
  1636. 40,
  1637. VK.OEM_7,
  1638. ConsoleModifiers.Shift,
  1639. '\"'
  1640. ),
  1641. new ScanCodeMapping (
  1642. 41,
  1643. VK.OEM_5,
  1644. 0,
  1645. '\\'
  1646. ), // Oem5 (Backslash)
  1647. new ScanCodeMapping (
  1648. 41,
  1649. VK.OEM_5,
  1650. ConsoleModifiers.Shift,
  1651. '|'
  1652. ),
  1653. new ScanCodeMapping (
  1654. 42,
  1655. VK.LSHIFT,
  1656. 0,
  1657. '\0'
  1658. ), // Left Shift
  1659. new ScanCodeMapping (
  1660. 42,
  1661. VK.LSHIFT,
  1662. ConsoleModifiers.Shift,
  1663. '\0'
  1664. ),
  1665. new ScanCodeMapping (
  1666. 43,
  1667. VK.OEM_2,
  1668. 0,
  1669. '/'
  1670. ), // Oem2 (Forward Slash)
  1671. new ScanCodeMapping (
  1672. 43,
  1673. VK.OEM_2,
  1674. ConsoleModifiers.Shift,
  1675. '?'
  1676. ),
  1677. new ScanCodeMapping (
  1678. 44,
  1679. (VK)'Z',
  1680. 0,
  1681. 'z'
  1682. ), // Z
  1683. new ScanCodeMapping (
  1684. 44,
  1685. (VK)'Z',
  1686. ConsoleModifiers.Shift,
  1687. 'Z'
  1688. ),
  1689. new ScanCodeMapping (
  1690. 45,
  1691. (VK)'X',
  1692. 0,
  1693. 'x'
  1694. ), // X
  1695. new ScanCodeMapping (
  1696. 45,
  1697. (VK)'X',
  1698. ConsoleModifiers.Shift,
  1699. 'X'
  1700. ),
  1701. new ScanCodeMapping (
  1702. 46,
  1703. (VK)'C',
  1704. 0,
  1705. 'c'
  1706. ), // C
  1707. new ScanCodeMapping (
  1708. 46,
  1709. (VK)'C',
  1710. ConsoleModifiers.Shift,
  1711. 'C'
  1712. ),
  1713. new ScanCodeMapping (
  1714. 47,
  1715. (VK)'V',
  1716. 0,
  1717. 'v'
  1718. ), // V
  1719. new ScanCodeMapping (
  1720. 47,
  1721. (VK)'V',
  1722. ConsoleModifiers.Shift,
  1723. 'V'
  1724. ),
  1725. new ScanCodeMapping (
  1726. 48,
  1727. (VK)'B',
  1728. 0,
  1729. 'b'
  1730. ), // B
  1731. new ScanCodeMapping (
  1732. 48,
  1733. (VK)'B',
  1734. ConsoleModifiers.Shift,
  1735. 'B'
  1736. ),
  1737. new ScanCodeMapping (
  1738. 49,
  1739. (VK)'N',
  1740. 0,
  1741. 'n'
  1742. ), // N
  1743. new ScanCodeMapping (
  1744. 49,
  1745. (VK)'N',
  1746. ConsoleModifiers.Shift,
  1747. 'N'
  1748. ),
  1749. new ScanCodeMapping (
  1750. 50,
  1751. (VK)'M',
  1752. 0,
  1753. 'm'
  1754. ), // M
  1755. new ScanCodeMapping (
  1756. 50,
  1757. (VK)'M',
  1758. ConsoleModifiers.Shift,
  1759. 'M'
  1760. ),
  1761. new ScanCodeMapping (
  1762. 51,
  1763. VK.OEM_COMMA,
  1764. 0,
  1765. ','
  1766. ), // OemComma
  1767. new ScanCodeMapping (
  1768. 51,
  1769. VK.OEM_COMMA,
  1770. ConsoleModifiers.Shift,
  1771. '<'
  1772. ),
  1773. new ScanCodeMapping (
  1774. 52,
  1775. VK.OEM_PERIOD,
  1776. 0,
  1777. '.'
  1778. ), // OemPeriod
  1779. new ScanCodeMapping (
  1780. 52,
  1781. VK.OEM_PERIOD,
  1782. ConsoleModifiers.Shift,
  1783. '>'
  1784. ),
  1785. new ScanCodeMapping (
  1786. 53,
  1787. VK.OEM_MINUS,
  1788. 0,
  1789. '-'
  1790. ), // OemMinus
  1791. new ScanCodeMapping (
  1792. 53,
  1793. VK.OEM_MINUS,
  1794. ConsoleModifiers.Shift,
  1795. '_'
  1796. ),
  1797. new ScanCodeMapping (
  1798. 54,
  1799. VK.RSHIFT,
  1800. 0,
  1801. '\0'
  1802. ), // Right Shift
  1803. new ScanCodeMapping (
  1804. 54,
  1805. VK.RSHIFT,
  1806. ConsoleModifiers.Shift,
  1807. '\0'
  1808. ),
  1809. new ScanCodeMapping (
  1810. 55,
  1811. VK.PRINT,
  1812. 0,
  1813. '\0'
  1814. ), // Print Screen
  1815. new ScanCodeMapping (
  1816. 55,
  1817. VK.PRINT,
  1818. ConsoleModifiers.Shift,
  1819. '\0'
  1820. ),
  1821. new ScanCodeMapping (
  1822. 56,
  1823. VK.LMENU,
  1824. 0,
  1825. '\0'
  1826. ), // Alt
  1827. new ScanCodeMapping (
  1828. 56,
  1829. VK.LMENU,
  1830. ConsoleModifiers.Shift,
  1831. '\0'
  1832. ),
  1833. new ScanCodeMapping (
  1834. 57,
  1835. VK.SPACE,
  1836. 0,
  1837. ' '
  1838. ), // Spacebar
  1839. new ScanCodeMapping (
  1840. 57,
  1841. VK.SPACE,
  1842. ConsoleModifiers.Shift,
  1843. ' '
  1844. ),
  1845. new ScanCodeMapping (
  1846. 58,
  1847. VK.CAPITAL,
  1848. 0,
  1849. '\0'
  1850. ), // Caps Lock
  1851. new ScanCodeMapping (
  1852. 58,
  1853. VK.CAPITAL,
  1854. ConsoleModifiers.Shift,
  1855. '\0'
  1856. ),
  1857. new ScanCodeMapping (
  1858. 59,
  1859. VK.F1,
  1860. 0,
  1861. '\0'
  1862. ), // F1
  1863. new ScanCodeMapping (
  1864. 59,
  1865. VK.F1,
  1866. ConsoleModifiers.Shift,
  1867. '\0'
  1868. ),
  1869. new ScanCodeMapping (
  1870. 60,
  1871. VK.F2,
  1872. 0,
  1873. '\0'
  1874. ), // F2
  1875. new ScanCodeMapping (
  1876. 60,
  1877. VK.F2,
  1878. ConsoleModifiers.Shift,
  1879. '\0'
  1880. ),
  1881. new ScanCodeMapping (
  1882. 61,
  1883. VK.F3,
  1884. 0,
  1885. '\0'
  1886. ), // F3
  1887. new ScanCodeMapping (
  1888. 61,
  1889. VK.F3,
  1890. ConsoleModifiers.Shift,
  1891. '\0'
  1892. ),
  1893. new ScanCodeMapping (
  1894. 62,
  1895. VK.F4,
  1896. 0,
  1897. '\0'
  1898. ), // F4
  1899. new ScanCodeMapping (
  1900. 62,
  1901. VK.F4,
  1902. ConsoleModifiers.Shift,
  1903. '\0'
  1904. ),
  1905. new ScanCodeMapping (
  1906. 63,
  1907. VK.F5,
  1908. 0,
  1909. '\0'
  1910. ), // F5
  1911. new ScanCodeMapping (
  1912. 63,
  1913. VK.F5,
  1914. ConsoleModifiers.Shift,
  1915. '\0'
  1916. ),
  1917. new ScanCodeMapping (
  1918. 64,
  1919. VK.F6,
  1920. 0,
  1921. '\0'
  1922. ), // F6
  1923. new ScanCodeMapping (
  1924. 64,
  1925. VK.F6,
  1926. ConsoleModifiers.Shift,
  1927. '\0'
  1928. ),
  1929. new ScanCodeMapping (
  1930. 65,
  1931. VK.F7,
  1932. 0,
  1933. '\0'
  1934. ), // F7
  1935. new ScanCodeMapping (
  1936. 65,
  1937. VK.F7,
  1938. ConsoleModifiers.Shift,
  1939. '\0'
  1940. ),
  1941. new ScanCodeMapping (
  1942. 66,
  1943. VK.F8,
  1944. 0,
  1945. '\0'
  1946. ), // F8
  1947. new ScanCodeMapping (
  1948. 66,
  1949. VK.F8,
  1950. ConsoleModifiers.Shift,
  1951. '\0'
  1952. ),
  1953. new ScanCodeMapping (
  1954. 67,
  1955. VK.F9,
  1956. 0,
  1957. '\0'
  1958. ), // F9
  1959. new ScanCodeMapping (
  1960. 67,
  1961. VK.F9,
  1962. ConsoleModifiers.Shift,
  1963. '\0'
  1964. ),
  1965. new ScanCodeMapping (
  1966. 68,
  1967. VK.F10,
  1968. 0,
  1969. '\0'
  1970. ), // F10
  1971. new ScanCodeMapping (
  1972. 68,
  1973. VK.F10,
  1974. ConsoleModifiers.Shift,
  1975. '\0'
  1976. ),
  1977. new ScanCodeMapping (
  1978. 69,
  1979. VK.NUMLOCK,
  1980. 0,
  1981. '\0'
  1982. ), // Num Lock
  1983. new ScanCodeMapping (
  1984. 69,
  1985. VK.NUMLOCK,
  1986. ConsoleModifiers.Shift,
  1987. '\0'
  1988. ),
  1989. new ScanCodeMapping (
  1990. 70,
  1991. VK.SCROLL,
  1992. 0,
  1993. '\0'
  1994. ), // Scroll Lock
  1995. new ScanCodeMapping (
  1996. 70,
  1997. VK.SCROLL,
  1998. ConsoleModifiers.Shift,
  1999. '\0'
  2000. ),
  2001. new ScanCodeMapping (
  2002. 71,
  2003. VK.HOME,
  2004. 0,
  2005. '\0'
  2006. ), // Home
  2007. new ScanCodeMapping (
  2008. 71,
  2009. VK.HOME,
  2010. ConsoleModifiers.Shift,
  2011. '\0'
  2012. ),
  2013. new ScanCodeMapping (
  2014. 72,
  2015. VK.UP,
  2016. 0,
  2017. '\0'
  2018. ), // Up Arrow
  2019. new ScanCodeMapping (
  2020. 72,
  2021. VK.UP,
  2022. ConsoleModifiers.Shift,
  2023. '\0'
  2024. ),
  2025. new ScanCodeMapping (
  2026. 73,
  2027. VK.PRIOR,
  2028. 0,
  2029. '\0'
  2030. ), // Page Up
  2031. new ScanCodeMapping (
  2032. 73,
  2033. VK.PRIOR,
  2034. ConsoleModifiers.Shift,
  2035. '\0'
  2036. ),
  2037. new ScanCodeMapping (
  2038. 74,
  2039. VK.SUBTRACT,
  2040. 0,
  2041. '-'
  2042. ), // Subtract (Num Pad '-')
  2043. new ScanCodeMapping (
  2044. 74,
  2045. VK.SUBTRACT,
  2046. ConsoleModifiers.Shift,
  2047. '-'
  2048. ),
  2049. new ScanCodeMapping (
  2050. 75,
  2051. VK.LEFT,
  2052. 0,
  2053. '\0'
  2054. ), // Left Arrow
  2055. new ScanCodeMapping (
  2056. 75,
  2057. VK.LEFT,
  2058. ConsoleModifiers.Shift,
  2059. '\0'
  2060. ),
  2061. new ScanCodeMapping (
  2062. 76,
  2063. VK.CLEAR,
  2064. 0,
  2065. '\0'
  2066. ), // Center key (Num Pad 5 with Num Lock off)
  2067. new ScanCodeMapping (
  2068. 76,
  2069. VK.CLEAR,
  2070. ConsoleModifiers.Shift,
  2071. '\0'
  2072. ),
  2073. new ScanCodeMapping (
  2074. 77,
  2075. VK.RIGHT,
  2076. 0,
  2077. '\0'
  2078. ), // Right Arrow
  2079. new ScanCodeMapping (
  2080. 77,
  2081. VK.RIGHT,
  2082. ConsoleModifiers.Shift,
  2083. '\0'
  2084. ),
  2085. new ScanCodeMapping (
  2086. 78,
  2087. VK.ADD,
  2088. 0,
  2089. '+'
  2090. ), // Add (Num Pad '+')
  2091. new ScanCodeMapping (
  2092. 78,
  2093. VK.ADD,
  2094. ConsoleModifiers.Shift,
  2095. '+'
  2096. ),
  2097. new ScanCodeMapping (
  2098. 79,
  2099. VK.END,
  2100. 0,
  2101. '\0'
  2102. ), // End
  2103. new ScanCodeMapping (
  2104. 79,
  2105. VK.END,
  2106. ConsoleModifiers.Shift,
  2107. '\0'
  2108. ),
  2109. new ScanCodeMapping (
  2110. 80,
  2111. VK.DOWN,
  2112. 0,
  2113. '\0'
  2114. ), // Down Arrow
  2115. new ScanCodeMapping (
  2116. 80,
  2117. VK.DOWN,
  2118. ConsoleModifiers.Shift,
  2119. '\0'
  2120. ),
  2121. new ScanCodeMapping (
  2122. 81,
  2123. VK.NEXT,
  2124. 0,
  2125. '\0'
  2126. ), // Page Down
  2127. new ScanCodeMapping (
  2128. 81,
  2129. VK.NEXT,
  2130. ConsoleModifiers.Shift,
  2131. '\0'
  2132. ),
  2133. new ScanCodeMapping (
  2134. 82,
  2135. VK.INSERT,
  2136. 0,
  2137. '\0'
  2138. ), // Insert
  2139. new ScanCodeMapping (
  2140. 82,
  2141. VK.INSERT,
  2142. ConsoleModifiers.Shift,
  2143. '\0'
  2144. ),
  2145. new ScanCodeMapping (
  2146. 83,
  2147. VK.DELETE,
  2148. 0,
  2149. '\0'
  2150. ), // Delete
  2151. new ScanCodeMapping (
  2152. 83,
  2153. VK.DELETE,
  2154. ConsoleModifiers.Shift,
  2155. '\0'
  2156. ),
  2157. new ScanCodeMapping (
  2158. 86,
  2159. VK.OEM_102,
  2160. 0,
  2161. '<'
  2162. ), // OEM 102 (Typically '<' or '|' key next to Left Shift)
  2163. new ScanCodeMapping (
  2164. 86,
  2165. VK.OEM_102,
  2166. ConsoleModifiers.Shift,
  2167. '>'
  2168. ),
  2169. new ScanCodeMapping (
  2170. 87,
  2171. VK.F11,
  2172. 0,
  2173. '\0'
  2174. ), // F11
  2175. new ScanCodeMapping (
  2176. 87,
  2177. VK.F11,
  2178. ConsoleModifiers.Shift,
  2179. '\0'
  2180. ),
  2181. new ScanCodeMapping (
  2182. 88,
  2183. VK.F12,
  2184. 0,
  2185. '\0'
  2186. ), // F12
  2187. new ScanCodeMapping (
  2188. 88,
  2189. VK.F12,
  2190. ConsoleModifiers.Shift,
  2191. '\0'
  2192. )
  2193. };
  2194. /// <summary>Decode a <see cref="ConsoleKeyInfo"/> that is using <see cref="ConsoleKey.Packet"/>.</summary>
  2195. /// <param name="consoleKeyInfo">The console key info.</param>
  2196. /// <returns>The decoded <see cref="ConsoleKeyInfo"/> or the <paramref name="consoleKeyInfo"/>.</returns>
  2197. /// <remarks>
  2198. /// If it's a <see cref="ConsoleKey.Packet"/> the <see cref="ConsoleKeyInfo.KeyChar"/> may be a
  2199. /// <see cref="ConsoleKeyInfo.Key"/> or a <see cref="ConsoleKeyInfo.KeyChar"/> value.
  2200. /// </remarks>
  2201. public static ConsoleKeyInfo DecodeVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  2202. {
  2203. if (consoleKeyInfo.Key != ConsoleKey.Packet)
  2204. {
  2205. return consoleKeyInfo;
  2206. }
  2207. return GetConsoleKeyInfoFromKeyChar (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out _);
  2208. }
  2209. /// <summary>
  2210. /// Encode the <see cref="ConsoleKeyInfo.KeyChar"/> with the <see cref="ConsoleKeyInfo.Key"/> if the first a byte
  2211. /// length, otherwise only the KeyChar is considered and searched on the database.
  2212. /// </summary>
  2213. /// <param name="consoleKeyInfo">The console key info.</param>
  2214. /// <returns>The encoded KeyChar with the Key if both can be shifted, otherwise only the KeyChar.</returns>
  2215. /// <remarks>This is useful to use with the <see cref="ConsoleKey.Packet"/>.</remarks>
  2216. public static char EncodeKeyCharForVKPacket (ConsoleKeyInfo consoleKeyInfo)
  2217. {
  2218. char keyChar = consoleKeyInfo.KeyChar;
  2219. ConsoleKey consoleKey = consoleKeyInfo.Key;
  2220. if (keyChar != 0 && consoleKeyInfo.KeyChar < byte.MaxValue && consoleKey == ConsoleKey.None)
  2221. {
  2222. // try to get the ConsoleKey
  2223. ScanCodeMapping scode = _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyChar);
  2224. if (scode is { })
  2225. {
  2226. consoleKey = (ConsoleKey)scode.VirtualKey;
  2227. }
  2228. }
  2229. if (keyChar < byte.MaxValue && consoleKey != ConsoleKey.None)
  2230. {
  2231. keyChar = (char)((consoleKeyInfo.KeyChar << 8) | (byte)consoleKey);
  2232. }
  2233. return keyChar;
  2234. }
  2235. }