ConsoleKeyMapping.cs 84 KB

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