Skip to content

Instantly share code, notes, and snippets.

@ohrodr
Created April 18, 2012 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohrodr/2411493 to your computer and use it in GitHub Desktop.
Save ohrodr/2411493 to your computer and use it in GitHub Desktop.
InputHandler for XNA Framework. Very Basic library component
#region Author
// Robb O'Driscoll <ohrodr@gmail.com>
// Very basic Input handling mechanism for the XNA framework.
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion
namespace gamelib
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class InputHandler : Microsoft.Xna.Framework.GameComponent
{
#region Fields
/// <summary>
/// Current keyboard state
/// </summary>
static KeyboardState currentState;
/// <summary>
/// Last keyboard state
/// </summary>
static KeyboardState lastState;
#endregion
#region Property Region
/// <summary>
/// Returns the current keyboard State
/// </summary>
public static KeyboardState KeyboardState
{
get { return currentState; }
}
/// <summary>
/// Returns the last known keyboard state
/// </summary>
public static KeyboardState LastKeyboardState
{
get { return lastState; }
}
#endregion
#region Constructor Region
public InputHandler(Game game)
: base(game)
{
// Store the keyboard state upon initialization
currentState = Keyboard.GetState();
}
#endregion
#region XNA Framework Methods
/// <summary>
/// Allows the game component to perform any initialization it needs to before
/// starting to run.
/// </summary>
public override void Initialize()
{
// Blank for now
base.Initialize();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// We need to store the old state and get the new state on update.
lastState = currentState;
currentState = Keyboard.GetState();
base.Update(gameTime);
}
#endregion
#region General Method Region
/// <summary>
/// Cycle the states by setting last to current. This is a bad cleanup.
/// <todo>Clean this up potentially</todo>
/// </summary>
public static void Flush()
{
lastState = currentState;
}
#endregion
#region Keyboard Region
/// <summary>
/// Check for releases by comparing the previous state to the current state.
/// In the event of a key release it will have been down, and currently its up
/// </summary>
/// <param name="key">This is the key to check for release</param>
/// <returns></returns>
public static bool KeyReleased(Keys key)
{
return currentState.IsKeyUp(key) &&
lastState.IsKeyDown(key);
}
/// <summary>
/// Given a previous key state of up determine if its been pressed
/// </summary>
/// <param name="key">key to check</param>
/// <returns></returns>
public static bool KeyPressed(Keys key)
{
return currentState.IsKeyDown(key) &&
lastState.IsKeyUp(key);
}
/// <summary>
/// Don't examine last state just check if a key is down
/// </summary>
/// <param name="key">key to check</param>
/// <returns></returns>
public static bool KeyDown(Keys key)
{
// check if a key is down regardless of current/past state
return currentState.IsKeyDown(key);
}
#endregion
}
}
@ohrodr
Copy link
Author

ohrodr commented Apr 18, 2012

Implement within your Game1 class constructor like so:

Components.Add(new InputHandler(this));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment