4/02/2012

The source code to get joystick input value(C++,MFC)

My Environment : MS VS 2008 & MFC(Dialog Based)
Joy Stick : Logitech Extreme 3D pro (XBox Type)
Cteated Date : 2012. 03
[source code]





We use dxShow to get joystick input value.
So we need these files "mmsystem.h", "winmm.lib", "winmm.dll". But normally, there are already in your computer.

The method to get value of joystick is very easy.
First, to confirm whether the joystick is connected well or not. And calibrate range by control pannel in window.

Second, cording~
#include "mmsystem.h"

JOYINFOEX joyInfoEx;
 joyInfoEx.dwSize = sizeof(joyInfoEx);
 joyGetDevCaps(JOYSTICKID1, &joyCaps, sizeof(joyCaps));
 JoyPresent = (joyGetPosEx(JOYSTICKID1, &joyInfoEx) == JOYERR_NOERROR);

JOYINFOEX joyInfoEx;
 if (JoyPresent)
 {
  joyInfoEx.dwSize = sizeof(joyInfoEx);
  joyInfoEx.dwFlags = JOY_RETURNALL;
  joyGetPosEx(JOYSTICKID1, &joyInfoEx);
 }

//And you can confirm button click and stick moving value

 if (joyInfoEx.dwButtons & 0x1) //then do whatever
 {
  str.Format("button1");
  m_ListBox2.AddString(str);
 }

 if (joyInfoEx.dwButtons & 0x2) //then do whatever
 {
  str.Format("button2");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x4) //then do whatever
 {
  str.Format("button3");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x8) //then do whatever
 {
  str.Format("button4");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x10) //then do whatever
 {
  str.Format("button5");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x20) //then do whatever
 {
  str.Format("button6");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x40) //then do whatever
 {
  str.Format("button6");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x80) //then do whatever
 {
  str.Format("button6");
  m_ListBox2.AddString(str);
 }


if ( joyInfoEx.dwPOV == 0 )
 {
  str.Format("Up");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 4500){
  str.Format("Up+Right");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 9000){
  str.Format("Right");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 13500){
  str.Format("Down+Right");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 18000){
  str.Format("Down");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 22500){
  str.Format("Down+Left");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 27000){
  str.Format("Left");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 31500){
  str.Format("Up+Left");
  m_ListBox2.AddString(str);
 }if ( joyInfoEx.dwPOV & 0x1 ){
  str.Format("No Input");
  m_ListBox2.AddString(str);
 }

str.Format("X:%d Y:%d Z:%d", joyInfoEx.dwXpos, joyInfoEx.dwYpos, joyInfoEx.dwZpos);  m_ListBox.AddString(str); str.Format("R:%d U:%d V:%d", joyInfoEx.dwRpos, joyInfoEx.dwUpos, joyInfoEx.dwVpos);  m_ListBox3.AddString(str);


Thank you~
Everybody Fighting !!!

No comments:

Post a Comment