Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:37:47+00:00 2026-06-07T16:37:47+00:00

I am following the tutorial at CodeProject with input, and thought I would do

  • 0

I am following the tutorial at CodeProject with input, and thought I would do some myself. So I have tried to generate the following code, but when I press the D-pad, or other buttons nothing happens. But when I press the Start and Back buttons, it registers. Thanks in advance.

CXBOXController Class

#ifndef _XBOX_CONTROLLER_H_
#define _XBOX_CONTROLLER_H_

// No MFC
#define WIN32_LEAN_AND_MEAN

// We need the Windows Header and the XInput Header
#include <windows.h>
#include <XInput.h>

// Now, the XInput Library
// NOTE: COMMENT THIS OUT IF YOU ARE NOT USING
// A COMPILER THAT SUPPORTS THIS METHOD OF LINKING LIBRARIES
#pragma comment(lib, "XInput.lib")

// XBOX Controller Class Definition
class CXBOXController
{
private:
XINPUT_STATE _controllerState;
int _controllerNum;
public:
CXBOXController(int playerNumber);
XINPUT_STATE GetState();
bool IsConnected();
void Vibrate(int leftVal = 0, int rightVal = 0);
};

#endif

CXBOXController::CXBOXController(int playerNumber)
{
// Set the Controller Number
_controllerNum = playerNumber - 1;
}

XINPUT_STATE CXBOXController::GetState()
{
// Zeroise the state
ZeroMemory(&_controllerState, sizeof(XINPUT_STATE));

// Get the state
XInputGetState(_controllerNum, &_controllerState);

return _controllerState;
}

bool CXBOXController::IsConnected()
{
// Zeroise the state
ZeroMemory(&_controllerState, sizeof(XINPUT_STATE));

// Get the state
DWORD Result = XInputGetState(_controllerNum, &_controllerState);

if(Result == ERROR_SUCCESS)
{
    return true;
}
else
{
    return false;
}
}

void CXBOXController::Vibrate(int leftVal, int rightVal)
{
// Create a Vibraton State
XINPUT_VIBRATION Vibration;

// Zeroise the Vibration
ZeroMemory(&Vibration, sizeof(XINPUT_VIBRATION));

// Set the Vibration Values
Vibration.wLeftMotorSpeed = leftVal;
Vibration.wRightMotorSpeed = rightVal;

// Vibrate the controller
XInputSetState(_controllerNum, &Vibration);
}

Code

                Player1 = new CXBOXController(1);

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
                {
                    std::cout << "Up";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
                {
                    std::cout << "Down";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
                                    {
                    std::cout << "Right";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
                {
                    std::cout << "Left";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
                {
                    std::cout << "Right-Click";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
                {
                    std::cout << "Click";
                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:37:48+00:00Added an answer on June 7, 2026 at 4:37 pm

    I use the following code in my game engine; the trick to differentiate rising and falling keys is to measure the state of the game pad before and after:

    void Nitro::GamePadState::GetState(u32 playerIndex)
    {
        XINPUT_STATE xinputState;
    
        DWORD result = XInputGetState(playerIndex, &xinputState);
    
        if (result == ERROR_SUCCESS)
        {
            Triggers.Left = xinputState.Gamepad.bLeftTrigger;
            Triggers.Right = xinputState.Gamepad.bRightTrigger;
            ThumbSticks.Left = short2 (xinputState.Gamepad.sThumbLX, xinputState.Gamepad.sThumbLY);
            ThumbSticks.Right = short2 (xinputState.Gamepad.sThumbRX, xinputState.Gamepad.sThumbRY);
            mButtonStates.Update(xinputState.Gamepad.wButtons);
        }
    }
    
    class NitroAPI ButtonState
    {
    private:
        u32 mButtonStates;      // Current buttons' state
        u32 mPrevButtonStates;  // Previous buttons' state
        u32 mButtonDowns;       // 1 = button pressed this frame
        u32 mButtonUps;         // 1 = button released this frame
    
    public:
        ButtonState();
        void Update(u32 newState);
    
        bool IsButtonDown(Buttons button) const;
        bool IsButtonFalling(Buttons button) const;
        bool IsButtonUp(Buttons button) const;
        bool IsButtonRising(Buttons button) const;
    };
    

    The important method is Update and the IsButton*. I XOR the previous and current button state to differentiate what changed:

    void Nitro::ButtonState::Update(u32 newState)
    {
        mPrevButtonStates = mButtonStates;
        mButtonStates = newState;
    
        u32 buttonChanges = mButtonStates ^ mPrevButtonStates;
        mButtonDowns = buttonChanges & mButtonStates;
        mButtonUps = buttonChanges & (~mButtonStates);
    }
    

    Then it’s a simple comparison to see what button changed:

    bool Nitro::ButtonState::IsButtonFalling(Buttons button) const
    {
        return ((mButtonStates & button) != 0) && ((mPrevButtonStates & button) == 0);
    }
    
    bool Nitro::ButtonState::IsButtonRising(Buttons button) const
    {
        return ((mButtonStates & button) == 0) && ((mPrevButtonStates & button) != 0);
    }
    
    bool Nitro::ButtonState::IsButtonDown(Buttons button) const
    {
        return ((mButtonStates & button) != 0);
    }
    
    bool Nitro::ButtonState::IsButtonUp(Buttons button) const
    {
        return ((mButtonStates & button) == 0);
    }
    

    In your case you could alleviate the problem by placing your code in a while loop, or use something similar to what I posted to keep track of the transients:

    while (true)
    {
        DWORD Result = XInputGetState(_controllerNum, &_controllerState);
        if (result == ERROR_SUCCESS)
        {
            if(_controllerState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
            // ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following this tutorial http://www.codeproject.com/KB/cpp/authforwebservices.aspx They have this in the tutorial [SoapHeader(Authentication, Required
I have made a C# COM object by following the tutorial. http://www.codeproject.com/Articles/18939/C-Com Now I
I have been playing with the source code available from the following tutorial .
I have created a listview using following tutorial link http://www.ezzylearning.com/tutorial.aspx?tid=1763429 Outcome of this is,
i'm following this tutorial: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide and downloading my own source of mod_wsgi. The problem
I have done the following tutorial: http://www.anddev.org/networking-database-problems-f29/connecting-to-mysql-database-t50063.html for connecting an android device to an
I have read the following tutorial Uploading Files To the Server Using PHP and
I've tried to get trough the following tutorial Making Operating System Calls from SQL
I am following this tutorial: http://www.codeproject.com/KB/android/AndroidSQLite.aspx I must be overthinking this SQLite stuff (in
I have used the following tutorial for my project . I need to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.