1

我目前正在编写一些 C++ 代码来检测游戏手柄按钮按下。我正在使用以下代码来定义一组可能的按钮按下:

#include <windows.h>
#include <mmsystem.h>

this->buttons[0] = JOY_BUTTON1;
this->buttons[1] = JOY_BUTTON2;
...
this->buttons[31] = JOY_BUTTON32;

然后使用类似下面的东西来检测按下了哪个按钮:

joyGetPosEx(this->joyStickId, &info);

buttonPressed = false;

for(int i=0; i<32; i++){
  if((info.dwButtons & this->buttons[i]) == this->buttons[i]){
    buttonPressed = true;
    cout << "button number " << (i+1) << "was pressed!" << endl;
  }
}

if(buttonPressed === false){
  cout << "could not detect button press, dwButtons was set to: " << info.dwButtons << endl;
}

这适用于游戏手柄按钮 1-4。但是,按钮 5-32 不起作用。例如,当按下游戏手柄上的按钮 5 时,程序认为dwButtons设置为 16。JOY_BUTTON5定义mmsystem.h为 257。所以在我看来, JOY_BUTTON5 - 32 在 mmsystem 中定义不正确。是这样吗,还是我错过了什么?

4

1 回答 1

0

我假设您正在使用 MinGW。是的,这是他们头文件中的一个错误。Microsoft Win32 头文件具有不同的值(正确的值)。

MinGW 目前有:

#define JOY_BUTTON5 257
#define JOY_BUTTON6 513
#define JOY_BUTTON7 1025
#define JOY_BUTTON8 2049

它应该是:

#define JOY_BUTTON5 16
#define JOY_BUTTON6 32
#define JOY_BUTTON7 64
#define JOY_BUTTON8 128
于 2016-10-31T13:16:59.833 回答