using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework.Input; using MLEM.Misc; namespace MLEM.Input { /// /// A set of extension methods for dealing with and /// public static class KeysExtensions { /// /// All enum values of /// public static readonly ModifierKey[] ModifierKeys = EnumHelper.GetValues().ToArray(); /// /// Returns all of the keys that the given modifier key represents /// /// The modifier key /// All of the keys the modifier key represents public static IEnumerable GetKeys(this ModifierKey modifier) { switch (modifier) { case ModifierKey.Shift: yield return Keys.LeftShift; yield return Keys.RightShift; break; case ModifierKey.Control: yield return Keys.LeftControl; yield return Keys.RightControl; break; case ModifierKey.Alt: yield return Keys.LeftAlt; yield return Keys.RightAlt; break; } } /// /// Returns the modifier key that the given key represents. /// If there is no matching modifier key, is returned. /// /// The key to convert to a modifier key /// The modifier key, or public static ModifierKey GetModifier(this Keys key) { foreach (var mod in ModifierKeys) { if (GetKeys(mod).Contains(key)) return mod; } return ModifierKey.None; } /// public static ModifierKey GetModifier(this GenericInput input) { return input.Type == GenericInput.InputType.Keyboard ? GetModifier((Keys) input) : ModifierKey.None; } /// /// Returns whether the given key is a modifier key or not. /// /// The key /// If the key is a modifier key public static bool IsModifier(this Keys key) { return GetModifier(key) != ModifierKey.None; } /// public static bool IsModifier(this GenericInput input) { return GetModifier(input) != ModifierKey.None; } } /// /// An enum representing modifier keys. /// A modifier key is a key that is usually pressed as part of key combination to change the function of a regular key. /// public enum ModifierKey { /// /// No modifier key. Only used for /// None, /// /// The shift modifier key. This represents Left Shift and Right Shift keys. /// Shift, /// /// The control modifier key. This represents Left Control and Right Control. /// Control, /// /// The alt modifier key. This represents Alt and Alt Graph. /// Alt } }