Detecting the Devices You Can Use

Just like last chapter, you will need to ensure that you add references to DirectInput in order to use the code that will be discussed in this chapter. You will need to add a reference to Microsoft.DirectX.DirectInput as well as add a using clause for this namespace. Once this is done for the project(s), you are ready to begin.

Unless your computer is a server system, mounted inside a "rack" (a headless system), chances are your computer has at a minimum two input devices: a keyboard and a mouse.

With the multitude of USB devices on the market today, though, you may very well have quite a few input devices. It's not uncommon for a laptop, for example, to have multiple mice attached. You could theoretically have upward of 100 different user input devices attached to your system at any given time.

With all of these different input devices available to you, what can you do to detect the various devices available on your system? Do you remember way back early in the book when you talked about the Manager class that exists in Direct3D? DirectInput has a similar class that you can use to find out just about anything you will need to about the devices attached (or even not attached) to your system.

The easiest thing you can do is to detect all of the devices available on the system. You can use the devices property on the manager object to do this enumeration. First though, you'll need to create a tree view on our form, and set its dock parameter to fill so that it covers the entire form. You will also need some constants for the "root" key names you will use for the tree view. Add these constants to your application:

 private const string AllItemsNode = "All Items"; private const string KeyboardsNode = "All Keyboard Items"; private const string MiceNode = "All Mice Items"; private const string GamePadNode = "All Joysticks and Gamepad Items"; private const string FeedbackNode = "All ForceFeedback Items"; 

Based on these constants, you can see that you will need to create five different nodes in the tree: one for all the items on your system, and then one section each for mice, keyboards, joysticks, and force feedback items. Everything in the "All Items" node should be duplicated in other nodes as well.

You should fill this node first. You will create a single function to do them all called "LoadDevices". The only object of this function will be to fill the tree view with the items currently on the system. You should add the method found in Listing 15.1 now.

Listing 15.1 Adding Your Devices to the TreeView
 public void LoadDevices() {     TreeNode allNodes = new TreeNode(AllItemsNode);     // First get all devices     foreach(DeviceInstance di in Manager.Devices)     {         TreeNode newNode = new TreeNode(string.Format("{0} - {1} ({2})",             di.InstanceName, Manager.GetDeviceAttached(di.InstanceGuid)             ? "Attached" : "Detached", di.InstanceGuid));         allNodes.Nodes.Add(newNode);     }     treeView1.Nodes.Add(allNodes); } 

As you can see, the device enumerator class (DeviceList) that is returned from the Devices property returns a list of DeviceInstance structures. This structure contains all of the useful information on the devices, including the instance GUID (to use when creating the device), the product name, and the type of device.

You can also use another of the manager class methods here to check if the device is attached. It is entirely possible to have a device "available" on your system that isn't currently attached. It's important that you only use devices that are attached to your system.

Lastly, you add each device that was found into the parent node, and add that parent node into the tree view itself; all in all, it's quite simple. However, what if you wanted to only look for certain types of devices, for example, only the keyboards that are attached to the system? Add the code in Listing 15.2 directly below your all devices check in LoadDevices.

Listing 15.2 Adding Keyboards to Your TreeView
 // Now get all keyboards TreeNode kbdNodes = new TreeNode(KeyboardsNode); foreach(DeviceInstance di in Manager.GetDevices(DeviceClass.Keyboard,     EnumDevicesFlags.AttachedOnly)) {     TreeNode newNode = new TreeNode(string.Format("{0} - {1} ({2})",         di.InstanceName, Manager.GetDeviceAttached(di.InstanceGuid)         ? "Attached" : "Detached", di.InstanceGuid));     kbdNodes.Nodes.Add(newNode); } treeView1.Nodes.Add(kbdNodes); 

The code here is remarkably similar to the all devices code, with the major exception being the actual enumeration being done. Rather than using the Devices property, you will use a new method on the manager class, GetDevices, which allows you to further specify the types of devices you will want to enumerate. In this case, you want all keyboard objects, and only the ones that are attached to the system. The mice and game pad code is pretty much the same, just using a different device class value:

 // Now get all mice TreeNode miceNodes = new TreeNode(MiceNode); foreach(DeviceInstance di in Manager.GetDevices(DeviceClass.Pointer,     EnumDevicesFlags.AttachedOnly)) {     TreeNode newNode = new TreeNode(string.Format("{0} - {1} ({2})",         di.InstanceName, Manager.GetDeviceAttached(di.InstanceGuid)         ? "Attached" : "Detached", di.InstanceGuid));     miceNodes.Nodes.Add(newNode); } treeView1.Nodes.Add(miceNodes); // Now get all joysticks and gamepads TreeNode gpdNodes = new TreeNode(GamePadNode); foreach(DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl,     EnumDevicesFlags.AllDevices)) {     TreeNode newNode = new TreeNode(string.Format("{0} - {1} ({2})",         di.InstanceName, Manager.GetDeviceAttached(di.InstanceGuid)         ? "Attached" : "Detached", di.InstanceGuid));     gpdNodes.Nodes.Add(newNode); } treeView1.Nodes.Add(gpdNodes); 

Notice that the mouse device class is Pointer? Pointer isn't restricted to mice; it's just the most common type. Screen pointers fall into this category as well. What if you wanted to enumerate only devices with the force feedback ability? It's not specific to any one device type necessarily; it's a feature you want to support. This check is quite simple as well:

 // Now get all Force Feedback items TreeNode ffNodes = new TreeNode(FeedbackNode); foreach(DeviceInstance di in Manager.GetDevices(DeviceClass.All,     EnumDevicesFlags.ForceFeeback)) {     TreeNode newNode = new TreeNode(string.Format("{0} - {1} ({2})",         di.InstanceName, Manager.GetDeviceAttached(di.InstanceGuid)         ? "Attached" : "Detached", di.InstanceGuid));     ffNodes.Nodes.Add(newNode); } treeView1.Nodes.Add(ffNodes); 

The overall structure of the code remains the same; you've just changed the enumeration once more. Now you want to enumerate all device classes, but only those that support force feedback.

OPENING THE CONTROL PANEL

There is also a method on the Manager class called "RunControlPanel" that (much like its name implies) opens the control panel. This is useful for allowing customization of the devices you have on your system.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net