My team and I are currently developing a 2D platformer with SDL/OpenGL and we want to add support to the Xbox360 Gamepad with the XInput library from Microsoft, but we are currently having a problem.

First of all, the method for reading the key states it's this one:

bool InputMapping::Gamepad::checkKeyState(WORD button)
  DWORD dwResult;
  DWORD idPlayer = playerID - 1;

  ZeroMemory( &state, sizeof(XINPUT_STATE) );

  dwResult = XInputGetState( idPlayer, &state );

  if ( state.Gamepad.wButtons & button)
    return true;

  return false;

This method returns true or false if the selected key it's pressed or not.

But it seems that the polling of the keys it's ocurring really fast, in the menus, moving through the buttons it's really fast and I can't controlled.

There is a way to limit the time between polling inside of the XInput or I should do it by myself? or what you recommend me?

If I didn't explain me clearly don't hesitate in say that.

Thanks a lot.


There is a good reason why polling of controls is fast. Finally it is the only way the user can take action in your game. ;-)

Ok, now the (hopefully) non obvious part: You could either require the user to press and release the button to navigate in the menus. For that you would store a boolean flag if button is pressed and fire your "clicked" event after button is released again.

Same thing could be done by storing game/system time and firing clicked event every x ms.

In both cases I would recommend using this method only in menus and not for controlling the actual game character since that would result in serious lagging.

|improve this answer|||||
  • \$\begingroup\$ Thanks a lot for this great comment, I implemented the released key method and works perfectly. \$\endgroup\$ – oscar.rpr Jan 2 '12 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.