How to get JoyStick Z rotation messages in C++

2019-08-12 16:44发布

问题:

I'm having C++ code that properly intercepts a joysticks x and y positions for my Saitek Cyborg joystick.

This joystick also have the ability to twist around the handle axis, and I'm trying to get the messages for this motion.

For the x and y, the message ID's are MM_JOY1MOVE and MM_JOY2MOVE, and they allow me to capture the x and y position for the stick.

For the z-axis, there are MM_JOY1ZMOVE and MM_JOY2ZMOVE, but none of these do capture the twist motion.

Looking in mmsystem.h, the following defines are found:

....
#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
...

But none of these do work.

Question is: what is the messageID for the z-twist motion, or how can I find it?

回答1:

Well not sure what you mean by Z twist (if it is analog axis or just the POV button) but I handle joysticks with JOYINFOEX and JOYCAPS like this:

//---------------------------------------------------------------------------
//--- 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
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

Usage:

// 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));

This can handle 6 analog axises + POV button + all the buttons you want ...

Had written it for Win2000 but still works in Win7 x64 with my 4 axises + POV + 7 buttons Genius joystick :) (had to take the dust off it haven't hold it in hand for years)

PS.

This is really old source code so I needed to add this bds 2006 C hidden memory manager conflicts to it just to be sure.