using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework.Input; 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 = {ModifierKey.None, ModifierKey.Shift, ModifierKey.Control, ModifierKey.Alt}; private static readonly Dictionary KeysLookup = new Dictionary { {ModifierKey.Shift, new[] {Keys.LeftShift, Keys.RightShift}}, {ModifierKey.Control, new[] {Keys.LeftControl, Keys.RightControl}}, {ModifierKey.Alt, new[] {Keys.LeftAlt, Keys.RightAlt}} }; private static readonly Dictionary ModifiersLookup = KeysExtensions.KeysLookup .SelectMany(kv => kv.Value.Select(v => (kv.Key, v))) .ToDictionary(kv => kv.Item2, kv => kv.Item1); /// /// 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) { return KeysExtensions.KeysLookup.TryGetValue(modifier, out var keys) ? keys : Enumerable.Empty(); } /// /// 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) { return KeysExtensions.ModifiersLookup.TryGetValue(key, out var mod) ? mod : ModifierKey.None; } /// public static ModifierKey GetModifier(this GenericInput input) { return input.Type == GenericInput.InputType.Keyboard ? ((Keys) input).GetModifier() : 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 key.GetModifier() != ModifierKey.None; } /// public static bool IsModifier(this GenericInput input) { return input.GetModifier() != 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 } }