So I am using xinput with my program, it is all set up and working so I can detect my xbox one controller. I want to be able to detect when a button on the controller is pressed. The procedure I use works if I hold down the button when the program starts. I have the if command setup inside a while so it constantly executes although for some reason the value does not change when I press A on my controller.
So basically, if I hold down A when the program is opening it works and returns the cout on the screen. Although if I want to press it a little after the program has started (which is what I want to work) it does not detect it.
Here is my code:
using namespace std;
XINPUT_STATE state;
bool A_button_pressed;
int online;
int test;
int main() {
if (XInputGetState(0, &state) == ERROR_SUCCESS)
{
online = 1;
cout << "I could find a controller, it is an Xbox Controller" << endl;
} else {
online = 2;
cout << "Unable to find controller, searching..." << endl;
}
cout << A_button_pressed << endl;
cout << "Active" << endl;
while (online == 1) {
bool A_button_pressed = ((state.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0);
cout << A_button_pressed << endl;
if (A_button_pressed = ((state.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0)) {
cout << "You pressed a button, congrats, game over..." << endl;
}
};
}
As far as I know I am including all of the correct libraries in the correct order:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <Xinput.h>
#pragma comment(lib, "Xinput.lib")
#pragma comment(lib, "Xinput9_1_0.lib")
Your problem is that you are only calling XInputGetState once at startup. You must call XInputGetState every frame that your program runs so that your state info can be updated.