所以我在我的程序中使用了 xinput,它已经全部设置好并且正在工作,所以我可以检测到我的 xbox one 控制器。我希望能够检测到何时按下控制器上的按钮。如果我在程序启动时按住按钮,我使用的程序就可以工作。我在一段时间内设置了 if 命令,因此它会不断执行,尽管由于某种原因,当我在控制器上按 A 时该值不会改变。
所以基本上,如果我在程序打开时按住 A ,它就会工作并在屏幕上返回 cout 。虽然如果我想在程序启动后稍微按下它(这是我想要的工作)它不会检测到它。
这是我的代码:
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;
}
};
}
据我所知,我以正确的顺序包含了所有正确的库:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <Xinput.h>
#pragma comment(lib, "Xinput.lib")
#pragma comment(lib, "Xinput9_1_0.lib")