C++ Win32 Using XBox Controller For Input With Windows Messages

Started by
12 comments, last by BennettSteele 12 years, 2 months ago
So i started thinking... how cool would it be too play my game with a controller? :D

But i have no clue how i would receive window messages from a wired controller... where should i start?
Advertisement
You don't use Win32 for that, you use XInput (part of DirectX)

http://msdn.microsoft.com/en-us/library/windows/desktop/ee417014%28v=vs.85%29.aspx
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Darn... Hopefully i will be able to merge the XInput code with the Win32 message code i have for key-presses... do you think its possible?
In theory you could use joySetCapture() to get messages from it like it was any other joystick device, but I haven't tried it myself.

Darn... Hopefully i will be able to merge the XInput code with the Win32 message code i have for key-presses... do you think its possible?


Yes it is possible and pretty much recommended. (Microsoft recommends using Win32 for keyboard/mouse, DirectInput for gamepads/joysticks and XInput for xbox360 controllers or other XInput capable devices)

You could treat the xbox360 controller as a DirectInput device if you want, but you will miss out on some functionality, Using joySetcapture is probably not a good idea as its pretty darn ancient and will not work very well with a xbox360 controller. (the old joystick handling code only supports one analog stick and 4 buttons per joystick and no more than 2 joysticks total).
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Im going to use XInput and try to merge the code for XBox and joystick related items into my key-press functions... wish me luck! tongue.png

Entry 1: 11:04

DX It took me this long too get the library working... so now i can really work! :D
Ok, i have it so the controllers can connect and such, but i cant seem so get the presses... what is wrong with this code?

unsigned int ConsUsing=0;
for(unsigned int Con=0;Con<4;Con++)
{
DWORD isController;//Is it connected?
_XINPUT_STATE ConState;//all states of controller
ZeroMemory(&ConState,sizeof(_XINPUT_STATE));
isController=XInputGetState(Con,&ConState);
if(isController==ERROR_SUCCESS)
{
if(!APP.UseXBOX[Con])
{
APP.UseXBOX[Con]=true;
std::stringstream ConRep;ConRep<<"Switched To Using Xbox Controller On Port: "<<Con;
APP.Log.Write(ConRep.str().c_str());
}
ConsUsing++;
}
else if(APP.UseXBOX[Con])
{
APP.UseXBOX[Con]=false;
std::stringstream ConRep;ConRep<<"NOT Using Xbox Controller On Port: "<<Con;
APP.Log.Write(ConRep.str().c_str());
}
}
if(ConsUsing!=0)
{
APP.UsingXbox=true;
}
//after alot of other stuff regarding normal key presses
else if(APP.UsingXbox)
{
for(unsigned int Con=0;Con<4;Con++)
{
_XINPUT_STATE ConState;//all states of controller
ZeroMemory(&ConState,sizeof(_XINPUT_STATE));
XInputGetState(Con,&ConState);
if(ConState.Gamepad.bLeftTrigger>0)
{
std::stringstream ConRep;ConRep<<"XBox Controller Pressed Start On Port: "<<Con;
APP.Quit(ConRep.str().c_str());
APP.Log.Write(ConRep.str().c_str());
}
}
}
As it stands there is nothing inherently wrong with your code, but that's given the fact that we know little about the structure of your entire program. A little bit more information and description is needed in order to figure out the problem. Based upon the small sampling above, there is no problem with the logic.

Although I have a bit of a recommendation. It's not necessary to poll the state of all controllers twice. Simply fold the detection of whether a controller is connected into the logic that handles input. It's only necessary to poll a controller's state once per update, seeing as it is the state.
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume
Nevermind, i got it to work! :D
Mind explaining what the problem was for future readers who search for threads involving XInput?
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume

This topic is closed to new replies.

Advertisement