资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
前言这是我的第一篇技术博客,希望能为大家提供些有用信息。概述前段时间有一项工作是通过游戏手柄控制云台,通过google 和 msdn 搞定了,在这吧就总结一下吧。问题对于手柄控制主要有以下几个问题在合适的时机获取游戏手柄的输入?一般游戏手柄都是有n 个方向键和n 个功能键控制的,要确认各个键的状态?解决方案通过 windows API 中与游戏手柄相关的函数进行控制,主要是 joystick 系统函数, 这一问题的关键点是P/Invoke 调用。优点是不需要directx 支持,缺点是调用如果要想让多个窗口接受手柄输入有点麻烦(后边会详细描述)。通过 directx 的 directx input , Microsoft.DirectX.DirectInput命名空间下的类对joystick 进行了很好的封装, 在.net 中用时很方便。 优点使用方便, 缺点需要directx 支持不过这点应该不用担心,现在大部分系统安装完后应该都安装了directx 在本篇中将先介绍通过directxinput 进行控制,这篇文章有些地方借鉴了codepoject 上的文章点击这里可以看到程序最终运行效果1.获取系统中已经连接成功的游戏手柄列表,代码如下view plaincopy to clipboardprint? public static string FindJoysticks(IntPtr hWnd) string systemJoysticks = null; try 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 13 页 - - - - - - - - - / 查找连接成功的游戏设备DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); / 遍历列表并获取设备名称if (gameControllerList.Count 0) systemJoysticks = new stringgameControllerList.Count; int i = 0; foreach (DeviceInstance deviceInstance in gameControllerList) / 创建一个Device 对像并获取Device joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); systemJoysticksi = joystickDevice.DeviceInformation.InstanceName; i+; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 13 页 - - - - - - - - - catch (Exception err) Debug.WriteLine(FindJoysticks(); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); return systemJoysticks; public static string FindJoysticks(IntPtr hWnd) string systemJoysticks = null; try / 查找连接成功的游戏设备DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); / 遍历列表并获取设备名称if (gameControllerList.Count 0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 13 页 - - - - - - - - - systemJoysticks = new stringgameControllerList.Count; int i = 0; foreach (DeviceInstance deviceInstance in gameControllerList) / 创建一个Device 对像并获取Device joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); systemJoysticksi = joystickDevice.DeviceInformation.InstanceName; i+; catch (Exception err) Debug.WriteLine(FindJoysticks(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 13 页 - - - - - - - - - Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); return systemJoysticks; view plaincopy to clipboardprint? view plaincopy to clipboardprint? 2.连接到手柄设备开始获取手柄输入2.连接到手柄设备开始获取手柄输入view plaincopy to clipboardprint? public bool AcquireJoystick(string name) try DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); int i = 0; bool found = false; / 查找指定名称的设备并连接到指定的窗口foreach (DeviceInstance deviceInstance in gameControllerList) if (deviceInstance.InstanceName = name) found = true; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 13 页 - - - - - - - - - / 创建一个Device 对象用来管理一个joystick joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; i+; if (!found) return false; /告诉 directx 要控制的是一个Joystick joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); joystickDevice.Acquire(); DeviceCaps cps = joystickDevice.Caps; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 13 页 - - - - - - - - - Debug.WriteLine(Joystick Axis: + cps.NumberAxes); Debug.WriteLine(Joystick Buttons: + cps.NumberButtons); UpdateStatus(); catch (Exception err) Debug.WriteLine(FindJoysticks(); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); return false; return true; view plaincopy to clipboardprint?public bool AcquireJoystick(string name) try DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); int i = 0; bool found = false; / 查找指定名称的设备并连接到指定的窗口foreach (DeviceInstance deviceInstance in gameControllerList) if (deviceInstance.InstanceName = name) found = true; / 创建一个 Device 对象用来管理一个joystick joystickDevice = new Device(deviceInstance.InstanceGuid); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 13 页 - - - - - - - - - joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; i+; if (!found) return false; /告诉directx 要控制的是一个Joystick joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); joystickDevice.Acquire(); DeviceCaps cps = joystickDevice.Caps; Debug.WriteLine(Joystick Axis: + cps.NumberAxes); Debug.WriteLine(Joystick Buttons: + cps.NumberButtons); UpdateStatus(); catch (Exception err) Debug.WriteLine(FindJoysticks(); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); return false; return true; public bool AcquireJoystick(string name) try DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); int i = 0; bool found = false; / 查找指定名称的设备并连接到指定的窗口foreach (DeviceInstance deviceInstance in gameControllerList) if (deviceInstance.InstanceName = name) found = true; / 创建一个Device 对象用来管理一个joystick 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 13 页 - - - - - - - - - joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; i+; if (!found) return false; /告诉 directx 要控制的是一个Joystick joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); joystickDevice.Acquire(); DeviceCaps cps = joystickDevice.Caps; Debug.WriteLine(Joystick Axis: + cps.NumberAxes); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 13 页 - - - - - - - - - Debug.WriteLine(Joystick Buttons: + cps.NumberButtons); UpdateStatus(); catch (Exception err) Debug.WriteLine(FindJoysticks(); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); return false; return true; view plaincopy to clipboardprint? 3.获取手柄状态并管理按键的状态上放开还是按下等信息,由于从手柄获取到的信息只含方向和那个键是按下的所以要想获取某个键是否按下或弹起要进行一些处理,下面的程序逻缉大家看下应该会懂的。3.获取手柄状态并管理按键的状态上放开还是按下等信息,由于从手柄获取到的信息只含方向和那个键是按下的所以要想获取某个键是否按下或弹起要进行一些处理,下面的程序逻缉大家看下应该会懂的。view plaincopy to clipboardprint? public void UpdateStatus() Poll(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 13 页 - - - - - - - - - int extraAxis = state.GetSlider(); /获取方向键的状态,并区分左右上下以及按键的按下与放开if (state.X = 0) m_joyMove.Direction = JoyDirection.LEFT; m_joyMove.Event = JoyEvent.WM_DOWN; else if (state.Y = 0) m_joyMove.Direction = JoyDirection.UP; m_joyMove.Event = JoyEvent.WM_DOWN; else if (state.X = 65535) m_joyMove.Direction = JoyDirection.RIGHT; m_joyMove.Event = JoyEvent.WM_DOWN; else if (state.Y = 65535) m_joyMove.Direction = JoyDirection.DOWN; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 13 页 - - - - - - - - - m_joyMove.Event = JoyEvent.WM_DOWN; else if (state.X = 32511 & (m_joyMove.Direction = JoyDirection.LEFT | m_joyMove.Direction = JoyDirection.RIGHT) & m_joyMove.Event = JoyEvent.WM_DOWN) m_joyMove.Event = JoyEvent.WM_UP; else if (state.Y = 32511 & (m_joyMove.Direction = JoyDirection.UP | m_joyMove.Direction = JoyDirection.DOWN) & m_joyMove.Event = JoyEvent.WM_DOWN) m_joyMove.Event = JoyEvent.WM_UP; else m_joyMove.Direction = JoyDirection.NONE; m_joyMove.Event = JoyEvent.NONE; / 获取按键的状态byte jsButtons = state.GetButtons(); if (jsButtons != null) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 13 页 - - - - - - - - - if (m_JoyButtons = null) m_JoyButtons = new JoyEventjsButtons.Length; for (int i = 0; i = 128) m_JoyButtonsi = JoyEvent.WM_DOWN; else if (m_JoyButtonsi = JoyEvent.WM_DOWN) m_JoyButtonsi = JoyEvent.WM_UP; else m_JoyButtonsi = JoyEvent.NONE; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 13 页 - - - - - - - - -
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号