Multimedia Joystick Functions

游戏杆编程VC:

记下一个网址:http://msdn.microsoft.com/en-us/library/windows/desktop/dd743591(v=vs.85).aspx

游戏杆编程VB:

http://www.ex-designz.net/apicat.asp?apicat=31


只能读取4个键,3轴

API Explanation 
JOYINFO-type variables hold the current position of a joystick. This structure can store the positions of the x, y, and z axes, as well as the buttons pushed. Note that this structure can only receive information about buttons 1 through 4 on the joystick -- if there are more, they are ignored 

Parameter Information
Type JOYINFO
wXpos As Long
wYpos As Long
wZpos As Long
wButtons As Long
End Type

wXpos 
The current x-axis coordinate. 
wYpos 
The current y-axis coordinate. 
wZpos 
The current z-axis coordinate. 
wButtons 
Zero or more of the following flags, specifying which buttons are being depressed: 
JOY_BUTTON1 = &H1 
Button #1 is depressed. 
JOY_BUTTON2 = &H2 
Button #2 is depressed. 
JOY_BUTTON3 = &H4 
Button #3 is depressed. 
JOY_BUTTON4 = &H8 
Button #4 is depressed.


13556872952 梁 深圳做游戏杆的。既有使用标准做开发的,也有自定义协议的。


Microsoft DirectX SDK (August 2009)中也有游戏手柄的相应例子,具体是

$ \Microsoft DirectX SDK (August 2009)\Samples\C++\DirectInput\Joystick

有个网友的举例:

#include "StdAfx.h"
#include "Joystick.h"
#include "strsafe.h"

extern CString name;
Joystick::Joystick(void)
{
m_lpDIDevice=NULL;
m_lpDI=NULL;
m_hwnd=NULL;
m_hInstance=GetModuleHandle(NULL);//获得实例的句柄
}

Joystick::~Joystick(void)
{
RealseDevice();
}

//初始化设置
BOOL Joystick::Initialise()
{
HRESULT hr;
if (m_lpDI==NULL)
{
hr=DirectInput8Create(m_hInstance,// 实例句柄
DIRECTINPUT_VERSION,//窗口句柄
IID_IDirectInput8,//IDD的引用
(void **)&m_lpDI,//接口取值
NULL);//系统保留,一般为NULL
if (FAILED(hr))
{
OutputDebugString(L"Create失败 -in joystick::Initialise\n");
return false;
}

hr=m_lpDI->EnumDevices(DI8DEVCLASS_POINTER,//DI8DEVCLASS_GAMECTRL,扫描的设备的类型
DIEnumDevicesCallback,//指向回调函数的指针
&joystickGUID,//返回值,存储需要的信息(获得GUID)
DIEDFL_ATTACHEDONLY);//枚举扫描控制标志
if (FAILED(hr))
{
OutputDebugString(L"EnumDevices失败 -in joystick::Initialise\n");
return false;
}
if (!m_lpDIDevice)
{
hr=m_lpDI->CreateDevice(joystickGUID,//设备的GUID
&m_lpDIDevice,//设备接口的地址
NULL);
if (FAILED(hr))
{
OutputDebugString(L"CreateDevice失败 -in joystick::Initialise\n");
return false;
}
}
//设置协作等级—— 前台模式 | 独占模式
hr=m_lpDIDevice->SetCooperativeLevel(m_hwnd,DISCL_FOREGROUND|DISCL_EXCLUSIVE);//DISCL_BACKGROUND,DISCL_FOREGROUND,DISCL_EXCLUSIVE
if (FAILED(hr))
{
OutputDebugString(L"SetCooperativeLevel失败 -in joystick::Initialise\n");
return false;
}
//设置数据格式
hr=m_lpDIDevice->SetDataFormat(&c_dfDIJoystick);//通用游戏杆
//hr=m_lpDIDevice->SetDataFormat(&c_dfDIMouse);//通用鼠标
if (FAILED(hr))
{
OutputDebugString(L"SetDataFormat失败 -in joystick::Initialise\n");
return false;
}
//使用枚举对象函数来设置轴的特性
hr = m_lpDIDevice->EnumObjects(EnumObjectsCallback, (VOID*)this, DIDFT_ALL );
if FAILED(hr)
{
OutputDebugString(L"EnumObjects - in joystick::Initialise\n");
return false;  
}
}
return true;
}
BOOL CALLBACK Joystick::DIEnumDevicesCallback(const DIDEVICEINSTANCE *lpddi, void *pvRef)
{
*(GUID*)pvRef=lpddi->guidInstance;//从这个结构中获得GUID,我们仅仅需要GUID
return DIENUM_STOP;
}
BOOL CALLBACK Joystick::EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdidoi, void *pContext)
{
HRESULT hr;
Joystick * js = (Joystick*)pContext; //首先取得JS对象指针
//设置游戏杆输入特性
if( pdidoi->dwType & DIDFT_AXIS ) //如果枚举的对象为轴
{
DIPROPRANGE diprg; //设置轴范围结构
diprg.diph.dwSize = sizeof(DIPROPRANGE);  
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);  
diprg.diph.dwHow = DIPH_BYID;  
diprg.diph.dwObj = pdidoi->dwType; // 枚举的轴
diprg.lMin = -1024; //最小值
diprg.lMax = +1024; //最大值
// 设置轴范围  
hr = js->m_lpDIDevice->SetProperty( DIPROP_RANGE, &diprg.diph);
if( FAILED(hr))  
{
OutputDebugString(L"设置轴范围失败 - in CDIJoystick::EnumObjectsCallback\n");
return DIENUM_STOP;
}
//设置死区属性,对于有轴的手柄来说
DIPROPDWORD dipdw; //死区结构
dipdw.diph.dwSize = sizeof( dipdw );  
dipdw.diph.dwHeaderSize = sizeof( dipdw.diph );  
diprg.diph.dwObj = pdidoi->dwType; // 枚举的轴
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = 1000; //10%的死区
hr = js->m_lpDIDevice->SetProperty(DIPROP_DEADZONE, &dipdw.diph);
if( FAILED(hr))  
{
OutputDebugString(L"设置死区失败 - in CDIJoystick::EnumObjectsCallback\n");
return DIENUM_STOP;
}
}
//
name=name+pdidoi->tszName+L' ';
return DIENUM_CONTINUE;
 }

HRESULT Joystick::PollDevice(void)
{
HRESULT hr;
if( NULL == m_lpDIDevice ) //未获得设备
return S_OK;
hr = m_lpDIDevice->Poll();// 轮循设备读取当前状态
if( FAILED(hr) )  
{
// 输入流中断,不能通过轮循获得任何状态值。
// 所以不需要任何重置,只要再次获得设备就行。
hr = m_lpDIDevice->Acquire();
/*while( hr == DIERR_INPUTLOST)
{
static int iCount = 0;
if (iCount>30) exit(-1); //累积30次获取设备失败,退出程序。
iCount++;
OutputDebugString(L"丢失设备,轮循失败 - in CJoystick::PollDevice\n");
hr = m_lpDIDevice->Acquire();
if( SUCCEEDED(hr) ) iCount = 0;  
} // hr也许为其他的错误.*/
while( hr == DIERR_INPUTLOST )//输入流中断,不能通过轮循获得任何状态值,循环检测,知道获得设备为止  
hr = m_lpDIDevice->Acquire();
return S_OK;  
}
int *buffer;
buffer=new int[30];
buffer=0;
// 获得输入状态,存储到成员变量 m_dijs 中
if( FAILED( hr = m_lpDIDevice->GetDeviceState( sizeof(DIJOYSTATE), &m_dijs)))
//if( FAILED( hr = m_lpDIDevice->GetDeviceState( sizeof(DIMOUSESTATE), &didod)))
return hr; // 在轮循过程中设备将为 已获得状态
return S_OK;
}

void Joystick::RealseDevice()
{
if (m_lpDIDevice)
{
m_lpDIDevice->Unacquire();// 归还设备
}
if (m_lpDIDevice)
{
m_lpDIDevice->Release();
m_lpDIDevice=NULL;
}
if (m_lpDI)
{
m_lpDI->Release();
m_lpDI=NULL;
}
}
我刚做完的这个,用的手柄是那种没有力反馈的,看看是不是对你有帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值