在windows中控制游戏设备有现成的通用接口DirectX,不过我今天说的是托管代码的编程方法,不光是C#, VB.net也是适用的。
首先确保你的操作系统中有DirectX 9 ,在新建项目中添加引用,添加Microsoft.DirectX, Microsoft.DirectX.DirectInput, 添加完成后就可以编写代码了。
首先罗列出所有的设备,下面的例子用树罗列出了计算机中所有的设备,如果你的手柄连接到了计算机上,你可以看看目录中有没有手柄的名字。
private
void
PopulateAllDevices(TreeView tvDevices)
...
{
//Add "All Devices" node to TreeView
TreeNode allNode = new TreeNode("All Devices");
tvDevices.Nodes.Add(allNode);
//Populate All devices
foreach(DeviceInstance di in Manager.Devices)
...{

//Get Device name
TreeNode nameNode = new TreeNode(di.InstanceName);
//Is device attached?
TreeNode attachedNode = new TreeNode(
"Attached = " +
Manager.GetDeviceAttached(di.ProductGuid));
//Get device Guid
TreeNode guidNode = new TreeNode(
"Guid = " + di.InstanceGuid);
//Add nodes
nameNode.Nodes.Add(attachedNode);
nameNode.Nodes.Add(guidNode);
allNode.Nodes.Add(nameNode);
}
}
上面的例子是罗列出所有设备,如果你只想罗列手柄的话
private
void
PopulateKeyboards(TreeView tvDevices)
...
{
//Add "Keyboard Devices" node to TreeView
TreeNode keyboardNode = new TreeNode("Keyboard Devices");
tvInputDevices.Nodes.Add(keyboardNode);
//Populate Attached Keyboards
foreach(DeviceInstance di in
Manager.GetDevices(DeviceType.Joystick,EnumDevicesFlags.AttachedOnly))

这篇博客介绍了如何在Windows环境下,通过C#和DirectX进行游戏设备的控制,包括列举所有设备、筛选手柄设备、设置设备反馈以及配置设备工作状态。示例代码来源于DirectX SDK,并建议读者查阅相关文档以获取更详细信息。

3581

被折叠的 条评论
为什么被折叠?



