KeyboardStateExtended.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using Microsoft.Xna.Framework.Input;
  6. namespace MonoGame.Extended.Input
  7. {
  8. /// <summary>
  9. /// Represents the state of keyboard input
  10. /// </summary>
  11. /// <remarks>
  12. /// This is an extended version of the base <see cref="Microsoft.Xna.Framework.Input.KeyboardState"/> struct
  13. /// that provides utility for checking the state differences between the previous and current state.
  14. /// </remarks>
  15. public struct KeyboardStateExtended
  16. {
  17. private KeyboardState _currentKeyboardState;
  18. private KeyboardState _previousKeyboardState;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="KeyboardStateExtended"/> value.
  21. /// </summary>
  22. /// <param name="currentKeyboardState">The state of keyboard input during the current update cycle.</param>
  23. /// <param name="previousKeyboardState">The state of keyboard input during the previous update cycle.</param>
  24. public KeyboardStateExtended(KeyboardState currentKeyboardState, KeyboardState previousKeyboardState)
  25. {
  26. _currentKeyboardState = currentKeyboardState;
  27. _previousKeyboardState = previousKeyboardState;
  28. }
  29. /// <summary>
  30. /// Gets a value that indicates whether the caps lock key is down during the current state.
  31. /// </summary>
  32. public bool CapsLock
  33. {
  34. #if FNA
  35. get { return _currentKeyboardState.IsKeyDown(Keys.CapsLock); }
  36. #else
  37. get { return _currentKeyboardState.CapsLock; }
  38. #endif
  39. }
  40. /// <summary>
  41. /// Gets a value that indicates whether the num lock key is down during the current state.
  42. /// </summary>
  43. public bool NumLock
  44. {
  45. #if FNA
  46. get { return _currentKeyboardState.IsKeyDown(Keys.NumLock); }
  47. #else
  48. get { return _currentKeyboardState.NumLock; }
  49. #endif
  50. }
  51. /// <summary>
  52. /// Returns a value that indicates whether either the left or right shift key is down during the current state.
  53. /// </summary>
  54. /// <returns>
  55. /// <see langword="true"/> if either the left or right shift key is down during the current state; otherwise,
  56. /// <see langword="false"/>.
  57. /// </returns>
  58. public bool IsShiftDown() => _currentKeyboardState.IsKeyDown(Keys.LeftShift) || _currentKeyboardState.IsKeyDown(Keys.RightShift);
  59. /// <summary>
  60. /// Returns a value that indicates whether either the left or right control key is down during the current
  61. /// state.
  62. /// </summary>
  63. /// <returns>
  64. /// <see langword="true"/> if either the left or right control key is down during the current state; otherwise,
  65. /// <see langword="false"/>.
  66. /// </returns>
  67. public bool IsControlDown() => _currentKeyboardState.IsKeyDown(Keys.LeftControl) || _currentKeyboardState.IsKeyDown(Keys.RightControl);
  68. /// <summary>
  69. /// Returns a value that indicates whether either the left or righ talt key is currently pressed down.
  70. /// </summary>
  71. /// <returns></returns>
  72. public bool IsAltDown() => _currentKeyboardState.IsKeyDown(Keys.LeftAlt) || _currentKeyboardState.IsKeyDown(Keys.RightAlt);
  73. /// <summary>
  74. /// Returns a value that indicates if the specified key is down during the current state.
  75. /// </summary>
  76. /// <param name="key">The key to check.</param>
  77. /// <returns>
  78. /// <see langword="true"/> if the key is down during the current state; otherwise, <see langword="false"/>.
  79. /// </returns>
  80. public bool IsKeyDown(Keys key) => _currentKeyboardState.IsKeyDown(key);
  81. /// <summary>
  82. /// Returns a value that indicates if the specified key is up during the current state.
  83. /// </summary>
  84. /// <param name="key">The key to check.</param>
  85. /// <returns>
  86. /// <see langword="true"/> if the key is up during the current state; otherwise, <see langword="false"/>.
  87. /// </returns>
  88. public bool IsKeyUp(Keys key) => _currentKeyboardState.IsKeyUp(key);
  89. /// <summary>
  90. /// Returns the total number of keys down during the current state.
  91. /// </summary>
  92. public int GetPressedKeyCount => _currentKeyboardState.GetPressedKeyCount();
  93. /// <summary>
  94. /// Returns an array of all keys that are down during the current state.
  95. /// </summary>
  96. /// <returns>
  97. /// An array of <see cref="Keys"/> values that represent each key that is down during the current state.
  98. /// </returns>
  99. public Keys[] GetPressedKeys() => _currentKeyboardState.GetPressedKeys();
  100. /// <summary>
  101. /// Fills an existing array with the keys pressed during the current state.
  102. /// </summary>
  103. /// <param name="keys">An existing array to fill with the pressed keys.</param>
  104. /// <exception cref="ArgumentNullException">
  105. /// Thrown if the <paramref name="keys"/> parameter is <see langword="null"/>.
  106. /// </exception>
  107. /// <exception cref="ArgumentOutOfRangeException">
  108. /// Thrown if the array provided by the <paramref name="keys"/> parameter is not large enough to fit all
  109. /// pressed keys. Use <see cref="GetPressedKeyCount"/> to determine the total number of elements.
  110. /// </exception>
  111. public void GetPressedKeys(Keys[] keys) => _currentKeyboardState.GetPressedKeys(keys);
  112. /// <summary>
  113. /// Returns whether the given key was down during previous state, but is now up.
  114. /// </summary>
  115. /// <param name="key">The key to check.</param>
  116. /// <returns>
  117. /// <see langword="true"/> if the key was released this state-change, otherwise <see langword="false"/>.
  118. /// </returns>
  119. public readonly bool WasKeyReleased(Keys key) => _previousKeyboardState.IsKeyDown(key) && _currentKeyboardState.IsKeyUp(key);
  120. /// <summary>
  121. /// Returns whether the given key was up during previous state, but is now down.
  122. /// </summary>
  123. /// <param name="key">The key to check.</param>
  124. /// <returns>
  125. /// <see langword="true"/> if the key was pressed this state-change, otherwise <see langword="false"/>.
  126. /// </returns>
  127. public readonly bool WasKeyPressed(Keys key) => _previousKeyboardState.IsKeyUp(key) && _currentKeyboardState.IsKeyDown(key);
  128. /// <summary>
  129. /// Returns whether any key was pressed down on the previous state.
  130. /// </summary>
  131. /// <returns>
  132. /// <see langword="true"/> if any key was pressed during the previous state; otherwise, <see langword="false"/>.
  133. /// </returns>
  134. public bool WasAnyKeyJustDown() => _previousKeyboardState.GetPressedKeyCount() > 0;
  135. }
  136. }