Key.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json.Serialization;
  7. namespace Terminal.Gui;
  8. /// <summary>
  9. /// Provides an abstraction for common keyboard operations and state. Used for processing keyboard input and raising keyboard events.
  10. /// </summary>
  11. /// <remarks>
  12. /// <para>
  13. /// This class provides a high-level abstraction with helper methods and properties for common keyboard operations. Use this class
  14. /// instead of the <see cref="Terminal.Gui.KeyCode"/> enumeration for keyboard input whenever possible.
  15. /// </para>
  16. /// <para>
  17. ///
  18. /// </para>
  19. /// <para>
  20. /// The default value for <see cref="Key"/> is <see cref="KeyCode.Null"/> and can be tested using <see cref="Key.Empty"/>.
  21. /// </para>
  22. /// <para>
  23. /// <list type="table">
  24. /// <listheader>
  25. /// <term>Concept</term><description>Definition</description>
  26. /// </listheader>
  27. /// <item>
  28. /// <term>Testing Shift State</term>
  29. /// <description>
  30. /// The <c>Is</c> properties (<see cref="IsShift"/>,<see cref="IsCtrl"/>, <see cref="IsAlt"/>) test for shift state; whether the key press was modified by a shift key.
  31. /// </description>
  32. /// </item>
  33. /// <item>
  34. /// <term>Adding Shift State</term>
  35. /// <description>
  36. /// The <c>With</c> properties (<see cref="WithShift"/>,<see cref="WithCtrl"/>, <see cref="WithAlt"/>) return a copy of the Key with the shift modifier applied. This
  37. /// is useful for specifying a key that requires a shift modifier (e.g. <c>var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;</c>.
  38. /// </description>
  39. /// </item>
  40. /// <item>
  41. /// <term>Removing Shift State</term>
  42. /// <description>
  43. /// The <c>No</c> properties (<see cref="NoShift"/>,<see cref="NoCtrl"/>, <see cref="NoAlt"/>) return a copy of the Key with the shift modifier removed. This
  44. /// is useful for specifying a key that does not require a shift modifier (e.g. <c>var ControlDelete = ControlAltDelete.NoCtrl;</c>.
  45. /// </description>
  46. /// </item>
  47. /// <item>
  48. /// <term>Encoding of A..Z</term>
  49. /// <description>
  50. /// Lowercase alpha keys are encoded (in <see cref="Key.KeyCode"/>) as values between 65 and 90 corresponding to
  51. /// the un-shifted A to Z keys on a keyboard. Properties are provided for these (e.g. <see cref="Key.A"/>, <see cref="Key.B"/>, etc.).
  52. /// Even though the encoded values are the same as the ASCII values for uppercase characters, these enum values represent *lowercase*, un-shifted characters.
  53. /// </description>
  54. /// </item>
  55. /// <item>
  56. /// <term>Persistence as strings</term>
  57. /// <description>
  58. /// Keys are persisted as <c>"[Modifiers]+[Key]</c>. For example <c>new Key(Key.Delete).WithAlt.WithDel</c> is persisted as <c>"Ctrl+Alt+Delete"</c>. See <see cref="ToString()"/>
  59. /// and <see cref="TryParse(string, out Terminal.Gui.Key)"/> for more information.
  60. /// </description>
  61. /// </item>
  62. /// </list>
  63. /// </para>
  64. /// </remarks>
  65. [JsonConverter (typeof (KeyJsonConverter))]
  66. public class Key : EventArgs, IEquatable<Key> {
  67. /// <summary>
  68. /// Constructs a new <see cref="Key"/>
  69. /// </summary>
  70. public Key () : this (KeyCode.Null) { }
  71. /// <summary>
  72. /// Constructs a new <see cref="Key"/> from the provided Key value
  73. /// </summary>
  74. /// <param name="k">The key</param>
  75. public Key (KeyCode k) => KeyCode = k;
  76. /// <summary>
  77. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  78. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  79. /// </summary>
  80. public bool Handled { get; set; } = false;
  81. /// <summary>
  82. /// The encoded key value.
  83. /// </summary>
  84. /// <para>
  85. /// IMPORTANT: Lowercase alpha keys are encoded (in <see cref="Gui.KeyCode"/>) as values between 65 and 90 corresponding to the un-shifted A to Z keys on a keyboard. Enum values
  86. /// are provided for these (e.g. <see cref="KeyCode.A"/>, <see cref="KeyCode.B"/>, etc.). Even though the values are the same as the ASCII
  87. /// values for uppercase characters, these enum values represent *lowercase*, un-shifted characters.
  88. /// </para>
  89. /// <remarks>
  90. /// This property is the backing data for the <see cref="Key"/>. It is a <see cref="KeyCode"/> enum value.
  91. /// </remarks>
  92. [JsonInclude] [JsonConverter (typeof (KeyCodeJsonConverter))]
  93. public KeyCode KeyCode { get; set; }
  94. /// <summary>
  95. /// Enables passing the key binding scope with the event. Default is <see cref="KeyBindingScope.Focused"/>.
  96. /// </summary>
  97. public KeyBindingScope Scope { get; set; } = KeyBindingScope.Focused;
  98. /// <summary>
  99. /// The key value as a Rune. This is the actual value of the key pressed, and is independent of the modifiers.
  100. /// </summary>
  101. /// <remarks>
  102. /// If the key pressed is a letter (a-z or A-Z), this will be the upper or lower case letter depending on whether the shift key is pressed.
  103. /// If the key is outside of the <see cref="KeyCode.CharMask"/> range, this will be <see langword="default"/>.
  104. /// </remarks>
  105. public Rune AsRune => ToRune (KeyCode);
  106. /// <summary>
  107. /// Converts a <see cref="KeyCode"/> to a <see cref="Rune"/>.
  108. /// </summary>
  109. /// <remarks>
  110. /// If the key is a letter (a-z or A-Z), this will be the upper or lower case letter depending on whether the shift key is pressed.
  111. /// If the key is outside of the <see cref="KeyCode.CharMask"/> range, this will be <see langword="default"/>.
  112. /// </remarks>
  113. /// <param name="key"></param>
  114. /// <returns>The key converted to a rune. <see langword="default"/> if conversion is not possible.</returns>
  115. public static Rune ToRune (KeyCode key)
  116. {
  117. if (key is KeyCode.Null or KeyCode.SpecialMask || key.HasFlag (KeyCode.CtrlMask) || key.HasFlag (KeyCode.AltMask)) {
  118. return default;
  119. }
  120. // Extract the base key (removing modifier flags)
  121. var baseKey = key & ~KeyCode.CtrlMask & ~KeyCode.AltMask & ~KeyCode.ShiftMask;
  122. switch (baseKey) {
  123. case >= KeyCode.A and <= KeyCode.Z when !key.HasFlag (KeyCode.ShiftMask):
  124. return new Rune ((char)(baseKey + 32));
  125. case >= KeyCode.A and <= KeyCode.Z:
  126. return new Rune ((char)baseKey);
  127. case > KeyCode.Null and < KeyCode.A:
  128. return new Rune ((char)baseKey);
  129. }
  130. if (Enum.IsDefined (typeof (KeyCode), baseKey)) {
  131. return default;
  132. }
  133. return new Rune ((char)baseKey);
  134. }
  135. /// <summary>
  136. /// Gets a value indicating whether the Shift key was pressed.
  137. /// </summary>
  138. /// <value><see langword="true"/> if is shift; otherwise, <see langword="false"/>.</value>
  139. public bool IsShift => (KeyCode & KeyCode.ShiftMask) != 0;
  140. /// <summary>
  141. /// Gets a value indicating whether the Alt key was pressed (real or synthesized)
  142. /// </summary>
  143. /// <value><see langword="true"/> if is alternate; otherwise, <see langword="false"/>.</value>
  144. public bool IsAlt => (KeyCode & KeyCode.AltMask) != 0;
  145. /// <summary>
  146. /// Gets a value indicating whether the Ctrl key was pressed.
  147. /// </summary>
  148. /// <value><see langword="true"/> if is ctrl; otherwise, <see langword="false"/>.</value>
  149. public bool IsCtrl => (KeyCode & KeyCode.CtrlMask) != 0;
  150. /// <summary>
  151. /// Gets a value indicating whether the KeyCode is composed of a lower case letter from 'a' to 'z', independent of the shift key.
  152. /// </summary>
  153. /// <remarks>
  154. /// IMPORTANT: Lowercase alpha keys are encoded in <see cref="Key.KeyCode"/> as values between 65 and 90 corresponding to
  155. /// the un-shifted A to Z keys on a keyboard. Helper properties are provided these (e.g. <see cref="Key.A"/>, <see cref="Key.B"/>, etc.).
  156. /// Even though the values are the same as the ASCII values for uppercase characters, these enum values represent *lowercase*, un-shifted characters.
  157. /// </remarks>
  158. public bool IsKeyCodeAtoZ => GetIsKeyCodeAtoZ (KeyCode);
  159. /// <summary>
  160. /// Tests if a KeyCode is composed of a lower case letter from 'a' to 'z', independent of the shift key.
  161. /// </summary>
  162. /// <remarks>
  163. /// IMPORTANT: Lowercase alpha keys are encoded in <see cref="Key.KeyCode"/> as values between 65 and 90 corresponding to
  164. /// the un-shifted A to Z keys on a keyboard. Helper properties are provided these (e.g. <see cref="Key.A"/>, <see cref="Key.B"/>, etc.).
  165. /// Even though the values are the same as the ASCII values for uppercase characters, these enum values represent *lowercase*, un-shifted characters.
  166. /// </remarks>
  167. public static bool GetIsKeyCodeAtoZ (KeyCode keyCode)
  168. {
  169. if ((keyCode & KeyCode.AltMask) != 0 || (keyCode & KeyCode.CtrlMask) != 0) {
  170. return false;
  171. }
  172. if ((keyCode & ~KeyCode.Space & ~KeyCode.ShiftMask) is >= KeyCode.A and <= KeyCode.Z) {
  173. return true;
  174. }
  175. return (keyCode & KeyCode.CharMask) is >= KeyCode.A and <= KeyCode.Z;
  176. }
  177. /// <summary>
  178. /// Indicates whether the <see cref="Key"/> is valid or not.
  179. /// </summary>
  180. public bool IsValid => KeyCode is not (KeyCode.Null or KeyCode.Unknown);
  181. /// <summary>
  182. /// Helper for specifying a shifted <see cref="Key"/>.
  183. /// <code>
  184. /// var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
  185. /// </code>
  186. /// </summary>
  187. public Key WithShift => new (KeyCode | KeyCode.ShiftMask);
  188. /// <summary>
  189. /// Helper for removing a shift modifier from a <see cref="Key"/>.
  190. /// <code>
  191. /// var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
  192. /// var AltDelete = ControlAltDelete.NoCtrl;
  193. /// </code>
  194. /// </summary>
  195. public Key NoShift => new (KeyCode & ~KeyCode.ShiftMask);
  196. /// <summary>
  197. /// Helper for specifying a shifted <see cref="Key"/>.
  198. /// <code>
  199. /// var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
  200. /// </code>
  201. /// </summary>
  202. public Key WithCtrl => new (KeyCode | KeyCode.CtrlMask);
  203. /// <summary>
  204. /// Helper for removing a shift modifier from a <see cref="Key"/>.
  205. /// <code>
  206. /// var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
  207. /// var AltDelete = ControlAltDelete.NoCtrl;
  208. /// </code>
  209. /// </summary>
  210. public Key NoCtrl => new (KeyCode & ~KeyCode.CtrlMask);
  211. /// <summary>
  212. /// Helper for specifying a shifted <see cref="Key"/>.
  213. /// <code>
  214. /// var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
  215. /// </code>
  216. /// </summary>
  217. public Key WithAlt => new (KeyCode | KeyCode.AltMask);
  218. /// <summary>
  219. /// Helper for removing a shift modifier from a <see cref="Key"/>.
  220. /// <code>
  221. /// var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
  222. /// var AltDelete = ControlAltDelete.NoCtrl;
  223. /// </code>
  224. /// </summary>
  225. public Key NoAlt => new (KeyCode & ~KeyCode.AltMask);
  226. #region Operators
  227. /// <summary>
  228. /// Explicitly cast a <see cref="Key"/> to a <see cref="Rune"/>. The conversion is lossy.
  229. /// </summary>
  230. /// <remarks>
  231. /// Uses <see cref="AsRune"/>.
  232. /// </remarks>
  233. /// <param name="kea"></param>
  234. public static explicit operator Rune (Key kea) => kea.AsRune;
  235. /// <summary>
  236. /// Explicitly cast <see cref="Key"/> to a <see langword="char"/>. The conversion is lossy.
  237. /// </summary>
  238. /// <param name="kea"></param>
  239. public static explicit operator char (Key kea) => (char)kea.AsRune.Value;
  240. /// <summary>
  241. /// Explicitly cast <see cref="Key"/> to a <see cref="KeyCode"/>. The conversion is lossy.
  242. /// </summary>
  243. /// <param name="key"></param>
  244. public static explicit operator KeyCode (Key key) => key.KeyCode;
  245. /// <summary>
  246. /// Cast <see cref="KeyCode"/> to a <see cref="Key"/>.
  247. /// </summary>
  248. /// <param name="keyCode"></param>
  249. public static implicit operator Key (KeyCode keyCode) => new (keyCode);
  250. /// <summary>
  251. /// Cast <see langword="char"/> to a <see cref="Key"/>.
  252. /// </summary>
  253. /// <param name="ch"></param>
  254. public static implicit operator Key (char ch) => new ((KeyCode)ch);
  255. /// <inheritdoc/>
  256. public override bool Equals (object obj) => obj is Key k && k.KeyCode == KeyCode;
  257. bool IEquatable<Key>.Equals (Key other) => Equals ((object)other);
  258. /// <inheritdoc/>
  259. public override int GetHashCode () => (int)KeyCode;
  260. /// <summary>
  261. /// </summary>
  262. /// <param name="a"></param>
  263. /// <param name="b"></param>
  264. /// <returns></returns>
  265. public static bool operator == (Key a, Key b) => a?.KeyCode == b?.KeyCode;
  266. /// <summary>
  267. /// </summary>
  268. /// <param name="a"></param>
  269. /// <param name="b"></param>
  270. /// <returns></returns>
  271. public static bool operator != (Key a, Key b) => a?.KeyCode != b?.KeyCode;
  272. /// <summary>
  273. /// Compares two <see cref="Key"/>s for less-than.
  274. /// </summary>
  275. /// <param name="a"></param>
  276. /// <param name="b"></param>
  277. /// <returns></returns>
  278. public static bool operator < (Key a, Key b) => a?.KeyCode < b?.KeyCode;
  279. /// <summary>
  280. /// Compares two <see cref="Key"/>s for greater-than.
  281. /// </summary>
  282. /// <param name="a"></param>
  283. /// <param name="b"></param>
  284. /// <returns></returns>
  285. public static bool operator > (Key a, Key b) => a?.KeyCode > b?.KeyCode;
  286. /// <summary>
  287. /// Compares two <see cref="Key"/>s for greater-than-or-equal-to.
  288. /// </summary>
  289. /// <param name="a"></param>
  290. /// <param name="b"></param>
  291. /// <returns></returns>
  292. public static bool operator <= (Key a, Key b) => a?.KeyCode <= b?.KeyCode;
  293. /// <summary>
  294. /// Compares two <see cref="Key"/>s for greater-than-or-equal-to.
  295. /// </summary>
  296. /// <param name="a"></param>
  297. /// <param name="b"></param>
  298. /// <returns></returns>
  299. public static bool operator >= (Key a, Key b) => a?.KeyCode >= b?.KeyCode;
  300. #endregion Operators
  301. #region String conversion
  302. /// <summary>
  303. /// Pretty prints the KeyEvent
  304. /// </summary>
  305. /// <returns></returns>
  306. public override string ToString () => ToString (KeyCode, (Rune)'+');
  307. static string GetKeyString (KeyCode key)
  308. {
  309. if (key is KeyCode.Null or KeyCode.SpecialMask) {
  310. return string.Empty;
  311. }
  312. // Extract the base key (removing modifier flags)
  313. var baseKey = key & ~KeyCode.CtrlMask & ~KeyCode.AltMask & ~KeyCode.ShiftMask;
  314. if (!key.HasFlag (KeyCode.ShiftMask) && baseKey is >= KeyCode.A and <= KeyCode.Z) {
  315. return ((char)(key + 32)).ToString ();
  316. }
  317. if (key is >= KeyCode.Space and < KeyCode.A) {
  318. return ((char)key).ToString ();
  319. }
  320. string keyName = Enum.GetName (typeof (KeyCode), key);
  321. return !string.IsNullOrEmpty (keyName) ? keyName : ((char)key).ToString ();
  322. }
  323. /// <summary>
  324. /// Formats a <see cref="KeyCode"/> as a string using the default separator of '+'
  325. /// </summary>
  326. /// <param name="key">The key to format.</param>
  327. /// <returns>The formatted string. If the key is a printable character, it will be returned as a string. Otherwise, the key name will be returned.</returns>
  328. public static string ToString (KeyCode key) => ToString (key, (Rune)'+');
  329. /// <summary>
  330. /// Formats a <see cref="KeyCode"/> as a string.
  331. /// </summary>
  332. /// <param name="key">The key to format.</param>
  333. /// <param name="separator">The character to use as a separator between modifier keys and and the key itself.</param>
  334. /// <returns>The formatted string. If the key is a printable character, it will be returned as a string. Otherwise, the key name will be returned.</returns>
  335. public static string ToString (KeyCode key, Rune separator)
  336. {
  337. if (key is KeyCode.Null) {
  338. return string.Empty;
  339. }
  340. var sb = new StringBuilder ();
  341. // Extract the base key (removing modifier flags)
  342. var baseKey = key & ~KeyCode.CtrlMask & ~KeyCode.AltMask & ~KeyCode.ShiftMask;
  343. // Extract and handle modifiers
  344. bool hasModifiers = false;
  345. if ((key & KeyCode.CtrlMask) != 0) {
  346. sb.Append ($"Ctrl{separator}");
  347. hasModifiers = true;
  348. }
  349. if ((key & KeyCode.AltMask) != 0) {
  350. sb.Append ($"Alt{separator}");
  351. hasModifiers = true;
  352. }
  353. if ((key & KeyCode.ShiftMask) != 0 && !GetIsKeyCodeAtoZ (key)) {
  354. sb.Append ($"Shift{separator}");
  355. hasModifiers = true;
  356. }
  357. // Handle special cases and modifiers on their own
  358. if (key != KeyCode.SpecialMask && (baseKey != KeyCode.Null || hasModifiers)) {
  359. if ((key & KeyCode.SpecialMask) != 0 && (baseKey & ~KeyCode.Space) is >= KeyCode.A and <= KeyCode.Z) {
  360. sb.Append (baseKey & ~KeyCode.Space);
  361. } else {
  362. // Append the actual key name
  363. sb.Append (GetKeyString (baseKey));
  364. }
  365. }
  366. string result = sb.ToString ();
  367. result = TrimEndRune (result, separator);
  368. return result;
  369. }
  370. static string TrimEndRune (string input, Rune runeToTrim)
  371. {
  372. // Convert the Rune to a string (which may be one or two chars)
  373. string runeString = runeToTrim.ToString ();
  374. if (input.EndsWith (runeString)) {
  375. // Remove the rune from the end of the string
  376. return input.Substring (0, input.Length - runeString.Length);
  377. }
  378. return input;
  379. }
  380. static readonly Dictionary<string, KeyCode> _modifierDict = new (comparer: StringComparer.InvariantCultureIgnoreCase) {
  381. { "Shift", KeyCode.ShiftMask },
  382. { "Ctrl", KeyCode.CtrlMask },
  383. { "Alt", KeyCode.AltMask }
  384. };
  385. /// <summary>
  386. /// Converts the provided string to a new <see cref="Key"/> instance.
  387. /// </summary>
  388. /// <param name="text">The text to analyze. Formats supported are
  389. /// "Ctrl+X", "Alt+X", "Shift+X", "Ctrl+Alt+X", "Ctrl+Shift+X", "Alt+Shift+X", "Ctrl+Alt+Shift+X", and "X".
  390. /// </param>
  391. /// <param name="key">The parsed value.</param>
  392. /// <returns>A boolean value indicating whether parsing was successful.</returns>
  393. /// <remarks>
  394. /// </remarks>
  395. public static bool TryParse (string text, [NotNullWhen (true)] out Key key)
  396. {
  397. if (string.IsNullOrEmpty (text)) {
  398. key = new Key (KeyCode.Null);
  399. return true;
  400. }
  401. key = null;
  402. // Split the string into parts
  403. string [] parts = text.Split ('+', '-');
  404. if (parts.Length is 0 or > 4 || parts.Any (string.IsNullOrEmpty)) {
  405. return false;
  406. }
  407. // if it's just a shift key
  408. if (parts.Length == 1) {
  409. switch (parts [0]) {
  410. case "Ctrl":
  411. key = new Key (KeyCode.CtrlKey);
  412. return true;
  413. case "Alt":
  414. key = new Key (KeyCode.AltKey);
  415. return true;
  416. case "Shift":
  417. key = new Key (KeyCode.ShiftKey);
  418. return true;
  419. }
  420. }
  421. var modifiers = KeyCode.Null;
  422. for (int index = 0; index < parts.Length; index++) {
  423. if (_modifierDict.TryGetValue (parts [index].ToLowerInvariant (), out var modifier)) {
  424. modifiers |= modifier;
  425. parts [index] = string.Empty; // eat it
  426. }
  427. }
  428. // we now have the modifiers
  429. string partNotFound = parts.FirstOrDefault (p => !string.IsNullOrEmpty (p), string.Empty);
  430. var parsedKeyCode = KeyCode.Null;
  431. int parsedInt = 0;
  432. if (partNotFound.Length == 1) {
  433. var keyCode = (KeyCode)partNotFound [0];
  434. // if it's a single digit int, treat it as such
  435. if (int.TryParse (partNotFound,
  436. System.Globalization.NumberStyles.Integer,
  437. System.Globalization.CultureInfo.InvariantCulture,
  438. out parsedInt)) {
  439. keyCode = (KeyCode)((int)KeyCode.D0 + parsedInt);
  440. } else if (Enum.TryParse (partNotFound, false, out parsedKeyCode)) {
  441. if (parsedKeyCode != KeyCode.Null) {
  442. if (parsedKeyCode is >= KeyCode.A and <= KeyCode.Z && modifiers == 0) {
  443. key = new Key (parsedKeyCode | KeyCode.ShiftMask);
  444. return true;
  445. }
  446. key = new Key ((KeyCode)parsedKeyCode | modifiers);
  447. return true;
  448. }
  449. }
  450. key = new Key (keyCode | modifiers);
  451. return true;
  452. }
  453. if (Enum.TryParse (partNotFound, true, out parsedKeyCode)) {
  454. if (parsedKeyCode != KeyCode.Null) {
  455. if (parsedKeyCode is >= KeyCode.A and <= KeyCode.Z && modifiers == 0) {
  456. key = new Key (parsedKeyCode | KeyCode.ShiftMask);
  457. return true;
  458. }
  459. key = new Key (parsedKeyCode | modifiers);
  460. return true;
  461. }
  462. }
  463. // if it's a number int, treat it as a unicode value
  464. if (int.TryParse (partNotFound,
  465. System.Globalization.NumberStyles.Number,
  466. System.Globalization.CultureInfo.InvariantCulture, out parsedInt)) {
  467. if (!Rune.IsValid (parsedInt)) {
  468. return false;
  469. }
  470. if ((KeyCode)parsedInt is >= KeyCode.A and <= KeyCode.Z && modifiers == 0) {
  471. key = new Key ((KeyCode)parsedInt | KeyCode.ShiftMask);
  472. return true;
  473. }
  474. key = new Key ((KeyCode)parsedInt);
  475. return true;
  476. }
  477. if (!Enum.TryParse (partNotFound, true, out parsedKeyCode)) {
  478. return false;
  479. }
  480. if (GetIsKeyCodeAtoZ (parsedKeyCode)) {
  481. key = new Key (parsedKeyCode | modifiers & ~KeyCode.Space);
  482. return true;
  483. }
  484. return false;
  485. }
  486. #endregion
  487. #region Standard Key Definitions
  488. /// <summary>
  489. /// An uninitialized The <see cref="Key"/> object.
  490. /// </summary>
  491. public new static readonly Key Empty = new ();
  492. /// <summary>
  493. /// The <see cref="Key"/> object for the Backspace key.
  494. /// </summary>
  495. public static readonly Key Backspace = new (KeyCode.Backspace);
  496. /// <summary>
  497. /// The <see cref="Key"/> object for the tab key (forwards tab key).
  498. /// </summary>
  499. public static readonly Key Tab = new (KeyCode.Tab);
  500. /// <summary>
  501. /// The <see cref="Key"/> object for the return key.
  502. /// </summary>
  503. public static readonly Key Enter = new (KeyCode.Enter);
  504. /// <summary>
  505. /// The <see cref="Key"/> object for the clear key.
  506. /// </summary>
  507. public static readonly Key Clear = new (KeyCode.Clear);
  508. /// <summary>
  509. /// The <see cref="Key"/> object for the Shift key.
  510. /// </summary>
  511. public static readonly Key Shift = new (KeyCode.ShiftKey);
  512. /// <summary>
  513. /// The <see cref="Key"/> object for the Ctrl key.
  514. /// </summary>
  515. public static readonly Key Ctrl = new (KeyCode.CtrlKey);
  516. /// <summary>
  517. /// The <see cref="Key"/> object for the Alt key.
  518. /// </summary>
  519. public static readonly Key Alt = new (KeyCode.AltKey);
  520. /// <summary>
  521. /// The <see cref="Key"/> object for the CapsLock key.
  522. /// </summary>
  523. public static readonly Key CapsLock = new (KeyCode.CapsLock);
  524. /// <summary>
  525. /// The <see cref="Key"/> object for the Escape key.
  526. /// </summary>
  527. public static readonly Key Esc = new (KeyCode.Esc);
  528. /// <summary>
  529. /// The <see cref="Key"/> object for the Space bar key.
  530. /// </summary>
  531. public static readonly Key Space = new (KeyCode.Space);
  532. /// <summary>
  533. /// The <see cref="Key"/> object for 0 key.
  534. /// </summary>
  535. public static readonly Key D0 = new (KeyCode.D0);
  536. /// <summary>
  537. /// The <see cref="Key"/> object for 1 key.
  538. /// </summary>
  539. public static readonly Key D1 = new (KeyCode.D1);
  540. /// <summary>
  541. /// The <see cref="Key"/> object for 2 key.
  542. /// </summary>
  543. public static readonly Key D2 = new (KeyCode.D2);
  544. /// <summary>
  545. /// The <see cref="Key"/> object for 3 key.
  546. /// </summary>
  547. public static readonly Key D3 = new (KeyCode.D3);
  548. /// <summary>
  549. /// The <see cref="Key"/> object for 4 key.
  550. /// </summary>
  551. public static readonly Key D4 = new (KeyCode.D4);
  552. /// <summary>
  553. /// The <see cref="Key"/> object for 5 key.
  554. /// </summary>
  555. public static readonly Key D5 = new (KeyCode.D5);
  556. /// <summary>
  557. /// The <see cref="Key"/> object for 6 key.
  558. /// </summary>
  559. public static readonly Key D6 = new (KeyCode.D6);
  560. /// <summary>
  561. /// The <see cref="Key"/> object for 7 key.
  562. /// </summary>
  563. public static readonly Key D7 = new (KeyCode.D7);
  564. /// <summary>
  565. /// The <see cref="Key"/> object for 8 key.
  566. /// </summary>
  567. public static readonly Key D8 = new (KeyCode.D8);
  568. /// <summary>
  569. /// The <see cref="Key"/> object for 9 key.
  570. /// </summary>
  571. public static readonly Key D9 = new (KeyCode.D9);
  572. /// <summary>
  573. /// The <see cref="Key"/> object for the A key (un-shifted). Use <c>Key.A.WithShift</c> for uppercase 'A'.
  574. /// </summary>
  575. public static readonly Key A = new (KeyCode.A);
  576. /// <summary>
  577. /// The <see cref="Key"/> object for the B key (un-shifted). Use <c>Key.B.WithShift</c> for uppercase 'B'.
  578. /// </summary>
  579. public static readonly Key B = new (KeyCode.B);
  580. /// <summary>
  581. /// The <see cref="Key"/> object for the C key (un-shifted). Use <c>Key.C.WithShift</c> for uppercase 'C'.
  582. /// </summary>
  583. public static readonly Key C = new (KeyCode.C);
  584. /// <summary>
  585. /// The <see cref="Key"/> object for the D key (un-shifted). Use <c>Key.D.WithShift</c> for uppercase 'D'.
  586. /// </summary>
  587. public static readonly Key D = new (KeyCode.D);
  588. /// <summary>
  589. /// The <see cref="Key"/> object for the E key (un-shifted). Use <c>Key.E.WithShift</c> for uppercase 'E'.
  590. /// </summary>
  591. public static readonly Key E = new (KeyCode.E);
  592. /// <summary>
  593. /// The <see cref="Key"/> object for the F key (un-shifted). Use <c>Key.F.WithShift</c> for uppercase 'F'.
  594. /// </summary>
  595. public static readonly Key F = new (KeyCode.F);
  596. /// <summary>
  597. /// The <see cref="Key"/> object for the G key (un-shifted). Use <c>Key.G.WithShift</c> for uppercase 'G'.
  598. /// </summary>
  599. public static readonly Key G = new (KeyCode.G);
  600. /// <summary>
  601. /// The <see cref="Key"/> object for the H key (un-shifted). Use <c>Key.H.WithShift</c> for uppercase 'H'.
  602. /// </summary>
  603. public static readonly Key H = new (KeyCode.H);
  604. /// <summary>
  605. /// The <see cref="Key"/> object for the I key (un-shifted). Use <c>Key.I.WithShift</c> for uppercase 'I'.
  606. /// </summary>
  607. public static readonly Key I = new (KeyCode.I);
  608. /// <summary>
  609. /// The <see cref="Key"/> object for the J key (un-shifted). Use <c>Key.J.WithShift</c> for uppercase 'J'.
  610. /// </summary>
  611. public static readonly Key J = new (KeyCode.J);
  612. /// <summary>
  613. /// The <see cref="Key"/> object for the K key (un-shifted). Use <c>Key.K.WithShift</c> for uppercase 'K'.
  614. /// </summary>
  615. public static readonly Key K = new (KeyCode.K);
  616. /// <summary>
  617. /// The <see cref="Key"/> object for the L key (un-shifted). Use <c>Key.L.WithShift</c> for uppercase 'L'.
  618. /// </summary>
  619. public static readonly Key L = new (KeyCode.L);
  620. /// <summary>
  621. /// The <see cref="Key"/> object for the M key (un-shifted). Use <c>Key.M.WithShift</c> for uppercase 'M'.
  622. /// </summary>
  623. public static readonly Key M = new (KeyCode.M);
  624. /// <summary>
  625. /// The <see cref="Key"/> object for the N key (un-shifted). Use <c>Key.N.WithShift</c> for uppercase 'N'.
  626. /// </summary>
  627. public static readonly Key N = new (KeyCode.N);
  628. /// <summary>
  629. /// The <see cref="Key"/> object for the O key (un-shifted). Use <c>Key.O.WithShift</c> for uppercase 'O'.
  630. /// </summary>
  631. public static readonly Key O = new (KeyCode.O);
  632. /// <summary>
  633. /// The <see cref="Key"/> object for the P key (un-shifted). Use <c>Key.P.WithShift</c> for uppercase 'P'.
  634. /// </summary>
  635. public static readonly Key P = new (KeyCode.P);
  636. /// <summary>
  637. /// The <see cref="Key"/> object for the Q key (un-shifted). Use <c>Key.Q.WithShift</c> for uppercase 'Q'.
  638. /// </summary>
  639. public static readonly Key Q = new (KeyCode.Q);
  640. /// <summary>
  641. /// The <see cref="Key"/> object for the R key (un-shifted). Use <c>Key.R.WithShift</c> for uppercase 'R'.
  642. /// </summary>
  643. public static readonly Key R = new (KeyCode.R);
  644. /// <summary>
  645. /// The <see cref="Key"/> object for the S key (un-shifted). Use <c>Key.S.WithShift</c> for uppercase 'S'.
  646. /// </summary>
  647. public static readonly Key S = new (KeyCode.S);
  648. /// <summary>
  649. /// The <see cref="Key"/> object for the T key (un-shifted). Use <c>Key.T.WithShift</c> for uppercase 'T'.
  650. /// </summary>
  651. public static readonly Key T = new (KeyCode.T);
  652. /// <summary>
  653. /// The <see cref="Key"/> object for the U key (un-shifted). Use <c>Key.U.WithShift</c> for uppercase 'U'.
  654. /// </summary>
  655. public static readonly Key U = new (KeyCode.U);
  656. /// <summary>
  657. /// The <see cref="Key"/> object for the V key (un-shifted). Use <c>Key.V.WithShift</c> for uppercase 'V'.
  658. /// </summary>
  659. public static readonly Key V = new (KeyCode.V);
  660. /// <summary>
  661. /// The <see cref="Key"/> object for the W key (un-shifted). Use <c>Key.W.WithShift</c> for uppercase 'W'.
  662. /// </summary>
  663. public static readonly Key W = new (KeyCode.W);
  664. /// <summary>
  665. /// The <see cref="Key"/> object for the X key (un-shifted). Use <c>Key.X.WithShift</c> for uppercase 'X'.
  666. /// </summary>
  667. public static readonly Key X = new (KeyCode.X);
  668. /// <summary>
  669. /// The <see cref="Key"/> object for the Y key (un-shifted). Use <c>Key.Y.WithShift</c> for uppercase 'Y'.
  670. /// </summary>
  671. public static readonly Key Y = new (KeyCode.Y);
  672. /// <summary>
  673. /// The <see cref="Key"/> object for the Z key (un-shifted). Use <c>Key.Z.WithShift</c> for uppercase 'Z'.
  674. /// </summary>
  675. public static readonly Key Z = new (KeyCode.Z);
  676. /// <summary>
  677. /// The <see cref="Key"/> object for the Delete key.
  678. /// </summary>
  679. public static readonly Key Delete = new (KeyCode.Delete);
  680. /// <summary>
  681. /// The <see cref="Key"/> object for the Cursor up key.
  682. /// </summary>
  683. public static readonly Key CursorUp = new (KeyCode.CursorUp);
  684. /// <summary>
  685. /// The <see cref="Key"/> object for Cursor down key.
  686. /// </summary>
  687. public static readonly Key CursorDown = new (KeyCode.CursorDown);
  688. /// <summary>
  689. /// The <see cref="Key"/> object for Cursor left key.
  690. /// </summary>
  691. public static readonly Key CursorLeft = new (KeyCode.CursorLeft);
  692. /// <summary>
  693. /// The <see cref="Key"/> object for Cursor right key.
  694. /// </summary>
  695. public static readonly Key CursorRight = new (KeyCode.CursorRight);
  696. /// <summary>
  697. /// The <see cref="Key"/> object for Page Up key.
  698. /// </summary>
  699. public static readonly Key PageUp = new (KeyCode.PageUp);
  700. /// <summary>
  701. /// The <see cref="Key"/> object for Page Down key.
  702. /// </summary>
  703. public static readonly Key PageDown = new (KeyCode.PageDown);
  704. /// <summary>
  705. /// The <see cref="Key"/> object for Home key.
  706. /// </summary>
  707. public static readonly Key Home = new (KeyCode.Home);
  708. /// <summary>
  709. /// The <see cref="Key"/> object for End key.
  710. /// </summary>
  711. public static readonly Key End = new (KeyCode.End);
  712. /// <summary>
  713. /// The <see cref="Key"/> object for Insert Character key.
  714. /// </summary>
  715. public static readonly Key InsertChar = new (KeyCode.InsertChar);
  716. /// <summary>
  717. /// The <see cref="Key"/> object for Delete Character key.
  718. /// </summary>
  719. public static readonly Key DeleteChar = new (KeyCode.DeleteChar);
  720. /// <summary>
  721. /// The <see cref="Key"/> object for Print Screen key.
  722. /// </summary>
  723. public static readonly Key PrintScreen = new (KeyCode.PrintScreen);
  724. /// <summary>
  725. /// The <see cref="Key"/> object for F1 key.
  726. /// </summary>
  727. public static readonly Key F1 = new (KeyCode.F1);
  728. /// <summary>
  729. /// The <see cref="Key"/> object for F2 key.
  730. /// </summary>
  731. public static readonly Key F2 = new (KeyCode.F2);
  732. /// <summary>
  733. /// The <see cref="Key"/> object for F3 key.
  734. /// </summary>
  735. public static readonly Key F3 = new (KeyCode.F3);
  736. /// <summary>
  737. /// The <see cref="Key"/> object for F4 key.
  738. /// </summary>
  739. public static readonly Key F4 = new (KeyCode.F4);
  740. /// <summary>
  741. /// The <see cref="Key"/> object for F5 key.
  742. /// </summary>
  743. public static readonly Key F5 = new (KeyCode.F5);
  744. /// <summary>
  745. /// The <see cref="Key"/> object for F6 key.
  746. /// </summary>
  747. public static readonly Key F6 = new (KeyCode.F6);
  748. /// <summary>
  749. /// The <see cref="Key"/> object for F7 key.
  750. /// </summary>
  751. public static readonly Key F7 = new (KeyCode.F7);
  752. /// <summary>
  753. /// The <see cref="Key"/> object for F8 key.
  754. /// </summary>
  755. public static readonly Key F8 = new (KeyCode.F8);
  756. /// <summary>
  757. /// The <see cref="Key"/> object for F9 key.
  758. /// </summary>
  759. public static readonly Key F9 = new (KeyCode.F9);
  760. /// <summary>
  761. /// The <see cref="Key"/> object for F10 key.
  762. /// </summary>
  763. public static readonly Key F10 = new (KeyCode.F10);
  764. /// <summary>
  765. /// The <see cref="Key"/> object for F11 key.
  766. /// </summary>
  767. public static readonly Key F11 = new (KeyCode.F11);
  768. /// <summary>
  769. /// The <see cref="Key"/> object for F12 key.
  770. /// </summary>
  771. public static readonly Key F12 = new (KeyCode.F12);
  772. /// <summary>
  773. /// The <see cref="Key"/> object for F13 key.
  774. /// </summary>
  775. public static readonly Key F13 = new (KeyCode.F13);
  776. /// <summary>
  777. /// The <see cref="Key"/> object for F14 key.
  778. /// </summary>
  779. public static readonly Key F14 = new (KeyCode.F14);
  780. /// <summary>
  781. /// The <see cref="Key"/> object for F15 key.
  782. /// </summary>
  783. public static readonly Key F15 = new (KeyCode.F15);
  784. /// <summary>
  785. /// The <see cref="Key"/> object for F16 key.
  786. /// </summary>
  787. public static readonly Key F16 = new (KeyCode.F16);
  788. /// <summary>
  789. /// The <see cref="Key"/> object for F17 key.
  790. /// </summary>
  791. public static readonly Key F17 = new (KeyCode.F17);
  792. /// <summary>
  793. /// The <see cref="Key"/> object for F18 key.
  794. /// </summary>
  795. public static readonly Key F18 = new (KeyCode.F18);
  796. /// <summary>
  797. /// The <see cref="Key"/> object for F19 key.
  798. /// </summary>
  799. public static readonly Key F19 = new (KeyCode.F19);
  800. /// <summary>
  801. /// The <see cref="Key"/> object for F20 key.
  802. /// </summary>
  803. public static readonly Key F20 = new (KeyCode.F20);
  804. /// <summary>
  805. /// The <see cref="Key"/> object for F21 key.
  806. /// </summary>
  807. public static readonly Key F21 = new (KeyCode.F21);
  808. /// <summary>
  809. /// The <see cref="Key"/> object for F22 key.
  810. /// </summary>
  811. public static readonly Key F22 = new (KeyCode.F22);
  812. /// <summary>
  813. /// The <see cref="Key"/> object for F23 key.
  814. /// </summary>
  815. public static readonly Key F23 = new (KeyCode.F23);
  816. /// <summary>
  817. /// The <see cref="Key"/> object for F24 key.
  818. /// </summary>
  819. public static readonly Key F24 = new (KeyCode.F24);
  820. #endregion
  821. }