Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to add basic gamepad input to my c++ DirectX game but i'm having some trouble creating a press stamp (registers the press only the first time it's pressed if button held down).

The entire input class is here:

My ButtonPressed function:

bool Input::wasGamePadButtonPressed(int padNum, WORD button)
    if((gamepad[padNum].wButtons & button) & 
        ((gamepad[padNum].wButtons & button) != (prevGamepadState[padNum].wButtons & button)))
        return true;
    return false;

The It's just as basic press stamp, if the button is down, and the button was not down before, return true, else return false.

In my input::strobe() i strobe all the inputs and collect their states. at the beginning of the strobe i copy the current states and save them to a previous states structure

void Input::strobe(void)
    //hold on to the previous states for input stamping
    memcpy(prevKeys, keys, sizeof(keys));
    memcpy(prevGamepadState, gamepad, sizeof(gamepad));

    //gather keyboard
    HRESULT err;
    err = keyboard->GetDeviceState(sizeof(keys), (LPVOID) &keys);
    if((err == DIERR_INPUTLOST )|| (err == DIERR_NOTACQUIRED))

    //gather mouse
    err = mouse->GetDeviceState(sizeof(mouse_state), (LPVOID) &mouse_state);
    if((err == DIERR_INPUTLOST )|| (err == DIERR_NOTACQUIRED))

    //strobe controllers
        for(int ii = 0; ii < 4; ii++)
            ZeroMemory(&gamepad[ii], sizeof(XINPUT_STATE));
            XINPUT_STATE state;
            DWORD result = XInputGetState(ii, &state);

            if(result == 0)
                gamepad[ii] = state.Gamepad;

This works fine for my keyboard press stamp, which is very similar to the gamepad press function, but the game pad one refuses to work for some reason, firing true every frame for as long as i hold down the button.

It seems really straight forward so i cannot think of why it't not working. the only thing i can think is an issue in the strober, which i did not write originally (most of the basic directinput stuff was given in a class i took), or its me misunderstanding bitwise masks and I've got that gamepadpressed function totally wrong.

share|improve this question
What values can "button" have, specifically? – Trevor Powell May 11 '13 at 0:59
these in the XINPUT_GAMEPAD structure:… they're bitmasks – Charles May 11 '13 at 1:06
One thing to remember about XInputGetState is that it's a non-buffering polling API. If you don't call it fast enough, you may miss 'stamps'. BTW, I see you are using DirectInput for keyboard and mouse. It is strongly recommended that you make use of standard Win32 messages instead for keyboard & mouse. DirectInput should be used only for legacy HID controllers (ala joysticks and the like). – Chuck Walbourn Jul 8 '14 at 20:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.