// Copyright (c) Craftwork Games. All rights reserved. // Licensed under the MIT license. // See LICENSE file in the project root for full license information. using System; using Microsoft.Xna.Framework.Input; namespace MonoGame.Extended.Input { /// /// Represents the state of keyboard input /// /// /// This is an extended version of the base struct /// that provides utility for checking the state differences between the previous and current state. /// public struct KeyboardStateExtended { private KeyboardState _currentKeyboardState; private KeyboardState _previousKeyboardState; /// /// Initializes a new instance of the value. /// /// The state of keyboard input during the current update cycle. /// The state of keyboard input during the previous update cycle. public KeyboardStateExtended(KeyboardState currentKeyboardState, KeyboardState previousKeyboardState) { _currentKeyboardState = currentKeyboardState; _previousKeyboardState = previousKeyboardState; } /// /// Gets a value that indicates whether the caps lock key is down during the current state. /// public bool CapsLock { #if FNA get { return _currentKeyboardState.IsKeyDown(Keys.CapsLock); } #else get { return _currentKeyboardState.CapsLock; } #endif } /// /// Gets a value that indicates whether the num lock key is down during the current state. /// public bool NumLock { #if FNA get { return _currentKeyboardState.IsKeyDown(Keys.NumLock); } #else get { return _currentKeyboardState.NumLock; } #endif } /// /// Returns a value that indicates whether either the left or right shift key is down during the current state. /// /// /// if either the left or right shift key is down during the current state; otherwise, /// . /// public bool IsShiftDown() => _currentKeyboardState.IsKeyDown(Keys.LeftShift) || _currentKeyboardState.IsKeyDown(Keys.RightShift); /// /// Returns a value that indicates whether either the left or right control key is down during the current /// state. /// /// /// if either the left or right control key is down during the current state; otherwise, /// . /// public bool IsControlDown() => _currentKeyboardState.IsKeyDown(Keys.LeftControl) || _currentKeyboardState.IsKeyDown(Keys.RightControl); /// /// Returns a value that indicates whether either the left or righ talt key is currently pressed down. /// /// public bool IsAltDown() => _currentKeyboardState.IsKeyDown(Keys.LeftAlt) || _currentKeyboardState.IsKeyDown(Keys.RightAlt); /// /// Returns a value that indicates if the specified key is down during the current state. /// /// The key to check. /// /// if the key is down during the current state; otherwise, . /// public bool IsKeyDown(Keys key) => _currentKeyboardState.IsKeyDown(key); /// /// Returns a value that indicates if the specified key is up during the current state. /// /// The key to check. /// /// if the key is up during the current state; otherwise, . /// public bool IsKeyUp(Keys key) => _currentKeyboardState.IsKeyUp(key); /// /// Returns the total number of keys down during the current state. /// public int GetPressedKeyCount => _currentKeyboardState.GetPressedKeyCount(); /// /// Returns an array of all keys that are down during the current state. /// /// /// An array of values that represent each key that is down during the current state. /// public Keys[] GetPressedKeys() => _currentKeyboardState.GetPressedKeys(); /// /// Fills an existing array with the keys pressed during the current state. /// /// An existing array to fill with the pressed keys. /// /// Thrown if the parameter is . /// /// /// Thrown if the array provided by the parameter is not large enough to fit all /// pressed keys. Use to determine the total number of elements. /// public void GetPressedKeys(Keys[] keys) => _currentKeyboardState.GetPressedKeys(keys); /// /// Returns whether the given key was down during previous state, but is now up. /// /// The key to check. /// /// if the key was released this state-change, otherwise . /// public readonly bool WasKeyReleased(Keys key) => _previousKeyboardState.IsKeyDown(key) && _currentKeyboardState.IsKeyUp(key); /// /// Returns whether the given key was up during previous state, but is now down. /// /// The key to check. /// /// if the key was pressed this state-change, otherwise . /// public readonly bool WasKeyPressed(Keys key) => _previousKeyboardState.IsKeyUp(key) && _currentKeyboardState.IsKeyDown(key); /// /// Returns whether any key was pressed down on the previous state. /// /// /// if any key was pressed during the previous state; otherwise, . /// public bool WasAnyKeyJustDown() => _previousKeyboardState.GetPressedKeyCount() > 0; } }