KeyboardExtended.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Microsoft.Xna.Framework.Input;
  2. namespace MonoGame.Extended.Input;
  3. /// <summary>
  4. /// Represents keyboard input.
  5. /// </summary>
  6. /// <remarks>
  7. /// This is an extended version of the default <see cref="Microsoft.Xna.Framework.Input.Keyboard"/> class
  8. /// which offers internal tracking of both the previous and current state of keyboard input.
  9. /// </remarks>
  10. public static class KeyboardExtended
  11. {
  12. private static KeyboardState _currentKeyboardState;
  13. private static KeyboardState _previousKeyboardState;
  14. /// <summary>
  15. /// Gets the state of keyboard input.
  16. /// </summary>
  17. /// <returns>
  18. /// A <see cref="KeyboardStateExtended"/> value that represents the state of keyboard input.
  19. /// </returns>
  20. public static KeyboardStateExtended GetState()
  21. {
  22. return new KeyboardStateExtended(_currentKeyboardState, _previousKeyboardState);
  23. }
  24. /// <summary>
  25. /// Updates the <see cref="KeyboardExtended"/>
  26. /// </summary>
  27. /// <remarks>
  28. /// This internally will overwrite the source data for the previous state with the current state, then get the
  29. /// current state from the keyboard input. This should only be called once per update cycle. Calling it more
  30. /// than once per update cycle can result in the cached previous state being overwritten with invalid data.
  31. /// </remarks>
  32. public static void Update()
  33. {
  34. _previousKeyboardState = _currentKeyboardState;
  35. _currentKeyboardState = Keyboard.GetState();
  36. }
  37. }