Code:
///==========================================================================
/// ===Input.cs===
///Wraps user input into a class allowing checks to be performed on
///multiple input devices with a single method call. This lightweight
///version does not include support for GamePad.
///==========================================================================
///Author: Jonathan Deaves ("garfunkle")
///Date: 27-March-2011
///Version: 1.1(Light)
///==========================================================================
///Usage:
///1. Declare an Input instance at top of class that will receive input
/// E.G. Input inputHandle;
///2. Initalize it somewhere (usually in an init method of some kind)
/// E.G inputHandle = new Input();
///3. Call it's update during the class update, before any input.
/// E.G inputHandle.Update();
///3. Then call individual methods to check keys.
/// E.G.
/// if(inputHandle.MoveDown()){
/// Player.Position.Y++;
/// }
/// This example will check if the S key of keyboard is pressed.
///4. This default script comes with several methods which are commented for
/// their functions. Use them as a base for extending the function to your
/// personal project.
///==========================================================================
using System.Linq;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
namespace Menu_System
{
class Input
{
/// <summary>
/// These store current and previous states
/// of input devices supported.
/// </summary>
private KeyboardState keyboardState;
private KeyboardState previousKeyboardState;
private MouseState mouseState;
private MouseState previousmouseState;
public Point MousePoint
{
get { return new Point(mouseState.X, mouseState.Y); }
}
public Input()
{
}
public void Update()
{
//Sets previous states to that of what was just pressed.
previousKeyboardState = keyboardState;
previousmouseState = mouseState;
//Updates current states to current presses
keyboardState = Keyboard.GetState();
mouseState = Mouse.GetState();
}
public bool IsNewKeyPressed(Keys key)
{
//Returns true if a key has been pressed
//then released.
//Return false otherwise
return (keyboardState.IsKeyDown(key) &&
previousKeyboardState.IsKeyUp(key));
}
public bool IsKeyPressed(Keys key)
{
//Returns true if a key is being pressed
//at the time of checking.
//Returns false otherwise.
if (keyboardState.IsKeyDown(key))
return true;
return false;
}
public bool isMouseLeftNew()
{
//Returns true if left mouse button is
//pressed then released.
//Return false otherwise
if (mouseState.LeftButton == ButtonState.Pressed && previousmouseState.LeftButton == ButtonState.Released)
return true;
return false;
}
public bool isMouseRightNew()
{
//Returns true if left mouse button is
//being pressed at the time of checking.
//Return false otherwise
if (mouseState.RightButton == ButtonState.Pressed && previousmouseState.RightButton == ButtonState.Released)
return true;
return false;
}
/// <summary>
/// These next four methods check for Direction keys
/// or thumbstick position to determine movement.
/// They return true if it is being pressed
/// at the time of the check.
/// </summary>
public bool MoveDown()
{
if (keyboardState.IsKeyDown(Keys.Down))
return true;
return false;
}
public bool MoveUp()
{
if (keyboardState.IsKeyDown(Keys.Up))
return true;
return false;
}
public bool MoveLeft()
{
if (keyboardState.IsKeyDown(Keys.Left))
return true;
return false;
}
public bool MoveRight()
{
if (keyboardState.IsKeyDown(Keys.Right))
return true;
return false;
}
}
}