Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static List <Joystick> GetJoysticks()
        {
            List <Joystick> ret = new List <Joystick>();

            JoystickNative.JOYCAPS tmpJoyCaps = new JoystickNative.JOYCAPS();

            uint joystick_device_count = JoystickNative.joyGetNumDevs();

            for (int i = 0; i < joystick_device_count; i++)
            {
                if (JoystickNative.joyGetDevCaps((UIntPtr)i, ref tmpJoyCaps, (uint)Marshal.SizeOf(tmpJoyCaps)) == JoystickNative.JOYERR_NOERROR) // Get joystick info
                {
                    JoystickNative.JOYINFO joyinfo = new JoystickNative.JOYINFO();
                    if (JoystickNative.joyGetPos((uint)i, ref joyinfo) == JoystickNative.JOYERR_NOERROR)
                    {
                        ret.Add(new Joystick(i));
                    }
                }
            }

            Joystick no_joy = new Joystick(-1);

            ret.Add(no_joy);

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        public Joystick(int in_joy_index)
        {
            if (in_joy_index < 0)
            {
                ID   = JoystickNative.joyGetNumDevs();
                Name = "";

                HasZ      = false;
                HasR      = false;
                HasU      = false;
                HasV      = false;
                HasPOV    = false;
                IsPOV4DIR = false;
                IsPOVCTS  = false;

                NumberOfAxes    = 0;
                NumberOfButtons = 0;

                Description = "[none]";
            }
            else
            {
                JoystickNative.JOYCAPS joyCaps = new JoystickNative.JOYCAPS();

                if (JoystickNative.joyGetDevCaps((UIntPtr)in_joy_index, ref joyCaps, (uint)Marshal.SizeOf(joyCaps)) != 0) //Get joystick info
                {
                    throw new Exception("Joystick is not ready.");
                }

                ID   = (uint)in_joy_index;
                Name = joyCaps.szPname;

                HasZ      = (joyCaps.wCaps & JoystickNative.JOYCAPS_HASZ) != 0;
                HasR      = (joyCaps.wCaps & JoystickNative.JOYCAPS_HASR) != 0;
                HasU      = (joyCaps.wCaps & JoystickNative.JOYCAPS_HASU) != 0;
                HasV      = (joyCaps.wCaps & JoystickNative.JOYCAPS_HASV) != 0;
                HasPOV    = (joyCaps.wCaps & JoystickNative.JOYCAPS_HASPOV) != 0;
                IsPOV4DIR = (joyCaps.wCaps & JoystickNative.JOYCAPS_POV4DIR) != 0;
                IsPOVCTS  = (joyCaps.wCaps & JoystickNative.JOYCAPS_POVCTS) != 0;

                NumberOfAxes    = joyCaps.wNumAxes;
                NumberOfButtons = joyCaps.wNumButtons;

                // try to get human readable name
                try
                {
                    const string userRoot   = "HKEY_CURRENT_USER";
                    string       key        = string.Format("{0}\\System\\CurrentControlSet\\Control\\MediaResources\\Joystick\\{1}\\CurrentJoystickSettings", userRoot, joyCaps.szRegKey);
                    string       value_name = string.Format("Joystick{0}OEMName", in_joy_index + 1);

                    string oem_name = (string)Registry.GetValue(key, value_name, "");

                    key         = string.Format("{0}\\System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Joystick\\OEM\\{1}", userRoot, oem_name);
                    Description = (string)Registry.GetValue(key, "OEMName", "");
                }
                catch
                {
                    Description = "Game Controller #" + (in_joy_index + 1).ToString();
                }
            }
        }