Source

Conjurer / Conjurer / Input / KeyboardManager.cs

Full commit
// -----------------------------------------------------------------------
// <copyright file="KeyboardManager.cs" company="Petr Abdulin">
// Conjurer is copyright 2011-2012 Petr Abdulin.
// Conjurer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License version 3
// as published by the Free Software Foundation.
// See the GNU Lesser General Public License for more
// details: http://www.gnu.org/licenses/lgpl.html
// </copyright>
// -----------------------------------------------------------------------

namespace Conjurer.Input
{
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework.Input;

    /// <summary>
    /// Keyboard processing helper class.
    /// </summary>
    public static class KeyboardManager
    {
        /// <summary>
        /// Previous state of the keyboard.
        /// </summary>
        private static KeyboardState previousKeyboardState;

        /// <summary>
        /// Current state of the keyboard.
        /// </summary>
        private static KeyboardState currentKeyboardState;

        /// <summary>
        /// Updates keybord states. 
        /// This method must be called in your main Game class' Update method
        /// in order to KeyboardManager to function properly.
        /// </summary>
        public static void Update()
        {
            // save previous
            previousKeyboardState = currentKeyboardState;

            // get current
            currentKeyboardState = Keyboard.GetState();
        }

        /// <summary>
        /// Determines whether the specified key is pressed.
        /// </summary>
        /// <param name="key">The key to check.</param>
        /// <returns>
        /// <c>true</c> the specified key is down; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsKeyDown(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key);
        }

        /// <summary>
        /// Determines whether the specified key is not pressed.
        /// </summary>
        /// <param name="key">The key to check.</param>
        /// <returns>
        /// <c>true</c> the specified key is up; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsKeyUp(Keys key)
        {
            return currentKeyboardState.IsKeyUp(key);
        }

        /// <summary>
        /// Determines whether given key is just pressed.
        /// </summary>
        /// <param name="key">The key to check.</param>
        /// <returns>
        /// <c>true</c> if key is just pressed; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsKeyJustPressed(Keys key)
        {
            // if now it is down..
            if (currentKeyboardState.IsKeyDown(key))
            {
                // but wasn't before
                if (!previousKeyboardState.IsKeyDown(key))
                {
                    // then it's just pressed
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Determines whether given key is just released.
        /// </summary>
        /// <param name="key">The key to check.</param>
        /// <returns>
        /// <c>true</c> if key is just released; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsKeyJustReleased(Keys key)
        {
            // if now it is up..
            if (currentKeyboardState.IsKeyUp(key))
            {
                // but wasn't before
                if (!previousKeyboardState.IsKeyUp(key))
                {
                    // then it's just released
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Determines whether all keys from given keys combination are just pressed.
        /// </summary>
        /// <param name="keysCombination">The keys to check.</param>
        /// <returns>
        /// <c>true</c> if all keys are just pressed; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsKeysCombinationJustPressed(IEnumerable<Keys> keysCombination)
        {
            // if all keys are down
            if (keysCombination.All(currentKeyboardState.IsKeyDown))
            {
                // but wasn't before
                if (!keysCombination.All(previousKeyboardState.IsKeyDown))
                {
                    // then combination is just pressed (all of the combination keys)
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Determines whether any key from given keys combination is just released.
        /// </summary>
        /// <param name="keysCombination">The keys combination to check.</param>
        /// <returns>
        /// <c>true</c> if any key is just released; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsKeysCombinationJustReleased(IEnumerable<Keys> keysCombination)
        {
            // if any key from combination is up
            if (keysCombination.Any(currentKeyboardState.IsKeyUp))
            {
                // but wasn't before
                if (!keysCombination.Any(previousKeyboardState.IsKeyUp))
                {
                    // then it's just released (any of the combination keys)
                    return true;
                }
            }

            return false;
        }
    }
}