/****************************************************************************** * C# Joystick Library - Copyright (c) 2006 Mark Harris - MarkH@rris.com.au ****************************************************************************** * You may use this library in your application, however please do give credit * to me for writing it and supplying it. If you modify this library you must * leave this notice at the top of this file. I'd love to see any changes you * do make, so please email them to me :) *****************************************************************************/ using System; using System.Collections.Generic; using System.Text; using Microsoft.DirectX.DirectInput; using System.Diagnostics; namespace JoystickInterface { /// /// Class to interface with a joystick device. /// public class Joystick { private Device joystickDevice; private JoystickState state; private int axisCount; /// /// Number of axes on the joystick. /// public int AxisCount { get { return axisCount; } } private int axisA; /// /// The first axis on the joystick. /// public int AxisA { get { return axisA; } } private int axisB; /// /// The second axis on the joystick. /// public int AxisB { get { return axisB; } } private int axisC; /// /// The third axis on the joystick. /// public int AxisC { get { return axisC; } } private int axisD; /// /// The fourth axis on the joystick. /// public int AxisD { get { return axisD; } } private int axisE; /// /// The fifth axis on the joystick. /// public int AxisE { get { return axisE; } } private int axisF; /// /// The sixth axis on the joystick. /// public int AxisF { get { return axisF; } } private IntPtr hWnd; private bool[] buttons; /// /// Array of buttons availiable on the joystick. This also includes PoV hats. /// public bool[] Buttons { get { return buttons; } } private string[] systemJoysticks; /// /// Constructor for the class. /// /// Handle of the window which the joystick will be "attached" to. public Joystick(IntPtr window_handle) { hWnd = window_handle; axisA = -1; axisB = -1; axisC = -1; axisD = -1; axisE = -1; axisF = -1; axisCount = 0; } private void Poll() { try { // poll the joystick joystickDevice.Poll(); // update the joystick state field state = joystickDevice.CurrentJoystickState; } catch (Exception err) { // we probably lost connection to the joystick // was it unplugged or locked by another application? Debug.WriteLine("Poll()"); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); } } /// /// Retrieves a list of joysticks attached to the computer. /// /// /// [C#] /// /// JoystickInterface.Joystick jst = new JoystickInterface.Joystick(this.Handle); /// string[] sticks = jst.FindJoysticks(); /// /// /// A list of joysticks as an array of strings. public string[] FindJoysticks() { systemJoysticks = null; try { // Find all the GameControl devices that are attached. DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); // check that we have at least one device. if (gameControllerList.Count > 0) { systemJoysticks = new string[gameControllerList.Count]; int i = 0; // loop through the devices. foreach (DeviceInstance deviceInstance in gameControllerList) { // create a device from this controller so we can retrieve info. joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); systemJoysticks[i] = joystickDevice.DeviceInformation.InstanceName; i++; } } } catch (Exception err) { Debug.WriteLine("FindJoysticks()"); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); } return systemJoysticks; } /// /// Acquire the named joystick. You can find this joystick through the method. /// /// Name of the joystick. /// The success of the connection. public bool AcquireJoystick(string name) { try { DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); int i = 0; bool found = false; // loop through the devices. foreach (DeviceInstance deviceInstance in gameControllerList) { if (deviceInstance.InstanceName == name) { found = true; // create a device from this controller so we can retrieve info. joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; } i++; } if (!found) return false; // Tell DirectX that this is a Joystick. joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); // Finally, acquire the device. joystickDevice.Acquire(); // How many axes? // Find the capabilities of the joystick DeviceCaps cps = joystickDevice.Caps; Debug.WriteLine("Joystick Axis: " + cps.NumberAxes); Debug.WriteLine("Joystick Buttons: " + cps.NumberButtons); axisCount = cps.NumberAxes; UpdateStatus(); } catch (Exception err) { Debug.WriteLine("FindJoysticks()"); Debug.WriteLine(err.Message); Debug.WriteLine(err.StackTrace); return false; } return true; } /// /// Unaquire a joystick releasing it back to the system. /// public void ReleaseJoystick() { joystickDevice.Unacquire(); } /// /// Update the properties of button and axis positions. /// public void UpdateStatus() { Poll(); int[] extraAxis = state.GetSlider(); //Rz Rx X Y Axis1 Axis2 axisA = state.Rz; axisB = state.Rx; axisC = state.X; axisD = state.Y; axisE = extraAxis[0]; axisF = extraAxis[1]; // not using buttons, so don't take the tiny amount of time it takes to get/parse byte[] jsButtons = state.GetButtons(); buttons = new bool[jsButtons.Length]; int i = 0; foreach (byte button in jsButtons) { buttons[i] = button >= 128; i++; } } } }