如何在C ++中获取JoyStick Z旋转消息 [英] How to get JoyStick Z rotation messages in C++

查看:156
本文介绍了如何在C ++中获取JoyStick Z旋转消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C ++代码可以正确截断我的Saitek Cyborg游戏杆的游戏杆x和y位置。

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.

对于x和y,消息ID为MM_JOY1MOVE和MM_JOY2MOVE,它们使我能够捕获操纵杆的x和y位置。

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.

对于z轴,有MM_JOY1ZMOVE和MM_JOY2ZMOVE,但没有一个捕获扭曲运动。

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

在mmsystem.h中,找到以下定义:

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

但是这些都不起作用。

问题是:z扭曲运动的messageID是什么,或者我该怎么办?找到它了?

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

推荐答案

不知道Z扭曲的含义(如果是模拟轴或只是 POV 按钮),但我可以使用 JOYINFOEX JOYCAPS 处理操纵杆,如下所示:

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

用法:

// 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 按钮+您所有的按钮想要...

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

已经为 Win2000 编写了它,但仍然可以在 Win7 x64 中使用我的4轴+ POV + 7个按钮Genius游戏杆:)(多年以来一直没有拿起灰尘)

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。

这确实是旧的源代码,因此我需要添加此 bds 2006 C隐藏内存管理器与之发生冲突,只是为了确定。

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.

这篇关于如何在C ++中获取JoyStick Z旋转消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆