ConsoleKeyMapping.cs 84 KB

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