UEXDev

InputHandler Impl

May 8th, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. public sealed class InputHandler : GameComponent, IInputManager
  2. {
  3.     public InputHandler(Game game)
  4.         : base(game)
  5.     {
  6.     }
  7.  
  8.     // assigned in Update() call
  9.     KeyboardState lastState;
  10.  
  11.     /// <summary>
  12.     /// Überprüft ob eine Taste gedrückt worden ist.
  13.     /// </summary>
  14.     public bool IsKeyPressed(Keys key)
  15.     {
  16.         Keys[] lastKeys = this.lastState.GetPressedKeys();
  17.         return IsKeyDown(key) && !lastKeys.Contains(key);
  18.     }
  19.    
  20.     /// <summary>
  21.     /// Überprüft, ob eine Taste momentan gedrückt ist.
  22.     /// </summary>
  23.     public bool IsKeyDown(Keys key)
  24.     {
  25.         KeyboardState currentState = Keyboard.GetState();
  26.         Keys[] currentKeys = currentState.GetPressedKeys();
  27.         return currentKeys.Contains(key);
  28.     }
  29.  
  30.     public override void Update(GameTime gameTime)
  31.     {
  32.         lastState = Keyboard.GetState();
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment