12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582 |
- using System.Globalization;
- using System.Runtime.InteropServices;
- namespace Terminal.Gui.ConsoleDrivers;
- /// <summary>Helper class to handle the scan code and virtual key from a <see cref="ConsoleKey"/>.</summary>
- public static class ConsoleKeyMapping
- {
- #if !WT_ISSUE_8871_FIXED // https://github.com/microsoft/terminal/issues/8871
- /// <summary>
- /// Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a
- /// virtual-key code.
- /// </summary>
- /// <param name="vk"></param>
- /// <param name="uMapType">
- /// If MAPVK_VK_TO_CHAR (2) - The uCode parameter is a virtual-key code and is translated into an
- /// un-shifted character value in the low order word of the return value.
- /// </param>
- /// <param name="dwhkl"></param>
- /// <returns>
- /// An un-shifted character value in the low order word of the return value. Dead keys (diacritics) are indicated
- /// by setting the top bit of the return value. If there is no translation, the function returns 0. See Remarks.
- /// </returns>
- [DllImport ("user32.dll", EntryPoint = "MapVirtualKeyExW", CharSet = CharSet.Unicode)]
- private static extern uint MapVirtualKeyEx (VK vk, uint uMapType, nint dwhkl);
- /// <summary>Retrieves the active input locale identifier (formerly called the keyboard layout).</summary>
- /// <param name="idThread">0 for current thread</param>
- /// <returns>
- /// The return value is the input locale identifier for the thread. The low word contains a Language Identifier
- /// for the input language and the high word contains a device handle to the physical layout of the keyboard.
- /// </returns>
- [DllImport ("user32.dll", EntryPoint = "GetKeyboardLayout", CharSet = CharSet.Unicode)]
- private static extern nint GetKeyboardLayout (nint idThread);
- //[DllImport ("user32.dll", EntryPoint = "GetKeyboardLayoutNameW", CharSet = CharSet.Unicode)]
- //extern static uint GetKeyboardLayoutName (uint idThread);
- [DllImport ("user32.dll")]
- private static extern nint GetForegroundWindow ();
- [DllImport ("user32.dll")]
- private static extern nint GetWindowThreadProcessId (nint hWnd, nint ProcessId);
- /// <summary>
- /// Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or
- /// characters using the Win32 API MapVirtualKey.
- /// </summary>
- /// <param name="vk"></param>
- /// <returns>
- /// An un-shifted character value in the low order word of the return value. Dead keys (diacritics) are indicated
- /// by setting the top bit of the return value. If there is no translation, the function returns 0.
- /// </returns>
- public static uint MapVKtoChar (VK vk)
- {
- if (Environment.OSVersion.Platform != PlatformID.Win32NT)
- {
- return 0;
- }
- nint tid = GetWindowThreadProcessId (GetForegroundWindow (), 0);
- nint hkl = GetKeyboardLayout (tid);
- return MapVirtualKeyEx (vk, 2, hkl);
- }
- #else
- /// <summary>
- /// Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
- /// </summary>
- /// <param name="vk"></param>
- /// <param name="uMapType">
- /// If MAPVK_VK_TO_CHAR (2) - The uCode parameter is a virtual-key code and is translated into an unshifted
- /// character value in the low order word of the return value.
- /// </param>
- /// <returns>An unshifted character value in the low order word of the return value. Dead keys (diacritics)
- /// are indicated by setting the top bit of the return value. If there is no translation,
- /// the function returns 0. See Remarks.</returns>
- [DllImport ("user32.dll", EntryPoint = "MapVirtualKeyW", CharSet = CharSet.Unicode)]
- extern static uint MapVirtualKey (VK vk, uint uMapType = 2);
- uint MapVKtoChar (VK vk) => MapVirtualKeyToCharEx (vk);
- #endif
- /// <summary>
- /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the calling
- /// thread.
- /// </summary>
- /// <param name="pwszKLID"></param>
- /// <returns></returns>
- [DllImport ("user32.dll")]
- private static extern bool GetKeyboardLayoutName ([Out] StringBuilder pwszKLID);
- /// <summary>
- /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the calling
- /// thread.
- /// </summary>
- /// <returns></returns>
- public static string GetKeyboardLayoutName ()
- {
- if (Environment.OSVersion.Platform != PlatformID.Win32NT)
- {
- return "none";
- }
- var klidSB = new StringBuilder ();
- GetKeyboardLayoutName (klidSB);
- return klidSB.ToString ();
- }
- private class ScanCodeMapping : IEquatable<ScanCodeMapping>
- {
- public readonly ConsoleModifiers Modifiers;
- public readonly uint ScanCode;
- public readonly uint UnicodeChar;
- public readonly VK VirtualKey;
- public ScanCodeMapping (uint scanCode, VK virtualKey, ConsoleModifiers modifiers, uint unicodeChar)
- {
- ScanCode = scanCode;
- VirtualKey = virtualKey;
- Modifiers = modifiers;
- UnicodeChar = unicodeChar;
- }
- public bool Equals (ScanCodeMapping other)
- {
- return ScanCode.Equals (other.ScanCode)
- && VirtualKey.Equals (other.VirtualKey)
- && Modifiers.Equals (other.Modifiers)
- && UnicodeChar.Equals (other.UnicodeChar);
- }
- }
- private static ConsoleModifiers GetModifiers (ConsoleModifiers modifiers)
- {
- if (modifiers.HasFlag (ConsoleModifiers.Shift)
- && !modifiers.HasFlag (ConsoleModifiers.Alt)
- && !modifiers.HasFlag (ConsoleModifiers.Control))
- {
- return ConsoleModifiers.Shift;
- }
- if (modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control))
- {
- return modifiers;
- }
- return 0;
- }
- private static ScanCodeMapping GetScanCode (string propName, uint keyValue, ConsoleModifiers modifiers)
- {
- switch (propName)
- {
- case "UnicodeChar":
- ScanCodeMapping sCode =
- _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyValue && e.Modifiers == modifiers);
- if (sCode is null && modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control))
- {
- return _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyValue && e.Modifiers == 0);
- }
- return sCode;
- case "VirtualKey":
- sCode = _scanCodes.FirstOrDefault (e => e.VirtualKey == (VK)keyValue && e.Modifiers == modifiers);
- if (sCode is null && modifiers == (ConsoleModifiers.Alt | ConsoleModifiers.Control))
- {
- return _scanCodes.FirstOrDefault (e => e.VirtualKey == (VK)keyValue && e.Modifiers == 0);
- }
- return sCode;
- }
- return null;
- }
- // BUGBUG: This API is not correct. It is only used by WindowsDriver in VKPacket scenarios
- /// <summary>Get the scan code from a <see cref="ConsoleKeyInfo"/>.</summary>
- /// <param name="consoleKeyInfo">The console key info.</param>
- /// <returns>The value if apply.</returns>
- public static uint GetScanCodeFromConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
- {
- ConsoleModifiers mod = GetModifiers (consoleKeyInfo.Modifiers);
- ScanCodeMapping scode = GetScanCode ("VirtualKey", (uint)consoleKeyInfo.Key, mod);
- if (scode is { })
- {
- return scode.ScanCode;
- }
- return 0;
- }
- // BUGBUG: This API is not correct. It is only used by FakeDriver and VkeyPacketSimulator
- /// <summary>Gets the <see cref="ConsoleKeyInfo"/> from the provided <see cref="KeyCode"/>.</summary>
- /// <param name="key">The key code.</param>
- /// <returns>The console key info.</returns>
- public static ConsoleKeyInfo GetConsoleKeyInfoFromKeyCode (KeyCode key)
- {
- ConsoleModifiers modifiers = MapToConsoleModifiers (key);
- uint keyValue = MapKeyCodeToConsoleKey (key, out bool isConsoleKey);
- if (isConsoleKey)
- {
- ConsoleModifiers mod = GetModifiers (modifiers);
- ScanCodeMapping scode = GetScanCode ("VirtualKey", keyValue, mod);
- if (scode is { })
- {
- return new ConsoleKeyInfo (
- (char)scode.UnicodeChar,
- (ConsoleKey)scode.VirtualKey,
- modifiers.HasFlag (ConsoleModifiers.Shift),
- modifiers.HasFlag (ConsoleModifiers.Alt),
- modifiers.HasFlag (ConsoleModifiers.Control)
- );
- }
- }
- else
- {
- uint keyChar = GetKeyCharFromUnicodeChar (keyValue, modifiers, out uint consoleKey, out _, isConsoleKey);
- if (consoleKey != 0)
- {
- return new ConsoleKeyInfo (
- (char)keyChar,
- (ConsoleKey)consoleKey,
- modifiers.HasFlag (ConsoleModifiers.Shift),
- modifiers.HasFlag (ConsoleModifiers.Alt),
- modifiers.HasFlag (ConsoleModifiers.Control)
- );
- }
- }
- return new ConsoleKeyInfo (
- (char)keyValue,
- ConsoleKey.None,
- modifiers.HasFlag (ConsoleModifiers.Shift),
- modifiers.HasFlag (ConsoleModifiers.Alt),
- modifiers.HasFlag (ConsoleModifiers.Control)
- );
- }
- /// <summary>Map existing <see cref="KeyCode"/> modifiers to <see cref="ConsoleModifiers"/>.</summary>
- /// <param name="key">The key code.</param>
- /// <returns>The console modifiers.</returns>
- public static ConsoleModifiers MapToConsoleModifiers (KeyCode key)
- {
- var modifiers = new ConsoleModifiers ();
- if (key.HasFlag (KeyCode.ShiftMask) || char.IsUpper ((char)key))
- {
- modifiers |= ConsoleModifiers.Shift;
- }
- if (key.HasFlag (KeyCode.AltMask))
- {
- modifiers |= ConsoleModifiers.Alt;
- }
- if (key.HasFlag (KeyCode.CtrlMask))
- {
- modifiers |= ConsoleModifiers.Control;
- }
- return modifiers;
- }
- /// <summary>Gets <see cref="ConsoleModifiers"/> from <see cref="bool"/> modifiers.</summary>
- /// <param name="shift">The shift key.</param>
- /// <param name="alt">The alt key.</param>
- /// <param name="control">The control key.</param>
- /// <returns>The console modifiers.</returns>
- public static ConsoleModifiers GetModifiers (bool shift, bool alt, bool control)
- {
- var modifiers = new ConsoleModifiers ();
- if (shift)
- {
- modifiers |= ConsoleModifiers.Shift;
- }
- if (alt)
- {
- modifiers |= ConsoleModifiers.Alt;
- }
- if (control)
- {
- modifiers |= ConsoleModifiers.Control;
- }
- return modifiers;
- }
- /// <summary>
- /// Get the <see cref="ConsoleKeyInfo"/> from a unicode character and modifiers (e.g. (Key)'a' and
- /// (Key)Key.CtrlMask).
- /// </summary>
- /// <param name="keyValue">The key as a unicode codepoint.</param>
- /// <param name="modifiers">The modifier keys.</param>
- /// <param name="scanCode">The resulting scan code.</param>
- /// <returns>The <see cref="ConsoleKeyInfo"/>.</returns>
- private static ConsoleKeyInfo GetConsoleKeyInfoFromKeyChar (
- uint keyValue,
- ConsoleModifiers modifiers,
- out uint scanCode
- )
- {
- scanCode = 0;
- if (keyValue == 0)
- {
- return new ConsoleKeyInfo (
- (char)keyValue,
- ConsoleKey.None,
- modifiers.HasFlag (ConsoleModifiers.Shift),
- modifiers.HasFlag (ConsoleModifiers.Alt),
- modifiers.HasFlag (ConsoleModifiers.Control)
- );
- }
- uint outputChar = keyValue;
- uint consoleKey;
- if (keyValue > byte.MaxValue)
- {
- ScanCodeMapping sCode = _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyValue);
- if (sCode is null)
- {
- consoleKey = (byte)(keyValue & byte.MaxValue);
- sCode = _scanCodes.FirstOrDefault (e => e.VirtualKey == (VK)consoleKey);
- if (sCode is null)
- {
- consoleKey = 0;
- outputChar = keyValue;
- }
- else
- {
- outputChar = (char)(keyValue >> 8);
- }
- }
- else
- {
- consoleKey = (byte)sCode.VirtualKey;
- outputChar = keyValue;
- }
- }
- else
- {
- consoleKey = (byte)keyValue;
- outputChar = '\0';
- }
- return new ConsoleKeyInfo (
- (char)outputChar,
- (ConsoleKey)consoleKey,
- modifiers.HasFlag (ConsoleModifiers.Shift),
- modifiers.HasFlag (ConsoleModifiers.Alt),
- modifiers.HasFlag (ConsoleModifiers.Control)
- );
- }
- // Used only by unit tests
- internal static uint GetKeyChar (uint keyValue, ConsoleModifiers modifiers)
- {
- if (modifiers == ConsoleModifiers.Shift && keyValue - 32 is >= 'A' and <= 'Z')
- {
- return keyValue - 32;
- }
- if (modifiers == ConsoleModifiers.None && keyValue is >= 'A' and <= 'Z')
- {
- return keyValue + 32;
- }
- if (modifiers == ConsoleModifiers.Shift && keyValue - 32 is >= 'À' and <= 'Ý')
- {
- return keyValue - 32;
- }
- if (modifiers == ConsoleModifiers.None && keyValue is >= 'À' and <= 'Ý')
- {
- return keyValue + 32;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '0')
- {
- return keyValue + 13;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 13 is '0')
- {
- return keyValue - 13;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is >= '1' and <= '9' and not '7')
- {
- return keyValue - 16;
- }
- if (modifiers == ConsoleModifiers.None && keyValue + 16 is >= '1' and <= '9' and not '7')
- {
- return keyValue + 16;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '7')
- {
- return keyValue - 8;
- }
- if (modifiers == ConsoleModifiers.None && keyValue + 8 is '7')
- {
- return keyValue + 8;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '\'')
- {
- return keyValue + 24;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 24 is '\'')
- {
- return keyValue - 24;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '«')
- {
- return keyValue + 16;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 16 is '«')
- {
- return keyValue - 16;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '\\')
- {
- return keyValue + 32;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 32 is '\\')
- {
- return keyValue - 32;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '+')
- {
- return keyValue - 1;
- }
- if (modifiers == ConsoleModifiers.None && keyValue + 1 is '+')
- {
- return keyValue + 1;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '´')
- {
- return keyValue - 84;
- }
- if (modifiers == ConsoleModifiers.None && keyValue + 84 is '´')
- {
- return keyValue + 84;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is 'º')
- {
- return keyValue - 16;
- }
- if (modifiers == ConsoleModifiers.None && keyValue + 16 is 'º')
- {
- return keyValue + 16;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '~')
- {
- return keyValue - 32;
- }
- if (modifiers == ConsoleModifiers.None && keyValue + 32 is '~')
- {
- return keyValue + 32;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '<')
- {
- return keyValue + 2;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 2 is '<')
- {
- return keyValue - 2;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is ',')
- {
- return keyValue + 15;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 15 is ',')
- {
- return keyValue - 15;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '.')
- {
- return keyValue + 12;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 12 is '.')
- {
- return keyValue - 12;
- }
- if (modifiers.HasFlag (ConsoleModifiers.Shift) && keyValue is '-')
- {
- return keyValue + 50;
- }
- if (modifiers == ConsoleModifiers.None && keyValue - 50 is '-')
- {
- return keyValue - 50;
- }
- return keyValue;
- }
- /// <summary>
- /// Get the output character from the <see cref="GetConsoleKeyInfoFromKeyCode"/>, with the correct
- /// <see cref="ConsoleKey"/> and the scan code used on <see cref="WindowsDriver"/>.
- /// </summary>
- /// <param name="unicodeChar">The unicode character.</param>
- /// <param name="modifiers">The modifiers keys.</param>
- /// <param name="consoleKey">The resulting console key.</param>
- /// <param name="scanCode">The resulting scan code.</param>
- /// <param name="isConsoleKey">Indicates if the <paramref name="unicodeChar"/> is a <see cref="ConsoleKey"/>.</param>
- /// <returns>The output character or the <paramref name="consoleKey"/>.</returns>
- /// <remarks>This is only used by the <see cref="GetConsoleKeyInfoFromKeyCode"/> and by unit tests.</remarks>
- internal static uint GetKeyCharFromUnicodeChar (
- uint unicodeChar,
- ConsoleModifiers modifiers,
- out uint consoleKey,
- out uint scanCode,
- bool isConsoleKey = false
- )
- {
- uint decodedChar = unicodeChar >> 8 == 0xff ? unicodeChar & 0xff : unicodeChar;
- uint keyChar = decodedChar;
- consoleKey = 0;
- ConsoleModifiers mod = GetModifiers (modifiers);
- scanCode = 0;
- ScanCodeMapping scode = null;
- if (unicodeChar != 0 && unicodeChar >> 8 != 0xff && isConsoleKey)
- {
- scode = GetScanCode ("VirtualKey", decodedChar, mod);
- }
- if (isConsoleKey && scode is { })
- {
- consoleKey = (uint)scode.VirtualKey;
- keyChar = scode.UnicodeChar;
- scanCode = scode.ScanCode;
- }
- if (scode is null)
- {
- scode = unicodeChar != 0 ? GetScanCode ("UnicodeChar", decodedChar, mod) : null;
- if (scode is { })
- {
- consoleKey = (uint)scode.VirtualKey;
- keyChar = scode.UnicodeChar;
- scanCode = scode.ScanCode;
- }
- }
- if (decodedChar != 0 && scanCode == 0 && char.IsLetter ((char)decodedChar))
- {
- string stFormD = ((char)decodedChar).ToString ().Normalize (NormalizationForm.FormD);
- for (var i = 0; i < stFormD.Length; i++)
- {
- UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory (stFormD [i]);
- if (uc != UnicodeCategory.NonSpacingMark && uc != UnicodeCategory.OtherLetter)
- {
- char ck = char.ToUpper (stFormD [i]);
- consoleKey = (uint)(ck > 0 && ck <= 255 ? char.ToUpper (stFormD [i]) : 0);
- scode = GetScanCode ("VirtualKey", char.ToUpper (stFormD [i]), 0);
- if (scode is { })
- {
- scanCode = scode.ScanCode;
- }
- }
- }
- }
- if (keyChar < 255 && consoleKey == 0 && scanCode == 0)
- {
- scode = GetScanCode ("VirtualKey", keyChar, mod);
- if (scode is { })
- {
- consoleKey = (uint)scode.VirtualKey;
- keyChar = scode.UnicodeChar;
- scanCode = scode.ScanCode;
- }
- }
- return keyChar;
- }
- /// <summary>Maps a unicode character (e.g. (Key)'a') to a uint representing a <see cref="ConsoleKey"/>.</summary>
- /// <param name="keyValue">The key value.</param>
- /// <param name="isConsoleKey">
- /// Indicates if the <paramref name="keyValue"/> is a <see cref="ConsoleKey"/>.
- /// <see langword="true"/> means the return value is in the ConsoleKey enum. <see langword="false"/> means the return
- /// value can be mapped to a valid unicode character.
- /// </param>
- /// <returns>The <see cref="ConsoleKey"/> or the <paramref name="keyValue"/>.</returns>
- /// <remarks>This is only used by the <see cref="GetConsoleKeyInfoFromKeyCode"/> and by unit tests.</remarks>
- internal static uint MapKeyCodeToConsoleKey (KeyCode keyValue, out bool isConsoleKey)
- {
- isConsoleKey = true;
- keyValue = keyValue & ~KeyCode.CtrlMask & ~KeyCode.ShiftMask & ~KeyCode.AltMask;
- switch (keyValue)
- {
- case KeyCode.Enter:
- return (uint)ConsoleKey.Enter;
- case KeyCode.CursorUp:
- return (uint)ConsoleKey.UpArrow;
- case KeyCode.CursorDown:
- return (uint)ConsoleKey.DownArrow;
- case KeyCode.CursorLeft:
- return (uint)ConsoleKey.LeftArrow;
- case KeyCode.CursorRight:
- return (uint)ConsoleKey.RightArrow;
- case KeyCode.PageUp:
- return (uint)ConsoleKey.PageUp;
- case KeyCode.PageDown:
- return (uint)ConsoleKey.PageDown;
- case KeyCode.Home:
- return (uint)ConsoleKey.Home;
- case KeyCode.End:
- return (uint)ConsoleKey.End;
- case KeyCode.Insert:
- return (uint)ConsoleKey.Insert;
- case KeyCode.Delete:
- return (uint)ConsoleKey.Delete;
- case KeyCode.F1:
- return (uint)ConsoleKey.F1;
- case KeyCode.F2:
- return (uint)ConsoleKey.F2;
- case KeyCode.F3:
- return (uint)ConsoleKey.F3;
- case KeyCode.F4:
- return (uint)ConsoleKey.F4;
- case KeyCode.F5:
- return (uint)ConsoleKey.F5;
- case KeyCode.F6:
- return (uint)ConsoleKey.F6;
- case KeyCode.F7:
- return (uint)ConsoleKey.F7;
- case KeyCode.F8:
- return (uint)ConsoleKey.F8;
- case KeyCode.F9:
- return (uint)ConsoleKey.F9;
- case KeyCode.F10:
- return (uint)ConsoleKey.F10;
- case KeyCode.F11:
- return (uint)ConsoleKey.F11;
- case KeyCode.F12:
- return (uint)ConsoleKey.F12;
- case KeyCode.F13:
- return (uint)ConsoleKey.F13;
- case KeyCode.F14:
- return (uint)ConsoleKey.F14;
- case KeyCode.F15:
- return (uint)ConsoleKey.F15;
- case KeyCode.F16:
- return (uint)ConsoleKey.F16;
- case KeyCode.F17:
- return (uint)ConsoleKey.F17;
- case KeyCode.F18:
- return (uint)ConsoleKey.F18;
- case KeyCode.F19:
- return (uint)ConsoleKey.F19;
- case KeyCode.F20:
- return (uint)ConsoleKey.F20;
- case KeyCode.F21:
- return (uint)ConsoleKey.F21;
- case KeyCode.F22:
- return (uint)ConsoleKey.F22;
- case KeyCode.F23:
- return (uint)ConsoleKey.F23;
- case KeyCode.F24:
- return (uint)ConsoleKey.F24;
- case KeyCode.Tab | KeyCode.ShiftMask:
- return (uint)ConsoleKey.Tab;
- case KeyCode.Space:
- return (uint)ConsoleKey.Spacebar;
- default:
- uint c = (char)keyValue;
- if (c is >= (char)ConsoleKey.A and <= (char)ConsoleKey.Z)
- {
- return c;
- }
- if ((c - 32) is >= (char)ConsoleKey.A and <= (char)ConsoleKey.Z)
- {
- return (c - 32);
- }
- if (Enum.IsDefined (typeof (ConsoleKey), keyValue.ToString ()))
- {
- return (uint)keyValue;
- }
- // DEL
- if ((uint)keyValue == 127)
- {
- return (uint)ConsoleKey.Backspace;
- }
- break;
- }
- isConsoleKey = false;
- return (uint)keyValue;
- }
- /// <summary>Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.</summary>
- /// <param name="consoleKeyInfo">The console key.</param>
- /// <returns>The <see cref="KeyCode"/> or the <paramref name="consoleKeyInfo"/>.</returns>
- public static KeyCode MapConsoleKeyInfoToKeyCode (ConsoleKeyInfo consoleKeyInfo)
- {
- KeyCode keyCode;
- switch (consoleKeyInfo.Key)
- {
- case ConsoleKey.Enter:
- keyCode = KeyCode.Enter;
- break;
- case ConsoleKey.Delete:
- keyCode = KeyCode.Delete;
- break;
- case ConsoleKey.UpArrow:
- keyCode = KeyCode.CursorUp;
- break;
- case ConsoleKey.DownArrow:
- keyCode = KeyCode.CursorDown;
- break;
- case ConsoleKey.LeftArrow:
- keyCode = KeyCode.CursorLeft;
- break;
- case ConsoleKey.RightArrow:
- keyCode = KeyCode.CursorRight;
- break;
- case ConsoleKey.PageUp:
- keyCode = KeyCode.PageUp;
- break;
- case ConsoleKey.PageDown:
- keyCode = KeyCode.PageDown;
- break;
- case ConsoleKey.Home:
- keyCode = KeyCode.Home;
- break;
- case ConsoleKey.End:
- keyCode = KeyCode.End;
- break;
- case ConsoleKey.Insert:
- keyCode = KeyCode.Insert;
- break;
- case ConsoleKey.F1:
- keyCode = KeyCode.F1;
- break;
- case ConsoleKey.F2:
- keyCode = KeyCode.F2;
- break;
- case ConsoleKey.F3:
- keyCode = KeyCode.F3;
- break;
- case ConsoleKey.F4:
- keyCode = KeyCode.F4;
- break;
- case ConsoleKey.F5:
- keyCode = KeyCode.F5;
- break;
- case ConsoleKey.F6:
- keyCode = KeyCode.F6;
- break;
- case ConsoleKey.F7:
- keyCode = KeyCode.F7;
- break;
- case ConsoleKey.F8:
- keyCode = KeyCode.F8;
- break;
- case ConsoleKey.F9:
- keyCode = KeyCode.F9;
- break;
- case ConsoleKey.F10:
- keyCode = KeyCode.F10;
- break;
- case ConsoleKey.F11:
- keyCode = KeyCode.F11;
- break;
- case ConsoleKey.F12:
- keyCode = KeyCode.F12;
- break;
- case ConsoleKey.F13:
- keyCode = KeyCode.F13;
- break;
- case ConsoleKey.F14:
- keyCode = KeyCode.F14;
- break;
- case ConsoleKey.F15:
- keyCode = KeyCode.F15;
- break;
- case ConsoleKey.F16:
- keyCode = KeyCode.F16;
- break;
- case ConsoleKey.F17:
- keyCode = KeyCode.F17;
- break;
- case ConsoleKey.F18:
- keyCode = KeyCode.F18;
- break;
- case ConsoleKey.F19:
- keyCode = KeyCode.F19;
- break;
- case ConsoleKey.F20:
- keyCode = KeyCode.F20;
- break;
- case ConsoleKey.F21:
- keyCode = KeyCode.F21;
- break;
- case ConsoleKey.F22:
- keyCode = KeyCode.F22;
- break;
- case ConsoleKey.F23:
- keyCode = KeyCode.F23;
- break;
- case ConsoleKey.F24:
- keyCode = KeyCode.F24;
- break;
- case ConsoleKey.Clear:
- keyCode = KeyCode.Clear;
- break;
- case ConsoleKey.Tab:
- keyCode = KeyCode.Tab;
- break;
- case ConsoleKey.Spacebar:
- keyCode = KeyCode.Space;
- break;
- case ConsoleKey.Backspace:
- keyCode = KeyCode.Backspace;
- break;
- default:
- if ((int)consoleKeyInfo.KeyChar is >= 1 and <= 26)
- {
- keyCode = (KeyCode)(consoleKeyInfo.KeyChar + 64);
- }
- else
- {
- keyCode = (KeyCode)consoleKeyInfo.KeyChar;
- }
- break;
- }
- keyCode |= MapToKeyCodeModifiers (consoleKeyInfo.Modifiers, keyCode);
- return keyCode;
- }
- /// <summary>Maps a <see cref="ConsoleKeyInfo"/> to a <see cref="KeyCode"/>.</summary>
- /// <param name="modifiers">The console modifiers.</param>
- /// <param name="key">The key code.</param>
- /// <returns>The <see cref="KeyCode"/> with <see cref="ConsoleModifiers"/> or the <paramref name="key"/></returns>
- public static KeyCode MapToKeyCodeModifiers (ConsoleModifiers modifiers, KeyCode key)
- {
- var keyMod = new KeyCode ();
- if ((modifiers & ConsoleModifiers.Shift) != 0)
- {
- keyMod = KeyCode.ShiftMask;
- }
- if ((modifiers & ConsoleModifiers.Control) != 0)
- {
- keyMod |= KeyCode.CtrlMask;
- }
- if ((modifiers & ConsoleModifiers.Alt) != 0)
- {
- keyMod |= KeyCode.AltMask;
- }
- return keyMod != KeyCode.Null ? keyMod | key : key;
- }
- /// <summary>Generated from winuser.h. See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes</summary>
- public enum VK : ushort
- {
- /// <summary>Left mouse button.</summary>
- LBUTTON = 0x01,
- /// <summary>Right mouse button.</summary>
- RBUTTON = 0x02,
- /// <summary>Control-break processing.</summary>
- CANCEL = 0x03,
- /// <summary>Middle mouse button (three-button mouse).</summary>
- MBUTTON = 0x04,
- /// <summary>X1 mouse button.</summary>
- XBUTTON1 = 0x05,
- /// <summary>X2 mouse button.</summary>
- XBUTTON2 = 0x06,
- /// <summary>BACKSPACE key.</summary>
- BACK = 0x08,
- /// <summary>TAB key.</summary>
- TAB = 0x09,
- /// <summary>CLEAR key.</summary>
- CLEAR = 0x0C,
- /// <summary>ENTER key.</summary>
- RETURN = 0x0D,
- /// <summary>SHIFT key.</summary>
- SHIFT = 0x10,
- /// <summary>CTRL key.</summary>
- CONTROL = 0x11,
- /// <summary>ALT key.</summary>
- MENU = 0x12,
- /// <summary>PAUSE key.</summary>
- PAUSE = 0x13,
- /// <summary>CAPS LOCK key.</summary>
- CAPITAL = 0x14,
- /// <summary>IME Kana mode.</summary>
- KANA = 0x15,
- /// <summary>IME Hangul mode.</summary>
- HANGUL = 0x15,
- /// <summary>IME Junja mode.</summary>
- JUNJA = 0x17,
- /// <summary>IME final mode.</summary>
- FINAL = 0x18,
- /// <summary>IME Hanja mode.</summary>
- HANJA = 0x19,
- /// <summary>IME Kanji mode.</summary>
- KANJI = 0x19,
- /// <summary>ESC key.</summary>
- ESCAPE = 0x1B,
- /// <summary>IME convert.</summary>
- CONVERT = 0x1C,
- /// <summary>IME nonconvert.</summary>
- NONCONVERT = 0x1D,
- /// <summary>IME accept.</summary>
- ACCEPT = 0x1E,
- /// <summary>IME mode change request.</summary>
- MODECHANGE = 0x1F,
- /// <summary>SPACEBAR.</summary>
- SPACE = 0x20,
- /// <summary>PAGE UP key.</summary>
- PRIOR = 0x21,
- /// <summary>PAGE DOWN key.</summary>
- NEXT = 0x22,
- /// <summary>END key.</summary>
- END = 0x23,
- /// <summary>HOME key.</summary>
- HOME = 0x24,
- /// <summary>LEFT ARROW key.</summary>
- LEFT = 0x25,
- /// <summary>UP ARROW key.</summary>
- UP = 0x26,
- /// <summary>RIGHT ARROW key.</summary>
- RIGHT = 0x27,
- /// <summary>DOWN ARROW key.</summary>
- DOWN = 0x28,
- /// <summary>SELECT key.</summary>
- SELECT = 0x29,
- /// <summary>PRINT key.</summary>
- PRINT = 0x2A,
- /// <summary>EXECUTE key</summary>
- EXECUTE = 0x2B,
- /// <summary>PRINT SCREEN key</summary>
- SNAPSHOT = 0x2C,
- /// <summary>INS key</summary>
- INSERT = 0x2D,
- /// <summary>DEL key</summary>
- DELETE = 0x2E,
- /// <summary>HELP key</summary>
- HELP = 0x2F,
- /// <summary>Left Windows key (Natural keyboard)</summary>
- LWIN = 0x5B,
- /// <summary>Right Windows key (Natural keyboard)</summary>
- RWIN = 0x5C,
- /// <summary>Applications key (Natural keyboard)</summary>
- APPS = 0x5D,
- /// <summary>Computer Sleep key</summary>
- SLEEP = 0x5F,
- /// <summary>Numeric keypad 0 key</summary>
- NUMPAD0 = 0x60,
- /// <summary>Numeric keypad 1 key</summary>
- NUMPAD1 = 0x61,
- /// <summary>Numeric keypad 2 key</summary>
- NUMPAD2 = 0x62,
- /// <summary>Numeric keypad 3 key</summary>
- NUMPAD3 = 0x63,
- /// <summary>Numeric keypad 4 key</summary>
- NUMPAD4 = 0x64,
- /// <summary>Numeric keypad 5 key</summary>
- NUMPAD5 = 0x65,
- /// <summary>Numeric keypad 6 key</summary>
- NUMPAD6 = 0x66,
- /// <summary>Numeric keypad 7 key</summary>
- NUMPAD7 = 0x67,
- /// <summary>Numeric keypad 8 key</summary>
- NUMPAD8 = 0x68,
- /// <summary>Numeric keypad 9 key</summary>
- NUMPAD9 = 0x69,
- /// <summary>Multiply key</summary>
- MULTIPLY = 0x6A,
- /// <summary>Add key</summary>
- ADD = 0x6B,
- /// <summary>Separator key</summary>
- SEPARATOR = 0x6C,
- /// <summary>Subtract key</summary>
- SUBTRACT = 0x6D,
- /// <summary>Decimal key</summary>
- DECIMAL = 0x6E,
- /// <summary>Divide key</summary>
- DIVIDE = 0x6F,
- /// <summary>F1 key</summary>
- F1 = 0x70,
- /// <summary>F2 key</summary>
- F2 = 0x71,
- /// <summary>F3 key</summary>
- F3 = 0x72,
- /// <summary>F4 key</summary>
- F4 = 0x73,
- /// <summary>F5 key</summary>
- F5 = 0x74,
- /// <summary>F6 key</summary>
- F6 = 0x75,
- /// <summary>F7 key</summary>
- F7 = 0x76,
- /// <summary>F8 key</summary>
- F8 = 0x77,
- /// <summary>F9 key</summary>
- F9 = 0x78,
- /// <summary>F10 key</summary>
- F10 = 0x79,
- /// <summary>F11 key</summary>
- F11 = 0x7A,
- /// <summary>F12 key</summary>
- F12 = 0x7B,
- /// <summary>F13 key</summary>
- F13 = 0x7C,
- /// <summary>F14 key</summary>
- F14 = 0x7D,
- /// <summary>F15 key</summary>
- F15 = 0x7E,
- /// <summary>F16 key</summary>
- F16 = 0x7F,
- /// <summary>F17 key</summary>
- F17 = 0x80,
- /// <summary>F18 key</summary>
- F18 = 0x81,
- /// <summary>F19 key</summary>
- F19 = 0x82,
- /// <summary>F20 key</summary>
- F20 = 0x83,
- /// <summary>F21 key</summary>
- F21 = 0x84,
- /// <summary>F22 key</summary>
- F22 = 0x85,
- /// <summary>F23 key</summary>
- F23 = 0x86,
- /// <summary>F24 key</summary>
- F24 = 0x87,
- /// <summary>NUM LOCK key</summary>
- NUMLOCK = 0x90,
- /// <summary>SCROLL LOCK key</summary>
- SCROLL = 0x91,
- /// <summary>NEC PC-9800 kbd definition: '=' key on numpad</summary>
- OEM_NEC_EQUAL = 0x92,
- /// <summary>Fujitsu/OASYS kbd definition: 'Dictionary' key</summary>
- OEM_FJ_JISHO = 0x92,
- /// <summary>Fujitsu/OASYS kbd definition: 'Unregister word' key</summary>
- OEM_FJ_MASSHOU = 0x93,
- /// <summary>Fujitsu/OASYS kbd definition: 'Register word' key</summary>
- OEM_FJ_TOUROKU = 0x94,
- /// <summary>Fujitsu/OASYS kbd definition: 'Left OYAYUBI' key</summary>
- OEM_FJ_LOYA = 0x95,
- /// <summary>Fujitsu/OASYS kbd definition: 'Right OYAYUBI' key</summary>
- OEM_FJ_ROYA = 0x96,
- /// <summary>Left SHIFT key</summary>
- LSHIFT = 0xA0,
- /// <summary>Right SHIFT key</summary>
- RSHIFT = 0xA1,
- /// <summary>Left CONTROL key</summary>
- LCONTROL = 0xA2,
- /// <summary>Right CONTROL key</summary>
- RCONTROL = 0xA3,
- /// <summary>Left MENU key (Left Alt key)</summary>
- LMENU = 0xA4,
- /// <summary>Right MENU key (Right Alt key)</summary>
- RMENU = 0xA5,
- /// <summary>Browser Back key</summary>
- BROWSER_BACK = 0xA6,
- /// <summary>Browser Forward key</summary>
- BROWSER_FORWARD = 0xA7,
- /// <summary>Browser Refresh key</summary>
- BROWSER_REFRESH = 0xA8,
- /// <summary>Browser Stop key</summary>
- BROWSER_STOP = 0xA9,
- /// <summary>Browser Search key</summary>
- BROWSER_SEARCH = 0xAA,
- /// <summary>Browser Favorites key</summary>
- BROWSER_FAVORITES = 0xAB,
- /// <summary>Browser Home key</summary>
- BROWSER_HOME = 0xAC,
- /// <summary>Volume Mute key</summary>
- VOLUME_MUTE = 0xAD,
- /// <summary>Volume Down key</summary>
- VOLUME_DOWN = 0xAE,
- /// <summary>Volume Up key</summary>
- VOLUME_UP = 0xAF,
- /// <summary>Next Track key</summary>
- MEDIA_NEXT_TRACK = 0xB0,
- /// <summary>Previous Track key</summary>
- MEDIA_PREV_TRACK = 0xB1,
- /// <summary>Stop Media key</summary>
- MEDIA_STOP = 0xB2,
- /// <summary>Play/Pause Media key</summary>
- MEDIA_PLAY_PAUSE = 0xB3,
- /// <summary>Start Mail key</summary>
- LAUNCH_MAIL = 0xB4,
- /// <summary>Select Media key</summary>
- LAUNCH_MEDIA_SELECT = 0xB5,
- /// <summary>Start Application 1 key</summary>
- LAUNCH_APP1 = 0xB6,
- /// <summary>Start Application 2 key</summary>
- LAUNCH_APP2 = 0xB7,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key</summary>
- OEM_1 = 0xBA,
- /// <summary>For any country/region, the '+' key</summary>
- OEM_PLUS = 0xBB,
- /// <summary>For any country/region, the ',' key</summary>
- OEM_COMMA = 0xBC,
- /// <summary>For any country/region, the '-' key</summary>
- OEM_MINUS = 0xBD,
- /// <summary>For any country/region, the '.' key</summary>
- OEM_PERIOD = 0xBE,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key</summary>
- OEM_2 = 0xBF,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key</summary>
- OEM_3 = 0xC0,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key</summary>
- OEM_4 = 0xDB,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key</summary>
- OEM_5 = 0xDC,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key</summary>
- OEM_6 = 0xDD,
- /// <summary>
- /// Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the
- /// 'single-quote/double-quote' key
- /// </summary>
- OEM_7 = 0xDE,
- /// <summary>Used for miscellaneous characters; it can vary by keyboard.</summary>
- OEM_8 = 0xDF,
- /// <summary>'AX' key on Japanese AX kbd</summary>
- OEM_AX = 0xE1,
- /// <summary>Either the angle bracket key or the backslash key on the RT 102-key keyboard</summary>
- OEM_102 = 0xE2,
- /// <summary>Help key on ICO</summary>
- ICO_HELP = 0xE3,
- /// <summary>00 key on ICO</summary>
- ICO_00 = 0xE4,
- /// <summary>Process key</summary>
- PROCESSKEY = 0xE5,
- /// <summary>Clear key on ICO</summary>
- ICO_CLEAR = 0xE6,
- /// <summary>Packet key to be used to pass Unicode characters as if they were keystrokes</summary>
- PACKET = 0xE7,
- /// <summary>Reset key</summary>
- OEM_RESET = 0xE9,
- /// <summary>Jump key</summary>
- OEM_JUMP = 0xEA,
- /// <summary>PA1 key</summary>
- OEM_PA1 = 0xEB,
- /// <summary>PA2 key</summary>
- OEM_PA2 = 0xEC,
- /// <summary>PA3 key</summary>
- OEM_PA3 = 0xED,
- /// <summary>WsCtrl key</summary>
- OEM_WSCTRL = 0xEE,
- /// <summary>CuSel key</summary>
- OEM_CUSEL = 0xEF,
- /// <summary>Attn key</summary>
- OEM_ATTN = 0xF0,
- /// <summary>Finish key</summary>
- OEM_FINISH = 0xF1,
- /// <summary>Copy key</summary>
- OEM_COPY = 0xF2,
- /// <summary>Auto key</summary>
- OEM_AUTO = 0xF3,
- /// <summary>Enlw key</summary>
- OEM_ENLW = 0xF4,
- /// <summary>BackTab key</summary>
- OEM_BACKTAB = 0xF5,
- /// <summary>Attn key</summary>
- ATTN = 0xF6,
- /// <summary>CrSel key</summary>
- CRSEL = 0xF7,
- /// <summary>ExSel key</summary>
- EXSEL = 0xF8,
- /// <summary>Erase EOF key</summary>
- EREOF = 0xF9,
- /// <summary>Play key</summary>
- PLAY = 0xFA,
- /// <summary>Zoom key</summary>
- ZOOM = 0xFB,
- /// <summary>Reserved</summary>
- NONAME = 0xFC,
- /// <summary>PA1 key</summary>
- PA1 = 0xFD,
- /// <summary>Clear key</summary>
- OEM_CLEAR = 0xFE
- }
- // BUGBUG: This database makes no sense. It is not possible to map a VK code to a character without knowing the keyboard layout
- // It should be deleted.
- private static readonly HashSet<ScanCodeMapping> _scanCodes = new ()
- {
- new ScanCodeMapping (
- 1,
- VK.ESCAPE,
- 0,
- '\u001B'
- ), // Escape
- new ScanCodeMapping (
- 1,
- VK.ESCAPE,
- ConsoleModifiers.Shift,
- '\u001B'
- ),
- new ScanCodeMapping (
- 2,
- (VK)'1',
- 0,
- '1'
- ), // D1
- new ScanCodeMapping (
- 2,
- (VK)'1',
- ConsoleModifiers.Shift,
- '!'
- ),
- new ScanCodeMapping (
- 3,
- (VK)'2',
- 0,
- '2'
- ), // D2
- new ScanCodeMapping (
- 3,
- (VK)'2',
- ConsoleModifiers.Shift,
- '\"'
- ), // BUGBUG: This is true for Portuguese keyboard, but not ENG (@) or DEU (")
- new ScanCodeMapping (
- 3,
- (VK)'2',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '@'
- ),
- new ScanCodeMapping (
- 4,
- (VK)'3',
- 0,
- '3'
- ), // D3
- new ScanCodeMapping (
- 4,
- (VK)'3',
- ConsoleModifiers.Shift,
- '#'
- ),
- new ScanCodeMapping (
- 4,
- (VK)'3',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '£'
- ),
- new ScanCodeMapping (
- 5,
- (VK)'4',
- 0,
- '4'
- ), // D4
- new ScanCodeMapping (
- 5,
- (VK)'4',
- ConsoleModifiers.Shift,
- '$'
- ),
- new ScanCodeMapping (
- 5,
- (VK)'4',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '§'
- ),
- new ScanCodeMapping (
- 6,
- (VK)'5',
- 0,
- '5'
- ), // D5
- new ScanCodeMapping (
- 6,
- (VK)'5',
- ConsoleModifiers.Shift,
- '%'
- ),
- new ScanCodeMapping (
- 6,
- (VK)'5',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '€'
- ),
- new ScanCodeMapping (
- 7,
- (VK)'6',
- 0,
- '6'
- ), // D6
- new ScanCodeMapping (
- 7,
- (VK)'6',
- ConsoleModifiers.Shift,
- '&'
- ),
- new ScanCodeMapping (
- 8,
- (VK)'7',
- 0,
- '7'
- ), // D7
- new ScanCodeMapping (
- 8,
- (VK)'7',
- ConsoleModifiers.Shift,
- '/'
- ),
- new ScanCodeMapping (
- 8,
- (VK)'7',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '{'
- ),
- new ScanCodeMapping (
- 9,
- (VK)'8',
- 0,
- '8'
- ), // D8
- new ScanCodeMapping (
- 9,
- (VK)'8',
- ConsoleModifiers.Shift,
- '('
- ),
- new ScanCodeMapping (
- 9,
- (VK)'8',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '['
- ),
- new ScanCodeMapping (
- 10,
- (VK)'9',
- 0,
- '9'
- ), // D9
- new ScanCodeMapping (
- 10,
- (VK)'9',
- ConsoleModifiers.Shift,
- ')'
- ),
- new ScanCodeMapping (
- 10,
- (VK)'9',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- ']'
- ),
- new ScanCodeMapping (
- 11,
- (VK)'0',
- 0,
- '0'
- ), // D0
- new ScanCodeMapping (
- 11,
- (VK)'0',
- ConsoleModifiers.Shift,
- '='
- ),
- new ScanCodeMapping (
- 11,
- (VK)'0',
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '}'
- ),
- new ScanCodeMapping (
- 12,
- VK.OEM_4,
- 0,
- '\''
- ), // Oem4
- new ScanCodeMapping (
- 12,
- VK.OEM_4,
- ConsoleModifiers.Shift,
- '?'
- ),
- new ScanCodeMapping (
- 13,
- VK.OEM_6,
- 0,
- '+'
- ), // Oem6
- new ScanCodeMapping (
- 13,
- VK.OEM_6,
- ConsoleModifiers.Shift,
- '*'
- ),
- new ScanCodeMapping (
- 14,
- VK.BACK,
- 0,
- '\u0008'
- ), // Backspace
- new ScanCodeMapping (
- 14,
- VK.BACK,
- ConsoleModifiers.Shift,
- '\u0008'
- ),
- new ScanCodeMapping (
- 15,
- VK.TAB,
- 0,
- '\u0009'
- ), // Tab
- new ScanCodeMapping (
- 15,
- VK.TAB,
- ConsoleModifiers.Shift,
- '\u000F'
- ),
- new ScanCodeMapping (
- 16,
- (VK)'Q',
- 0,
- 'q'
- ), // Q
- new ScanCodeMapping (
- 16,
- (VK)'Q',
- ConsoleModifiers.Shift,
- 'Q'
- ),
- new ScanCodeMapping (
- 17,
- (VK)'W',
- 0,
- 'w'
- ), // W
- new ScanCodeMapping (
- 17,
- (VK)'W',
- ConsoleModifiers.Shift,
- 'W'
- ),
- new ScanCodeMapping (
- 18,
- (VK)'E',
- 0,
- 'e'
- ), // E
- new ScanCodeMapping (
- 18,
- (VK)'E',
- ConsoleModifiers.Shift,
- 'E'
- ),
- new ScanCodeMapping (
- 19,
- (VK)'R',
- 0,
- 'r'
- ), // R
- new ScanCodeMapping (
- 19,
- (VK)'R',
- ConsoleModifiers.Shift,
- 'R'
- ),
- new ScanCodeMapping (
- 20,
- (VK)'T',
- 0,
- 't'
- ), // T
- new ScanCodeMapping (
- 20,
- (VK)'T',
- ConsoleModifiers.Shift,
- 'T'
- ),
- new ScanCodeMapping (
- 21,
- (VK)'Y',
- 0,
- 'y'
- ), // Y
- new ScanCodeMapping (
- 21,
- (VK)'Y',
- ConsoleModifiers.Shift,
- 'Y'
- ),
- new ScanCodeMapping (
- 22,
- (VK)'U',
- 0,
- 'u'
- ), // U
- new ScanCodeMapping (
- 22,
- (VK)'U',
- ConsoleModifiers.Shift,
- 'U'
- ),
- new ScanCodeMapping (
- 23,
- (VK)'I',
- 0,
- 'i'
- ), // I
- new ScanCodeMapping (
- 23,
- (VK)'I',
- ConsoleModifiers.Shift,
- 'I'
- ),
- new ScanCodeMapping (
- 24,
- (VK)'O',
- 0,
- 'o'
- ), // O
- new ScanCodeMapping (
- 24,
- (VK)'O',
- ConsoleModifiers.Shift,
- 'O'
- ),
- new ScanCodeMapping (
- 25,
- (VK)'P',
- 0,
- 'p'
- ), // P
- new ScanCodeMapping (
- 25,
- (VK)'P',
- ConsoleModifiers.Shift,
- 'P'
- ),
- new ScanCodeMapping (
- 26,
- VK.OEM_PLUS,
- 0,
- '+'
- ), // OemPlus
- new ScanCodeMapping (
- 26,
- VK.OEM_PLUS,
- ConsoleModifiers.Shift,
- '*'
- ),
- new ScanCodeMapping (
- 26,
- VK.OEM_PLUS,
- ConsoleModifiers.Alt
- | ConsoleModifiers.Control,
- '¨'
- ),
- new ScanCodeMapping (
- 27,
- VK.OEM_1,
- 0,
- '´'
- ), // Oem1
- new ScanCodeMapping (
- 27,
- VK.OEM_1,
- ConsoleModifiers.Shift,
- '`'
- ),
- new ScanCodeMapping (
- 28,
- VK.RETURN,
- 0,
- '\u000D'
- ), // Enter
- new ScanCodeMapping (
- 28,
- VK.RETURN,
- ConsoleModifiers.Shift,
- '\u000D'
- ),
- new ScanCodeMapping (
- 29,
- VK.CONTROL,
- 0,
- '\0'
- ), // Control
- new ScanCodeMapping (
- 29,
- VK.CONTROL,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 30,
- (VK)'A',
- 0,
- 'a'
- ), // A
- new ScanCodeMapping (
- 30,
- (VK)'A',
- ConsoleModifiers.Shift,
- 'A'
- ),
- new ScanCodeMapping (
- 31,
- (VK)'S',
- 0,
- 's'
- ), // S
- new ScanCodeMapping (
- 31,
- (VK)'S',
- ConsoleModifiers.Shift,
- 'S'
- ),
- new ScanCodeMapping (
- 32,
- (VK)'D',
- 0,
- 'd'
- ), // D
- new ScanCodeMapping (
- 32,
- (VK)'D',
- ConsoleModifiers.Shift,
- 'D'
- ),
- new ScanCodeMapping (
- 33,
- (VK)'F',
- 0,
- 'f'
- ), // F
- new ScanCodeMapping (
- 33,
- (VK)'F',
- ConsoleModifiers.Shift,
- 'F'
- ),
- new ScanCodeMapping (
- 34,
- (VK)'G',
- 0,
- 'g'
- ), // G
- new ScanCodeMapping (
- 34,
- (VK)'G',
- ConsoleModifiers.Shift,
- 'G'
- ),
- new ScanCodeMapping (
- 35,
- (VK)'H',
- 0,
- 'h'
- ), // H
- new ScanCodeMapping (
- 35,
- (VK)'H',
- ConsoleModifiers.Shift,
- 'H'
- ),
- new ScanCodeMapping (
- 36,
- (VK)'J',
- 0,
- 'j'
- ), // J
- new ScanCodeMapping (
- 36,
- (VK)'J',
- ConsoleModifiers.Shift,
- 'J'
- ),
- new ScanCodeMapping (
- 37,
- (VK)'K',
- 0,
- 'k'
- ), // K
- new ScanCodeMapping (
- 37,
- (VK)'K',
- ConsoleModifiers.Shift,
- 'K'
- ),
- new ScanCodeMapping (
- 38,
- (VK)'L',
- 0,
- 'l'
- ), // L
- new ScanCodeMapping (
- 38,
- (VK)'L',
- ConsoleModifiers.Shift,
- 'L'
- ),
- new ScanCodeMapping (
- 39,
- VK.OEM_3,
- 0,
- '`'
- ), // Oem3 (Backtick/Grave)
- new ScanCodeMapping (
- 39,
- VK.OEM_3,
- ConsoleModifiers.Shift,
- '~'
- ),
- new ScanCodeMapping (
- 40,
- VK.OEM_7,
- 0,
- '\''
- ), // Oem7 (Single Quote)
- new ScanCodeMapping (
- 40,
- VK.OEM_7,
- ConsoleModifiers.Shift,
- '\"'
- ),
- new ScanCodeMapping (
- 41,
- VK.OEM_5,
- 0,
- '\\'
- ), // Oem5 (Backslash)
- new ScanCodeMapping (
- 41,
- VK.OEM_5,
- ConsoleModifiers.Shift,
- '|'
- ),
- new ScanCodeMapping (
- 42,
- VK.LSHIFT,
- 0,
- '\0'
- ), // Left Shift
- new ScanCodeMapping (
- 42,
- VK.LSHIFT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 43,
- VK.OEM_2,
- 0,
- '/'
- ), // Oem2 (Forward Slash)
- new ScanCodeMapping (
- 43,
- VK.OEM_2,
- ConsoleModifiers.Shift,
- '?'
- ),
- new ScanCodeMapping (
- 44,
- (VK)'Z',
- 0,
- 'z'
- ), // Z
- new ScanCodeMapping (
- 44,
- (VK)'Z',
- ConsoleModifiers.Shift,
- 'Z'
- ),
- new ScanCodeMapping (
- 45,
- (VK)'X',
- 0,
- 'x'
- ), // X
- new ScanCodeMapping (
- 45,
- (VK)'X',
- ConsoleModifiers.Shift,
- 'X'
- ),
- new ScanCodeMapping (
- 46,
- (VK)'C',
- 0,
- 'c'
- ), // C
- new ScanCodeMapping (
- 46,
- (VK)'C',
- ConsoleModifiers.Shift,
- 'C'
- ),
- new ScanCodeMapping (
- 47,
- (VK)'V',
- 0,
- 'v'
- ), // V
- new ScanCodeMapping (
- 47,
- (VK)'V',
- ConsoleModifiers.Shift,
- 'V'
- ),
- new ScanCodeMapping (
- 48,
- (VK)'B',
- 0,
- 'b'
- ), // B
- new ScanCodeMapping (
- 48,
- (VK)'B',
- ConsoleModifiers.Shift,
- 'B'
- ),
- new ScanCodeMapping (
- 49,
- (VK)'N',
- 0,
- 'n'
- ), // N
- new ScanCodeMapping (
- 49,
- (VK)'N',
- ConsoleModifiers.Shift,
- 'N'
- ),
- new ScanCodeMapping (
- 50,
- (VK)'M',
- 0,
- 'm'
- ), // M
- new ScanCodeMapping (
- 50,
- (VK)'M',
- ConsoleModifiers.Shift,
- 'M'
- ),
- new ScanCodeMapping (
- 51,
- VK.OEM_COMMA,
- 0,
- ','
- ), // OemComma
- new ScanCodeMapping (
- 51,
- VK.OEM_COMMA,
- ConsoleModifiers.Shift,
- '<'
- ),
- new ScanCodeMapping (
- 52,
- VK.OEM_PERIOD,
- 0,
- '.'
- ), // OemPeriod
- new ScanCodeMapping (
- 52,
- VK.OEM_PERIOD,
- ConsoleModifiers.Shift,
- '>'
- ),
- new ScanCodeMapping (
- 53,
- VK.OEM_MINUS,
- 0,
- '-'
- ), // OemMinus
- new ScanCodeMapping (
- 53,
- VK.OEM_MINUS,
- ConsoleModifiers.Shift,
- '_'
- ),
- new ScanCodeMapping (
- 54,
- VK.RSHIFT,
- 0,
- '\0'
- ), // Right Shift
- new ScanCodeMapping (
- 54,
- VK.RSHIFT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 55,
- VK.PRINT,
- 0,
- '\0'
- ), // Print Screen
- new ScanCodeMapping (
- 55,
- VK.PRINT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 56,
- VK.LMENU,
- 0,
- '\0'
- ), // Alt
- new ScanCodeMapping (
- 56,
- VK.LMENU,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 57,
- VK.SPACE,
- 0,
- ' '
- ), // Spacebar
- new ScanCodeMapping (
- 57,
- VK.SPACE,
- ConsoleModifiers.Shift,
- ' '
- ),
- new ScanCodeMapping (
- 58,
- VK.CAPITAL,
- 0,
- '\0'
- ), // Caps Lock
- new ScanCodeMapping (
- 58,
- VK.CAPITAL,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 59,
- VK.F1,
- 0,
- '\0'
- ), // F1
- new ScanCodeMapping (
- 59,
- VK.F1,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 60,
- VK.F2,
- 0,
- '\0'
- ), // F2
- new ScanCodeMapping (
- 60,
- VK.F2,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 61,
- VK.F3,
- 0,
- '\0'
- ), // F3
- new ScanCodeMapping (
- 61,
- VK.F3,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 62,
- VK.F4,
- 0,
- '\0'
- ), // F4
- new ScanCodeMapping (
- 62,
- VK.F4,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 63,
- VK.F5,
- 0,
- '\0'
- ), // F5
- new ScanCodeMapping (
- 63,
- VK.F5,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 64,
- VK.F6,
- 0,
- '\0'
- ), // F6
- new ScanCodeMapping (
- 64,
- VK.F6,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 65,
- VK.F7,
- 0,
- '\0'
- ), // F7
- new ScanCodeMapping (
- 65,
- VK.F7,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 66,
- VK.F8,
- 0,
- '\0'
- ), // F8
- new ScanCodeMapping (
- 66,
- VK.F8,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 67,
- VK.F9,
- 0,
- '\0'
- ), // F9
- new ScanCodeMapping (
- 67,
- VK.F9,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 68,
- VK.F10,
- 0,
- '\0'
- ), // F10
- new ScanCodeMapping (
- 68,
- VK.F10,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 69,
- VK.NUMLOCK,
- 0,
- '\0'
- ), // Num Lock
- new ScanCodeMapping (
- 69,
- VK.NUMLOCK,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 70,
- VK.SCROLL,
- 0,
- '\0'
- ), // Scroll Lock
- new ScanCodeMapping (
- 70,
- VK.SCROLL,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 71,
- VK.HOME,
- 0,
- '\0'
- ), // Home
- new ScanCodeMapping (
- 71,
- VK.HOME,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 72,
- VK.UP,
- 0,
- '\0'
- ), // Up Arrow
- new ScanCodeMapping (
- 72,
- VK.UP,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 73,
- VK.PRIOR,
- 0,
- '\0'
- ), // Page Up
- new ScanCodeMapping (
- 73,
- VK.PRIOR,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 74,
- VK.SUBTRACT,
- 0,
- '-'
- ), // Subtract (Num Pad '-')
- new ScanCodeMapping (
- 74,
- VK.SUBTRACT,
- ConsoleModifiers.Shift,
- '-'
- ),
- new ScanCodeMapping (
- 75,
- VK.LEFT,
- 0,
- '\0'
- ), // Left Arrow
- new ScanCodeMapping (
- 75,
- VK.LEFT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 76,
- VK.CLEAR,
- 0,
- '\0'
- ), // Center key (Num Pad 5 with Num Lock off)
- new ScanCodeMapping (
- 76,
- VK.CLEAR,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 77,
- VK.RIGHT,
- 0,
- '\0'
- ), // Right Arrow
- new ScanCodeMapping (
- 77,
- VK.RIGHT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 78,
- VK.ADD,
- 0,
- '+'
- ), // Add (Num Pad '+')
- new ScanCodeMapping (
- 78,
- VK.ADD,
- ConsoleModifiers.Shift,
- '+'
- ),
- new ScanCodeMapping (
- 79,
- VK.END,
- 0,
- '\0'
- ), // End
- new ScanCodeMapping (
- 79,
- VK.END,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 80,
- VK.DOWN,
- 0,
- '\0'
- ), // Down Arrow
- new ScanCodeMapping (
- 80,
- VK.DOWN,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 81,
- VK.NEXT,
- 0,
- '\0'
- ), // Page Down
- new ScanCodeMapping (
- 81,
- VK.NEXT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 82,
- VK.INSERT,
- 0,
- '\0'
- ), // Insert
- new ScanCodeMapping (
- 82,
- VK.INSERT,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 83,
- VK.DELETE,
- 0,
- '\0'
- ), // Delete
- new ScanCodeMapping (
- 83,
- VK.DELETE,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 86,
- VK.OEM_102,
- 0,
- '<'
- ), // OEM 102 (Typically '<' or '|' key next to Left Shift)
- new ScanCodeMapping (
- 86,
- VK.OEM_102,
- ConsoleModifiers.Shift,
- '>'
- ),
- new ScanCodeMapping (
- 87,
- VK.F11,
- 0,
- '\0'
- ), // F11
- new ScanCodeMapping (
- 87,
- VK.F11,
- ConsoleModifiers.Shift,
- '\0'
- ),
- new ScanCodeMapping (
- 88,
- VK.F12,
- 0,
- '\0'
- ), // F12
- new ScanCodeMapping (
- 88,
- VK.F12,
- ConsoleModifiers.Shift,
- '\0'
- )
- };
- /// <summary>Decode a <see cref="ConsoleKeyInfo"/> that is using <see cref="ConsoleKey.Packet"/>.</summary>
- /// <param name="consoleKeyInfo">The console key info.</param>
- /// <returns>The decoded <see cref="ConsoleKeyInfo"/> or the <paramref name="consoleKeyInfo"/>.</returns>
- /// <remarks>
- /// If it's a <see cref="ConsoleKey.Packet"/> the <see cref="ConsoleKeyInfo.KeyChar"/> may be a
- /// <see cref="ConsoleKeyInfo.Key"/> or a <see cref="ConsoleKeyInfo.KeyChar"/> value.
- /// </remarks>
- public static ConsoleKeyInfo DecodeVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
- {
- if (consoleKeyInfo.Key != ConsoleKey.Packet)
- {
- return consoleKeyInfo;
- }
- return GetConsoleKeyInfoFromKeyChar (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out _);
- }
- /// <summary>
- /// Encode the <see cref="ConsoleKeyInfo.KeyChar"/> with the <see cref="ConsoleKeyInfo.Key"/> if the first a byte
- /// length, otherwise only the KeyChar is considered and searched on the database.
- /// </summary>
- /// <param name="consoleKeyInfo">The console key info.</param>
- /// <returns>The encoded KeyChar with the Key if both can be shifted, otherwise only the KeyChar.</returns>
- /// <remarks>This is useful to use with the <see cref="ConsoleKey.Packet"/>.</remarks>
- public static char EncodeKeyCharForVKPacket (ConsoleKeyInfo consoleKeyInfo)
- {
- char keyChar = consoleKeyInfo.KeyChar;
- ConsoleKey consoleKey = consoleKeyInfo.Key;
- if (keyChar != 0 && consoleKeyInfo.KeyChar < byte.MaxValue && consoleKey == ConsoleKey.None)
- {
- // try to get the ConsoleKey
- ScanCodeMapping scode = _scanCodes.FirstOrDefault (e => e.UnicodeChar == keyChar);
- if (scode is { })
- {
- consoleKey = (ConsoleKey)scode.VirtualKey;
- }
- }
- if (keyChar < byte.MaxValue && consoleKey != ConsoleKey.None)
- {
- keyChar = (char)((consoleKeyInfo.KeyChar << 8) | (byte)consoleKey);
- }
- return keyChar;
- }
- }
|