2
0

Key.cs 43 KB

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