Input Wrapper Class for XNA
Input.cs
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.
Info
This is a simple implementation of an Input Wrapper for XNA (should work in 3.1 and 4). It uses methods to determine keys pushed and uses specific methods to call movement direction.
This class is my own and as such credit goes to me and you should leave comments intact if you are to use it.
Usage
Instructions for usage are in the script or demo.
Demo
Dead Megaupload Link (Requires VS2008 and XNA 3.1 minimum, built using full VS so PM me if you have express and it does not build and I will create a new version).
Script
"Input.cs Version 1.0"
Code:
///==========================================================================
/// ===Input.cs===
///Wraps user input into a class allowing checks to be performed on
///multiple input devices with a single method call.
///==========================================================================
///Author: Jonathan Deaves ("garfunkle")
///Date: 27-March-2011
///Version: 1.0
///==========================================================================
///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 or if the
/// gamepad thumbstick is pushed downwards.
///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 InputHandling
{
class Input
{
/// <summary>
/// These store current and previous states
/// of input devices supported.
/// </summary>
public KeyboardState keyboardState;
public KeyboardState previousKeyboardState;
public MouseState mouseState;
public MouseState previousmouseState;
public GamePadState gamePadState;
public GamePadState previousGamePadState;
public Input()
{
}
public void Update()
{
//Sets previous states to that of what was just pressed.
previousKeyboardState = keyboardState;
previousmouseState = mouseState;
previousGamePadState = gamePadState;
//Updates current states to current presses
keyboardState = Keyboard.GetState();
mouseState = Mouse.GetState();
gamePadState = GamePad.GetState(PlayerIndex.One);
}
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 (gamePadState.ThumbSticks.Left.Y < 0 ||
keyboardState.IsKeyDown(Keys.Down))
return true;
return false;
}
public bool MoveUp()
{
if (gamePadState.ThumbSticks.Left.Y > 0 ||
keyboardState.IsKeyDown(Keys.Up))
return true;
return false;
}
public bool MoveLeft()
{
if (gamePadState.ThumbSticks.Left.X < 0 ||
keyboardState.IsKeyDown(Keys.Left))
return true;
return false;
}
public bool MoveRight()
{
if (gamePadState.ThumbSticks.Left.X > 0 ||
keyboardState.IsKeyDown(Keys.Right))
return true;
return false;
}
}
}
"Input.cs Verson 1.1(lightweight)"
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;
}
}
}
To extend this
Create your own method for Confirmation input (Press Enter to confirm/contine)
*Advanced* Implement text typing by returning key being pressed and adding a string. (PM for more info if you wish)