我的C ++代码可以正确拦截我的Saitek Cyborg游戏杆的游戏杆x和y位置。
该操纵杆还具有绕手柄轴扭转的功能,我正在尝试获取有关此动作的消息。
对于x和y,消息ID为MM_JOY1MOVE和MM_JOY2MOVE,它们使我能够捕获操纵杆的x和y位置。
对于z轴,有MM_JOY1ZMOVE和MM_JOY2ZMOVE,但没有一个捕获扭曲运动。
在mmsystem.h中查找以下定义:
....
#define MM_JOY1MOVE 0x3A0 /* joystick */
#define MM_JOY2MOVE 0x3A1
#define MM_JOY1ZMOVE 0x3A2
#define MM_JOY2ZMOVE 0x3A3
#define MM_JOY1BUTTONDOWN 0x3B5
#define MM_JOY2BUTTONDOWN 0x3B6
#define MM_JOY1BUTTONUP 0x3B7
#define MM_JOY2BUTTONUP 0x3B8
...
但是这些都不起作用。
问题是:z扭曲运动的messageID是什么,或者如何找到它?
最佳答案
不太确定Z扭曲(如果它是模拟轴或只是POV按钮)是什么意思,但是我用JOYINFOEX
和JOYCAPS
处理操纵杆是这样的:
//---------------------------------------------------------------------------
//--- Spektres Joystick class ver: 1.1 --------------------------------------
//---------------------------------------------------------------------------
#ifndef _joystick_h
#define _joystick_h
//---------------------------------------------------------------------------
//#include <dinput.h>
#include <Mmsystem.h>
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
const int _joystick_max=16;
//---------------------------------------------------------------------------
class joystick
{
public: int _enable_double;
int _x,_y,_z,_r,_u,_v,_pov; // status non zero ?
double x,y,z,r,u,v,pov; // pos
double x0,y0,z0,r0,u0,v0; // min
double dx,dy,dz,dr,du,dv; // 2/(max-min)
double deadzone;
DWORD buttons;
JOYINFOEX joy_info;
JOYCAPS joy_cp;
int joy_list[_joystick_max],joy_num;
int joy_id;
joystick() { deadzone=0.1; _enable_double=1; joy_id=-1; joy_num=0; init_list(); }
joystick(joystick& a) { *this=a; }
~joystick() {}
joystick* operator = (const joystick *a) { *this=*a; return this; }
//joystick* operator = (const joystick &a) { ...copy... return this; }
void init_list();
void refresh();
void refresh_cp();
};
//---------------------------------------------------------------------------
void joystick::init_list()
{
int i,j;
joy_num=0;
j=JOYSTICKID1;
for (i=0;i<_joystick_max;i++)
{
joy_info.dwSize = sizeof(JOYINFOEX);
joy_info.dwFlags=JOY_RETURNALL;
if (joyGetPosEx(i,&joy_info)==JOYERR_NOERROR)
{
joy_list[joy_num]=i;
joy_num++;
j++;
}
}
j=0;
for (i=0;i<joy_num;i++)
if (joy_list[i]==joy_id)
{ j=1; break; }
if (!j)
{
joy_id=-1;
if (joy_num>0) joy_id=joy_list[0];
}
refresh();
refresh_cp();
}
//---------------------------------------------------------------------------
void joystick::refresh()
{
if (joy_id==-1) return;
joy_info.dwSize = sizeof(JOYINFOEX);
joy_info.dwFlags=JOY_RETURNALL;
joyGetPosEx(joy_id,&joy_info);
if (_enable_double)
{
x=double(joy_info.dwXpos-x0)*dx-1.0;
y=double(joy_info.dwYpos-y0)*dy-1.0;
z=double(joy_info.dwZpos-z0)*dz-1.0;
r=double(joy_info.dwRpos-r0)*dr-1.0;
u=double(joy_info.dwUpos-u0)*du-1.0;
v=double(joy_info.dwVpos-v0)*dv-1.0;
pov=double(joy_info.dwPOV)*0.01;
_x=0; if (x<=-deadzone) { _x=1; x+=deadzone; } if (x>=+deadzone) { _x=1; x-=deadzone; }
_y=0; if (y<=-deadzone) { _y=1; y+=deadzone; } if (y>=+deadzone) { _y=1; y-=deadzone; }
_z=0; if (z<=-deadzone) { _z=1; z+=deadzone; } if (z>=+deadzone) { _z=1; z-=deadzone; }
_r=0; if (r<=-deadzone) { _r=1; r+=deadzone; } if (r>=+deadzone) { _r=1; r-=deadzone; }
_u=0; if (u<=-deadzone) { _u=1; u+=deadzone; } if (u>=+deadzone) { _u=1; u-=deadzone; }
_v=0; if (v<=-deadzone) { _v=1; v+=deadzone; } if (v>=+deadzone) { _v=1; v-=deadzone; }
_pov=1; if (joy_info.dwPOV==65535) _pov=0;
buttons=joy_info.dwButtons;
}
}
//---------------------------------------------------------------------------
void joystick::refresh_cp()
{
if (joy_id==-1) return;
joyGetDevCaps(joy_id,&joy_cp,sizeof(JOYCAPS));
double q=2.0+deadzone+deadzone;
x0=joy_cp.wXmin; dx=joy_cp.wXmax-x0; if (dx) dx=q/dx;
y0=joy_cp.wYmin; dy=joy_cp.wYmax-y0; if (dy) dy=q/dy;
z0=joy_cp.wZmin; dz=joy_cp.wZmax-z0; if (dz) dz=q/dz;
r0=joy_cp.wRmin; dr=joy_cp.wRmax-r0; if (dr) dr=q/dr;
u0=joy_cp.wUmin; du=joy_cp.wUmax-u0; if (du) du=q/du;
v0=joy_cp.wVmin; dv=joy_cp.wVmax-v0; if (dv) dv=q/dv;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
用法:
// some (locally) global variable
joystick joy;
// do this in some timer or whatever
joy.refresh();
// this just prints the joystick state on VCL Form->Canvas you can rewrite it to what you want:
int x,y,dy;
dy=20;
x=10;
y=10-dy;
Canvas->FillRect(TRect(x-20,y,x+200,y+15*dy));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("x: %i",joy.joy_info.dwXpos));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("y: %i",joy.joy_info.dwYpos));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("z: %i",joy.joy_info.dwZpos));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("r: %i",joy.joy_info.dwRpos));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("u: %i",joy.joy_info.dwUpos));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("v: %i",joy.joy_info.dwVpos));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("b: %i",joy.joy_info.dwButtons));
Canvas->TextOutA(x,y+=dy,AnsiString().sprintf("z: %i",joy.joy_info.dwPOV));
它可以处理6个模拟轴+ POV按钮+您想要的所有按钮...
曾经为Win2000编写过它,但仍然可以在Win7 x64中使用我的4轴+ POV + 7个按钮Genius游戏杆:)(不得不除掉灰尘多年未使用)
PS。
这确实是旧的源代码,因此为了确定我需要在其中添加此bds 2006 C hidden memory manager conflicts。