Exemplo n.º 1
1
        public Joystick[] GetSticks()
        {
            List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Creates a joystick for each game device in USB Ports
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    // Gets the joysticks properties and sets the range for them.
                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                    }

                    // Adds how ever many joysticks are connected to the computer into the sticks list.
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            Console.WriteLine(sticks.Count);
            return sticks.ToArray();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Найти все устройства типа джойстик
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            try
            {
                _quitThread = false;
                // create a device from this controller.
                _joystick = new SlimDX.DirectInput.Joystick(_directInput, _deviceInstance.InstanceGuid);

                foreach (var doi in _joystick.GetObjects(ObjectDeviceType.Axis))
                {
                    _joystick.GetObjectPropertiesById((int)doi.ObjectType).SetRange(MinimumAxisValue, MaximumAxisValue);
                }
                _joystick.Properties.AxisMode = DeviceAxisMode.Absolute;

                _joystick.SetCooperativeLevel(IntPtr.Zero, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
                // Tell DirectX that this is a Joystick.
                //            _joystick.SetDataFormat(DeviceDataFormat.Joystick);

                _joystickThread = new Thread(GetJoystickData);
                _joystick.SetNotification(_joystickEvent);
                // Finally, acquire the device.
                _buttons = new bool[_joystick.GetObjects(ObjectDeviceType.Button).Count];
                _axis    = new int[/*_joystick.GetObjects(ObjectDeviceType.Axis).Count+3*/ 128];
                //            PointOfView = new int[caps.NumberPointOfViews];
                _joystick.Acquire();

                _joystickThread.Start();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        public Joystick()
        {
            var di = new DirectInput();

            // Get the first device
            try
            {
                var device = di.GetDevices()[0];
                joystick = new SlimDX.DirectInput.Joystick(di, device.InstanceGuid);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("No devices: {0}", e.Message);
                Environment.Exit(1);
            }
            catch (DirectInputException e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

            foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
            }

            joystick.Acquire();
        }
Exemplo n.º 4
0
    //initializing connected joysticks and collecting them into a Jaystick-array
    //ref.: Chris Charitidis https://www.youtube.com/watch?v=rtnLGfAj7W0
    public static TJoystick[] GetSticks()
    {
        TJoystick.JOYSTICKNUM = 0;          //Reset joystick counter
        DirectInput input = new DirectInput();

        //For each connected device of type GameController, get the device and add it to a list of joysticks
        List <TJoystick> temp_sticks = new List <TJoystick>();

        foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
        {
            try
            {
                //get joystick device
                Joystick stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                stick.Acquire();


                //set max and min values of each axis
                foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                {
                    if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    {
                        stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-32768, 32767);
                    }
                }

                //add joystick to list
                TJoystick tstick = new TJoystick(stick);
                temp_sticks.Add(tstick);
            } catch (DirectInputException) { throw; }
        }

        return(temp_sticks.ToArray());
    }
Exemplo n.º 5
0
        /// <summary>
        /// Získání prvního gamepadu, ketrý je připojený k počítači
        /// </summary>
        /// <returns>zařízení</returns>
        /// <exception>Pokud se nepodařilo najít žádné funkční zařízení</exception>
        private SlimDX.DirectInput.Joystick getGamepad()
        {
            dinput = new DirectInput();
            SlimDX.DirectInput.Joystick gamepad;
            string errorMessage = "Nebylo nalezeno žádné vnější ovládací zařízení.";

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    gamepad = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    gamepad.Acquire();

                    foreach (DeviceObjectInstance deviceObject in gamepad.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            gamepad.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    return gamepad;
                }
                catch (DirectInputException e)
                {
                    dinput.Dispose();
                    errorMessage = e.Message;
                }
            }
            dinput.Dispose();
            throw new DirectInputException(errorMessage);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Získání prvního gamepadu, ketrý je připojený k počítači
        /// </summary>
        /// <returns>zařízení</returns>
        /// <exception>Pokud se nepodařilo najít žádné funkční zařízení</exception>
        private SlimDX.DirectInput.Joystick getGamepad()
        {
            dinput = new DirectInput();
            SlimDX.DirectInput.Joystick gamepad;
            string errorMessage = "Nebylo nalezeno žádné vnější ovládací zařízení.";

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    gamepad = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    gamepad.Acquire();

                    foreach (DeviceObjectInstance deviceObject in gamepad.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            gamepad.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    return(gamepad);
                }
                catch (DirectInputException e)
                {
                    dinput.Dispose();
                    errorMessage = e.Message;
                }
            }
            dinput.Dispose();
            throw new DirectInputException(errorMessage);
        }
Exemplo n.º 7
0
        public Joystick[] GetWheels()
        {
            List <SlimDX.DirectInput.Joystick> wheels = new List <Joystick>();

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    wheel = new Joystick(Input, device.InstanceGuid);
                    wheel.Acquire();
                    foreach (DeviceObjectInstance deviceObject in wheel.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            wheel.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 0);
                        }
                    }
                    wheels.Add(wheel);
                }
                catch (DirectInputException)
                {
                }
            }
            return(wheels.ToArray());
        }
Exemplo n.º 8
0
        private Joystick[] obtenerDispositivos()
        {
            var         sticks = new List <SlimDX.DirectInput.Joystick>();
            DirectInput dinput = new DirectInput();

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // crear los dispositivos
                try
                {
                    var stick = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                        }
                    }

                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            return(sticks.ToArray());
        }
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Creates a joystick for each game device in USB Ports
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    // Gets the joysticks properties and sets the range for them.
                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }

                    // Adds how ever many joysticks are connected to the computer into the sticks list.
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            Console.WriteLine(sticks.Count);
            return(sticks.ToArray());
        }
Exemplo n.º 10
0
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in myInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    myStick = new SlimDX.DirectInput.Joystick(myInput, device.InstanceGuid);
                    myStick.Acquire();
                    label16.Text   = "Connected";
                    panel1.Enabled = true;
                    foreach (DeviceObjectInstance deviceObject in myStick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            myStick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(Properties.Settings.Default.DataRangeStarts, Properties.Settings.Default.DataRangeEnds);
                        }
                    }
                    sticks.Add(myStick);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            rangeStart = Properties.Settings.Default.DataRangeStarts;
            rangeEnd   = Properties.Settings.Default.DataRangeEnds;
            return(sticks.ToArray());
        }
Exemplo n.º 11
0
        private IEnumerable <IInputInput> _GetInputs(SlimDX.DirectInput.Joystick device)
        {
            foreach (DeviceObjectInstance deviceObjectInstance in device.GetObjects())
            {
                JoystickInput input = null;

                if (_IsButton(deviceObjectInstance))
                {
                    input = new Button(deviceObjectInstance.Name, deviceObjectInstance.Usage);
                }
                else if (_IsAxis(deviceObjectInstance))
                {
                    input = _IsRotationAxis(deviceObjectInstance) ?
                            _CreateRotationAxis(deviceObjectInstance) :
                            _CreateAxis(deviceObjectInstance);
                }
                else if (_IsPov(deviceObjectInstance))
                {
                    input = new Pov(deviceObjectInstance.Name, deviceObjectInstance.DesignatorIndex);
                }

                if (input != null)
                {
                    yield return(input);
                }
            }
        }
Exemplo n.º 12
0
        //=============================================================================================================
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                    textBox_diagnostics.Text = "Nie wykryto Joysticka";
                }
            }
            return(sticks.ToArray());
        }
        public SlimDX.DirectInput.Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> Sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    Sticks.Add(stick);
                    lbJoystick.Text       = "Joystick (Connected)";
                    joystickTimer.Enabled = true;
                }
                catch (DirectInputException ex)
                {
                    lbJoystick.Text = "Joystick (No Connected)";
                }
            }
            return(Sticks.ToArray());
        }
Exemplo n.º 14
0
        //initializing connected joysticks and collecting them into a Jaystick-array
        //WTF mate (Chris Charitidis https://www.youtube.com/watch?v=rtnLGfAj7W0)
        public Joystick[] getSticks()
        {
            List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>();
            foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            //stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }

                    sticks.Add(stick);

                }
                catch (DirectInputException)
                {
                    throw;
                }

            }

            return sticks.ToArray();

        }
Exemplo n.º 15
0
        public DirectInput_Device(DirectInput directInput, DeviceInstance device)
        {
            this.StatusIcon    = Properties.Resources.GenericGamepad.ToImageSource();
            this.directInput   = directInput;
            this.device        = device;
            this.deviceWrapper = new DIdevice();
            gamepad            = new SlimDX.DirectInput.Joystick(directInput, device.InstanceGuid);

            this.DeviceName = gamepad.Information.ProductName + "(" + device.InstanceGuid + ")";
            gamepad.Acquire();


            foreach (DeviceObjectInstance deviceObject in gamepad.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                {
                    gamepad.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                }
            }

            foreach (var property in deviceWrapper.GetType().GetProperties())
            {
                PropertyInfo propertyS = deviceWrapper.GetType().GetProperty(property.Name);
                var          value     = property.GetValue(deviceWrapper, null);
                this.Channels.Add(value as DeviceChannel);
            }

            poolingThread = new Thread(ListenerThread);
            poolingThread.Start();
        }
Exemplo n.º 16
0
        private Joystick[] obtenerDispositivos()
        {
            var sticks = new List<SlimDX.DirectInput.Joystick>();
            DirectInput dinput = new DirectInput();
            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // crear los dispositivos
                try
                {
                    var stick = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }

                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            return sticks.ToArray();
        }
Exemplo n.º 17
0
        private static bool CreateController()
        {
            if (DXJoystick != null)
            {
                return(true);
            }
            int num = 1;

            foreach (var device in DirectInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                if (num == 1)
                {
                    DXJoystick = new DXJoystick(DirectInput, device.InstanceGuid);
                    Application.Current.Dispatcher.Invoke(() => {
                        try
                        {
                            DXJoystick.SetCooperativeLevel(new WindowInteropHelper(Application.Current.MainWindow).Handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
                        }
                        catch { }
                    });
                    break;
                }
                ++num;
            }
            if (DXJoystick != null)
            {
                foreach (var deviceObjectInstance in DXJoystick.GetObjects())
                {
                    if ((uint)(deviceObjectInstance.ObjectType & ObjectDeviceType.Axis) > 0U)
                    {
                        DXJoystick.GetObjectPropertiesById((int)deviceObjectInstance.ObjectType).SetRange(-1000, 1000);
                    }
                }
            }
            return(DXJoystick != null);
        }
        /// <summary>Gets a list of the attached joysticks.</summary>
        /// <returns>A list of joysticks.</returns>
        private List<DI.Joystick> GetAttachedJoysticks()
        {
            List<DI.Joystick> joysticks = new List<DI.Joystick>();
            foreach(DI.DeviceInstance device in _directInput.GetDevices(DI.DeviceClass.GameController, DI.DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    DI.Joystick joystick = new DI.Joystick(_directInput, device.InstanceGuid);
                    joystick.Acquire();

                    IList<DI.DeviceObjectInstance> deviceObjects = joystick.GetObjects();
                    for(int i = 0; i < deviceObjects.Count; i++)
                    {
                        DI.DeviceObjectInstance deviceObjectInstance = deviceObjects[i];

                        if((deviceObjectInstance.ObjectType & DI.ObjectDeviceType.Axis) != 0)
                            joystick.GetObjectPropertiesById((int) deviceObjectInstance.ObjectType).SetRange(-1000, 1000);
                    }

                    joysticks.Add(joystick);
                }
                catch(DI.DirectInputException)
                {
                }
            }
            return joysticks;
        }
Exemplo n.º 19
0
        public void Connect(int index)
        {
            List<DeviceInstance> devices = new List<DeviceInstance>();

            Declare();
            dinput = new DirectInput();

            //Get connected devices
            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                devices.Add(device);
            }

            if (devices.Count() > 0)
            {
                try
                {
                    //Connect to selected device
                    joystick = new SlimDX.DirectInput.Joystick(dinput, devices[index].InstanceGuid);
                    foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }
                    joystick.Acquire();
                    Connected = true;
                }
                catch (DirectInputException)
                {
                    Connected = false;
                }
                //Get data from selected gamepad
                ThreadTransmit = new Thread(delegate()
                {
                    while (Connected)
                    {
                        Thread.Sleep(50);
                        GetInput();
                    }
                });
                ThreadTransmit.Start();
            }
        }