2022年手柄控制

上传人:工**** 文档编号:567367598 上传时间:2024-07-20 格式:PDF 页数:13 大小:65.83KB
返回 下载 相关 举报
2022年手柄控制_第1页
第1页 / 共13页
2022年手柄控制_第2页
第2页 / 共13页
2022年手柄控制_第3页
第3页 / 共13页
2022年手柄控制_第4页
第4页 / 共13页
2022年手柄控制_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《2022年手柄控制》由会员分享,可在线阅读,更多相关《2022年手柄控制(13页珍藏版)》请在金锄头文库上搜索。

1、前言这是我的第一篇技术博客,希望能为大家提供些有用信息。概述前段时间有一项工作是通过游戏手柄控制云台,通过google 和 msdn 搞定了,在这吧就总结一下吧。问题对于手柄控制主要有以下几个问题在合适的时机获取游戏手柄的输入?一般游戏手柄都是有n 个方向键和n 个功能键控制的,要确认各个键的状态?解决方案通过 windows API 中与游戏手柄相关的函数进行控制,主要是 joystick 系统函数, 这一问题的关键点是P/Invoke 调用。优点是不需要directx 支持,缺点是调用如果要想让多个窗口接受手柄输入有点麻烦(后边会详细描述)。通过 directx 的 directx inp

2、ut , Microsoft.DirectX.DirectInput命名空间下的类对joystick 进行了很好的封装, 在.net 中用时很方便。 优点使用方便, 缺点需要directx 支持不过这点应该不用担心,现在大部分系统安装完后应该都安装了directx 在本篇中将先介绍通过directxinput 进行控制,这篇文章有些地方借鉴了codepoject 上的文章点击这里可以看到程序最终运行效果1.获取系统中已经连接成功的游戏手柄列表,代码如下view plaincopy to clipboardprint? public static string FindJoysticks(Int

3、Ptr hWnd) string systemJoysticks = null; try 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 13 页 - - - - - - - - - / 查找连接成功的游戏设备DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); / 遍历列表并获取设备名称if (gameCont

4、rollerList.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.Backgro

5、und | CooperativeLevelFlags.NonExclusive); systemJoysticksi = joystickDevice.DeviceInformation.InstanceName; i+; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 13 页 - - - - - - - - - catch (Exception err) Debug.WriteLine(FindJoysticks(); Debug.WriteLine(err.Mes

6、sage); 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 (gameControllerL

7、ist.Count 0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 13 页 - - - - - - - - - systemJoysticks = new stringgameControllerList.Count; int i = 0; foreach (DeviceInstance deviceInstance in gameControllerList) / 创建一个Device 对像并获取Device joystickDevice = new Devic

8、e(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); systemJoysticksi = joystickDevice.DeviceInformation.InstanceName; i+; catch (Exception err) Debug.WriteLine(FindJoysticks(); 名师资料总结 - - -精品资料欢迎下载 - - - - -

9、 - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 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

10、? 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

11、= name) found = true; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 13 页 - - - - - - - - - / 创建一个Device 对象用来管理一个joystick joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | Coope

12、rativeLevelFlags.NonExclusive); break; i+; if (!found) return false; /告诉 directx 要控制的是一个Joystick joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); joystickDevice.Acquire(); DeviceCaps cps = joystickDevice.Caps; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6

13、页,共 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 p

14、laincopy 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 (dev

15、iceInstance.InstanceName = name) found = true; / 创建一个 Device 对象用来管理一个joystick joystickDevice = new Device(deviceInstance.InstanceGuid); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 13 页 - - - - - - - - - joystickDevice.SetCooperativeLevel(hWnd, CooperativeLev

16、elFlags.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.W

17、riteLine(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.GetDe

18、vices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); int i = 0; bool found = false; / 查找指定名称的设备并连接到指定的窗口foreach (DeviceInstance deviceInstance in gameControllerList) if (deviceInstance.InstanceName = name) found = true; / 创建一个Device 对象用来管理一个joystick 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - -

19、 - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 13 页 - - - - - - - - - joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; i+; if (!found) return false; /告诉 directx 要控制的是一个Joys

20、tick joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); joystickDevice.Acquire(); DeviceCaps cps = joystickDevice.Caps; Debug.WriteLine(Joystick Axis: + cps.NumberAxes); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 13 页 - - - - - - - - - Debug.WriteLine

21、(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.获取手柄状态并管理按键的状态上放开还是按下等信息,由于从手柄获取到的信息只含方向和那个键是按下的所以要想获取某个键是否按下或

22、弹起要进行一些处理,下面的程序逻缉大家看下应该会懂的。3.获取手柄状态并管理按键的状态上放开还是按下等信息,由于从手柄获取到的信息只含方向和那个键是按下的所以要想获取某个键是否按下或弹起要进行一些处理,下面的程序逻缉大家看下应该会懂的。view plaincopy to clipboardprint? public void UpdateStatus() Poll(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 13 页 - - - - - - - - - int

23、 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 = Jo

24、yDirection.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_joy

25、Move.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_

26、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_JoyB

27、uttons = 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 页 - - - - - - - - -

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号