This project is read-only.

Getting Started

Xna Input is rather simple to use. All you need to do is add a reference to the dll in your project, and include this using statement:

using XnaInput;

In your Game object, you'll need to add an InputManager:

public InputManager Input;

and in the constructor or initialize method, initialize the object:

Input = new InputManager(this);

Now you're ready to use your new input object. It automatically registers itself as a game component and a service, so you don't need to call it's update method every update cycle.

To add a control, call Input.AssignControl().

For example, to make controls for an arcade game that needs analog motion and a fire button, you can call:

Input.AssignControl(PlayerIndex.One, "X", Pad.ThumbSticks.Left.X);
Input.AssignControl(PlayerIndex.One, "Y", Pad.ThumbSticks.Left.Y);
Input.AssignControl(PlayerIndex.One, "Fire", Buttons.RightTrigger);

When you need to poll for input, call:

float x = Input.ControlState(PlayerIndex.One, "X");
float y = Input.ControlState(PlayerIndex.One, "Y");
bool fire = (Input.ControlState(PlayerIndex.One, "Fire") == 1.0f);

Later, if you need to change your input to use the mouse, just call AssignControl again with mouse parts as parameters:

Input.AssignControl(PlayerIndex.One, "X", Input.Mouse.X);
Input.AssignControl(PlayerIndex.One, "Y", Input.Mouse.Y);
Input.AssignControl(PlayerIndex.One, "Fire", Input.Mouse.Click(Input.Mouse.Button.Left));

The only difference in how the ControlState call will respond, is that now the x and y values will be the position of the mouse pointer, as opposed to relative values from the gamepad. We could just use Input.Mouse.RelativeX and RelativeY. These return the change in X and Y between last frame and the present one. Alternatively, you can implement it yourself:

Input.AssignControl(PlayerIndex.One, "X", new delegate { return Input.Mouse.PreviousState.X - Input.Mouse.State.X; } );

This shows that you can create your own input control methods, by just creating a delegate that returns a float. Because this system is based on delegates, it's very flexible and simple. The possibilities are endless. For example, a control that returns random numbers between 0 and 1:

Random rand = new Random();
Input.AssignControl(PlayerIndex.One, "Random", new delegate { return (float)rand.NextDouble(); });

This can then be replaced by other possible sources of randomness, such as mouse position, etc.

If you would prefer keyboard over mouse, that's possible too, even though the keyboard isn't analog. This is because we can assign one key as the "high" key, and the other as the "low" key. Continuing our example:

Input.AssignControl(PlayerIndex.One, "X", Keys.Right, Keys.Left);
Input.AssignControl(PlayerIndex.One, "Y", Keys.Up, Keys.Down);
Input.AssignControl(PlayerIndex.One, "Fire", Keys.Space);

The "X" control will now return 1.0f if the left arrow is pressed, -1.0f if the right arrow is pressed, and 0.0f if both or none are pressed. The "Y" control works in the same way. The "Fire" control, however, returns 1.0f if the space bar is pressed, and 0.0f otherwise. Pretty simple, eh? Xna Input makes it really easy to swap analog and non-analog controls in this manner. Game pad buttons are supported in a similar way. And of course, you can always make your own controls ;)

In XnaInput 1.5, support was added for selecting an InputMethod using a string. This is great if you want to use an XML settings file with your game, and have customizable input for your players. Here's how to use it:

Input.AssignControl(PlayerIndex.One, "X", "Mouse.X");
Input.AssignControl(PlayerIndex.One, "Y", "Pad.Down.DPadUp", "Pad.Down.DPadDown");
Input.AssignControl(PlayerIndex.One, "Fire", "Keyboard.JustPressed.Space");

Normally, the way you make the string is either "Mouse" "Keyboard" or "Pad" followed by "Up" "Down" "JustPressed" or "JustReleased" if it's a button, followed by the button name, or just the part name if it's analog.

That's it for basic usage. You now know how to use Xna Input.

Last edited Mar 21, 2009 at 5:07 PM by Shader, version 5


Armigus Feb 26, 2009 at 5:01 PM 
BTW, you implement the helpers like this:

private MouseHelper _mouse;
private KeyboardHelper _keyboard;

public MouseHelper Mouse
get { return _mouse; }

public KeyboardHelper Keyboard
get { return _keyboard; }

Armigus Feb 26, 2009 at 4:58 PM 
This component needs an interface to register itself as a service to other components:

public interface IInputService
void AssignControl(PlayerIndex player, string name, InputMethod method);
void AssignControl(PlayerIndex player, string name, Pad.Generator gen);
void AssignControl(PlayerIndex player, string name, Buttons button);
void AssignControl(PlayerIndex player, string name, Buttons high, Buttons low);
void AssignControl(PlayerIndex player, string name, Keys high, Keys low);
void AssignControl(string name, InputMethod method);
void AssignControl(string name, Pad.Generator gen);
void AssignControl(string name, PlayerIndex padPlayer, Pad.Generator gen);
void AssignControl(string name, Buttons button);
void AssignControl(string name, Buttons high, Buttons low);
void AssignControl(string name, Keys key);
void AssignControl(string name, Keys high, Keys low);
MouseHelper Mouse { get; }
KeyboardHelper Keyboard { get; }
float ControlState(PlayerIndex player, string name);

public class InputManager : GameComponent, IInputService
public InputManager(Game game) : base(game)
game.Services.AddService(typeof(IInputService), this);