以从用C#.NET游戏杆输入 [英] Taking input from a joystick with C# .NET
本文介绍了以从用C#.NET游戏杆输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我四处搜索在谷歌,但我想出的唯一的东西是过时的,没有工作。
I searched around on Google for this, but the only things I came up with were outdated and did not work.
有没有人对如何使用C#.NET获取游戏杆数据的任何信息?
Does anyone have any information on how to get joystick data using C# .NET?
推荐答案
一:使用 SlimDX
二:它看起来像这样(其中 GamepadDevice
是我自己的包装,和code被精简下来,只是相关的部分)。
Two: it looks something like this (where GamepadDevice
is my own wrapper, and the code is slimmed down to just the relevant parts).
查找操纵杆/垫的GUID:
Find the joystick / pad GUIDs:
public virtual IList<GamepadDevice> Available()
{
IList<GamepadDevice> result = new List<GamepadDevice>();
DirectInput dinput = new DirectInput();
foreach (DeviceInstance di in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
GamepadDevice dev = new GamepadDevice();
dev.Guid = di.InstanceGuid;
dev.Name = di.InstanceName;
result.Add(dev);
}
return result;
}
在用户已经从列表中选择,获得了手柄
Once the user has selected from the list, acquire the gamepad:
private void acquire(System.Windows.Forms.Form parent)
{
DirectInput dinput = new DirectInput();
pad = new Joystick(dinput, this.Device.Guid);
foreach (DeviceObjectInstance doi in pad.GetObjects(ObjectDeviceType.Axis))
{
pad.GetObjectPropertiesById((int)doi.ObjectType).SetRange(-5000, 5000);
}
pad.Properties.AxisMode = DeviceAxisMode.Absolute;
pad.SetCooperativeLevel(parent, (CooperativeLevel.Nonexclusive | CooperativeLevel.Background));
pad.Acquire();
}
轮询垫看起来是这样的:
Polling the pad looks like this:
JoystickState state = new JoystickState();
if (pad.Poll().IsFailure)
{
result.Disconnect = true;
return result;
}
if (pad.GetCurrentState(ref state).IsFailure)
{
result.Disconnect = true;
return result;
}
result.X = state.X / 5000.0f;
result.Y = state.Y / 5000.0f;
int ispressed = 0;
bool[] buttons = state.GetButtons();
这篇关于以从用C#.NET游戏杆输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文