ConsoleKeyMapping.cs 83 KB

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