KeysConverter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.Text;
  10. namespace System.Windows.Forms
  11. {
  12. /// <summary>
  13. /// Provides a type converter to convert <see cref="Keys"/> objects to and from various
  14. /// other representations.
  15. /// </summary>
  16. public class KeysConverter : TypeConverter, IComparer
  17. {
  18. private IDictionary<string, Keys> _keyNames;
  19. private List<string> _displayOrder;
  20. private StandardValuesCollection _values;
  21. private void Initialize()
  22. {
  23. _keyNames = new Dictionary<string, Keys>(34);
  24. _displayOrder = new List<string>(34);
  25. AddKey("Enter", Keys.Return);
  26. AddKey("F12", Keys.F12);
  27. AddKey("F11", Keys.F11);
  28. AddKey("F10", Keys.F10);
  29. AddKey("End", Keys.End);
  30. AddKey("Ctrl", Keys.Control);
  31. AddKey("F8", Keys.F8);
  32. AddKey("F9", Keys.F9);
  33. AddKey("Alt", Keys.Alt);
  34. AddKey("F4", Keys.F4);
  35. AddKey("F5", Keys.F5);
  36. AddKey("F6", Keys.F6);
  37. AddKey("F7", Keys.F7);
  38. AddKey("F1", Keys.F1);
  39. AddKey("F2", Keys.F2);
  40. AddKey("F3", Keys.F3);
  41. AddKey("PageDown", Keys.Next);
  42. AddKey("Insert", Keys.Insert);
  43. AddKey("Home", Keys.Home);
  44. AddKey("Del", Keys.Delete);
  45. AddKey("Shift", Keys.Shift);
  46. AddKey("PageUp", Keys.Prior);
  47. AddKey("Back", Keys.Back);
  48. //new whidbey keys follow here...
  49. // Add string mappings for these values (numbers 0-9) so that the keyboard shortcuts
  50. // will be displayed properly in menus.
  51. AddKey("0", Keys.D0);
  52. AddKey("1", Keys.D1);
  53. AddKey("2", Keys.D2);
  54. AddKey("3", Keys.D3);
  55. AddKey("4", Keys.D4);
  56. AddKey("5", Keys.D5);
  57. AddKey("6", Keys.D6);
  58. AddKey("7", Keys.D7);
  59. AddKey("8", Keys.D8);
  60. AddKey("9", Keys.D9);
  61. void AddKey(string key, Keys value)
  62. {
  63. _keyNames[key] = value;
  64. _displayOrder.Add(key);
  65. }
  66. }
  67. /// <summary>
  68. /// Access to a lookup table of name/value pairs for keys. These are localized
  69. /// names.
  70. /// </summary>
  71. private IDictionary<string, Keys> KeyNames
  72. {
  73. get
  74. {
  75. if (_keyNames is null)
  76. {
  77. Debug.Assert(_displayOrder is null);
  78. Initialize();
  79. }
  80. return _keyNames;
  81. }
  82. }
  83. private List<string> DisplayOrder
  84. {
  85. get
  86. {
  87. if (_displayOrder is null)
  88. {
  89. Debug.Assert(_keyNames is null);
  90. Initialize();
  91. }
  92. return _displayOrder;
  93. }
  94. }
  95. /// <summary>
  96. /// Determines if this converter can convert an object in the given source
  97. /// type to the native type of the converter.
  98. /// </summary>
  99. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  100. {
  101. if (sourceType == typeof(string) || sourceType == typeof(Enum[]))
  102. {
  103. return true;
  104. }
  105. return base.CanConvertFrom(context, sourceType);
  106. }
  107. /// <summary>
  108. /// Gets a value indicating whether this converter can
  109. /// convert an object to the given destination type using the context.
  110. /// </summary>
  111. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  112. {
  113. if (destinationType == typeof(Enum[]))
  114. {
  115. return true;
  116. }
  117. return base.CanConvertTo(context, destinationType);
  118. }
  119. /// <summary>
  120. /// Compares two key values for equivalence.
  121. /// </summary>
  122. public int Compare(object a, object b)
  123. {
  124. return string.Compare(ConvertToString(a), ConvertToString(b), false, CultureInfo.InvariantCulture);
  125. }
  126. /// <summary>
  127. /// Converts the given object to the converter's native type.
  128. /// </summary>
  129. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  130. {
  131. if (value is string valueAsString)
  132. {
  133. string text = valueAsString.Trim();
  134. if (text.Length == 0)
  135. {
  136. return null;
  137. }
  138. else
  139. {
  140. // Parse an array of key tokens.
  141. string[] tokens = text.Split(new char[] { '+' });
  142. for (int i = 0; i < tokens.Length; i++)
  143. {
  144. tokens[i] = tokens[i].Trim();
  145. }
  146. // Now lookup each key token in our key hashtable.
  147. Keys key = (Keys)0;
  148. bool foundKeyCode = false;
  149. for (int i = 0; i < tokens.Length; i++)
  150. {
  151. if (!KeyNames.TryGetValue(tokens[i], out Keys currentKey))
  152. {
  153. // Key was not found in our dictionary. See if it is a valid value in
  154. // the Keys enum.
  155. currentKey = (Keys)Enum.Parse(typeof(Keys), tokens[i]);
  156. }
  157. if ((currentKey & Keys.KeyCode) != 0)
  158. {
  159. // We found a match. If we have previously found a
  160. // key code, then check to see that this guy
  161. // isn't a key code (it is illegal to have, say,
  162. // "A + B"
  163. if (foundKeyCode)
  164. {
  165. throw new FormatException("InvalidKeyCombination");
  166. }
  167. foundKeyCode = true;
  168. }
  169. // Now OR the key into our current key
  170. key |= currentKey;
  171. }
  172. return (object)key;
  173. }
  174. }
  175. else if (value is Enum[] valueAsEnumArray)
  176. {
  177. long finalValue = 0;
  178. foreach (Enum e in valueAsEnumArray)
  179. {
  180. finalValue |= Convert.ToInt64(e, CultureInfo.InvariantCulture);
  181. }
  182. return Enum.ToObject(typeof(Keys), finalValue);
  183. }
  184. return base.ConvertFrom(context, culture, value);
  185. }
  186. /// <summary>
  187. /// Converts the given object to another type. The most common types to convert
  188. /// are to and from a string object. The default implementation will make a call
  189. /// to ToString on the object if the object is valid and if the destination
  190. /// type is string. If this cannot convert to the destination type, this will
  191. /// throw a NotSupportedException.
  192. /// </summary>
  193. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  194. {
  195. if (destinationType == null)
  196. throw new ArgumentNullException();
  197. if (value is Keys || value is int)
  198. {
  199. bool asString = destinationType == typeof(string);
  200. bool asEnum = false;
  201. if (!asString)
  202. {
  203. asEnum = destinationType == typeof(Enum[]);
  204. }
  205. if (asString || asEnum)
  206. {
  207. Keys key = (Keys)value;
  208. bool added = false;
  209. ArrayList terms = new ArrayList();
  210. Keys modifiers = (key & Keys.Modifiers);
  211. // First, iterate through and do the modifiers. These are
  212. // additive, so we support things like Ctrl + Alt
  213. for (int i = 0; i < DisplayOrder.Count; i++)
  214. {
  215. string keyString = DisplayOrder[i];
  216. Keys keyValue = _keyNames[keyString];
  217. if (((int)keyValue & (int)modifiers) != 0)
  218. {
  219. if (asString)
  220. {
  221. if (added)
  222. {
  223. terms.Add("+");
  224. }
  225. terms.Add(keyString);
  226. }
  227. else
  228. {
  229. terms.Add(keyValue);
  230. }
  231. added = true;
  232. }
  233. }
  234. // Now reset and do the key values. Here, we quit if
  235. // we find a match.
  236. Keys keyOnly = key & Keys.KeyCode;
  237. bool foundKey = false;
  238. if (added && asString)
  239. {
  240. terms.Add("+");
  241. }
  242. for (int i = 0; i < DisplayOrder.Count; i++)
  243. {
  244. string keyString = DisplayOrder[i];
  245. Keys keyValue = _keyNames[keyString];
  246. if (keyValue.Equals(keyOnly))
  247. {
  248. if (asString)
  249. {
  250. terms.Add(keyString);
  251. }
  252. else
  253. {
  254. terms.Add(keyValue);
  255. }
  256. added = true;
  257. foundKey = true;
  258. break;
  259. }
  260. }
  261. // Finally, if the key wasn't in our list, add it to
  262. // the end anyway. Here we just pull the key value out
  263. // of the enum.
  264. if (!foundKey && Enum.IsDefined(typeof(Keys), (int)keyOnly))
  265. {
  266. if (asString)
  267. {
  268. terms.Add(((Enum)keyOnly).ToString());
  269. }
  270. else
  271. {
  272. terms.Add((Enum)keyOnly);
  273. }
  274. }
  275. if (asString)
  276. {
  277. StringBuilder b = new StringBuilder(32);
  278. foreach (string t in terms)
  279. {
  280. b.Append(t);
  281. }
  282. return b.ToString();
  283. }
  284. else
  285. {
  286. return (Enum[])terms.ToArray(typeof(Enum));
  287. }
  288. }
  289. }
  290. return base.ConvertTo(context, culture, value, destinationType);
  291. }
  292. /// <summary>
  293. /// Retrieves a collection containing a set of standard values
  294. /// for the data type this validator is designed for. This
  295. /// will return null if the data type does not support a
  296. /// standard set of values.
  297. /// </summary>
  298. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  299. {
  300. if (_values is null)
  301. {
  302. ArrayList list = new ArrayList();
  303. ICollection<Keys> keys = KeyNames.Values;
  304. foreach (object o in keys)
  305. {
  306. list.Add(o);
  307. }
  308. list.Sort(this);
  309. _values = new StandardValuesCollection(list.ToArray());
  310. }
  311. return _values;
  312. }
  313. /// <summary>
  314. /// Determines if the list of standard values returned from
  315. /// GetStandardValues is an exclusive list. If the list
  316. /// is exclusive, then no other values are valid, such as
  317. /// in an enum data type. If the list is not exclusive,
  318. /// then there are other valid values besides the list of
  319. /// standard values GetStandardValues provides.
  320. /// </summary>
  321. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  322. {
  323. return false;
  324. }
  325. /// <summary>
  326. /// Determines if this object supports a standard set of values
  327. /// that can be picked from a list.
  328. /// </summary>
  329. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  330. {
  331. return true;
  332. }
  333. }
  334. }