Re: Converting Keyboard to Xbox Controller?

Basically i have pong working so that the arrow keys control one paddle and w and s keys controlling the other and i want to convert it so i can put it on to my xbox and play with a controller i already have it in xbox format in visual stuido but i need to change some of the code to get the controllers to work

That's more or less it in a nutshell. If you work with gamepads though, at some point you will want to detect if the controller's still connected, and which controller the player uses and read from that controller and not PlayerIndex.One by default.

But the simple code above shows the similarity between reading from keyboard and reading from gamepad.

here is the code i have i already chamged the first line but then do i just change the rest to the corresponding buttons on the xbox controller or does it stay the same and visual studio changes it already I'm not to sure because i havent done too much with xbox yet. thanks

Re: Converting Keyboard to Xbox Controller?

I would have to disagree... what if you wanted players to be able to use their Xbox controllers on the PC? Also remember that the keyboard still works on the Xbox (admittedly it's not common but it does work perfectly).

Generic input is tricky - particularly if you want to use the thumbsticks on the xbox version as well as provide a mouse / keyboard alternative. I ended up creating a massive enum that contained values for all of the Keys enum, all of the mouse buttons and 4 x all of the gamepad buttons. I then had a IsButtonDown(GenericButton btn) method that determines which keyboard, gamepad or mouse button was requested and checks the correct input states based on that.

Another layer of abstraction is provided by a IsButtonDownMapped(int btnId). The idea here is that the input system stores a map of int -> GenericButton[]. You will have a PlayerInput enum that has values like Left, Right, Jump, Duck etc. Using this system, each of the PlayerInput values can be bound to any number of configurable generic buttons. To support multiple players at once, I have a little method like this:

The benefit is that the input is configurable at runtime, keys are handled in exactly the same way as mouse buttons and any differing input code paths for PC vs XBOX are restricted to one place - at startup. I wouldn't say it's the best possible solution to the gamepad vs keyboard conundrum, but it works well for me.

Re: Converting Keyboard to Xbox Controller?

Hey Zach, please use the Format Code Block with a maximum height (fourth last toolbar button in the post editor) when posting that much code, else the mods won't be happy ;)

Yeah, my generic input solution does indeed fall down when you actually want to use the Thumbstick's analog functionality. If you are just using it for going Up/Down/Left/Right, there isn't a problem. Have a look at Microsoft.Xna.Framework.Input.Buttons enum. This actually has values for all four directions of both Thumbsticks. This means you can say

if (gpState.IsButtonDown(Buttons.LeftThumbstickRight))
{
// Player is pushing right
}

So a simple version of my setup could be to designate the keyboard as controller Id 5 and have methods like this:

// you should store an array of game pad states (one for each controller)

If you don't like the idea of writing all those methods for every different possible input value, you might prefer to use a dictionary of Buttons -> Keys to define the gamepad -> keyboard equivalents... something like this:

Dictionary<Buttons, Keys> keyMappings;

// in initialise or constructor

public MyClassName()

{

gpState = new GamePadState[4];

gpStateLast = new GamePadState[4];

keyMappings = new Dictionary<Buttons, Keys>();

keyMappings.Add(Buttons.LeftThumbstickLeft, Keys.Left);

keyMappings.Add(Buttons.LeftThumbstickUp, Keys.Up);

// etc etc

}

// Movement methods

publicbool IsButtonDown(int playerId, Buttons btn)

{

if (playerId > 4)

{

//Keboard!

// probably want better error handling here

Keys k = keyMappings[btn];

return kbState.IsKeyDown(k);

}

else

{

// Gamepad

return gpState[playerId].IsButtonDown(btn);

}

}

If you want to use the dictionary method for key mappings, you will need to also look into using IEqualityComparer<T> to avoid garbage. See Nick's blog here for more info.

Re: Converting Keyboard to Xbox Controller?

Re: Converting Keyboard to Xbox Controller?

Here is what I did that may help you, but its a little long and drawn out. I just made a controllerManager class that would give me access to bools that I made. So I have public bool UpJustPressed() and public bool UpPressed(player index) the first being a single hit check and the second being a check to see if the button is held.

then the second method looks like this (this is rough code and may not work directly)