예제 #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();
        }
예제 #2
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();
        }
예제 #3
0
        public Joystick()
        {
            var di = new DirectInput();

            foreach (var device in di.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    joystick = new SlimDX.DirectInput.Joystick(di, device.InstanceGuid);
                    Connected = true;
                    joystick.RunControlPanel();
                    break;
                }
                catch
                {
                }
            }

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

            if (Connected)
                joystick.Acquire();
        }
예제 #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());
    }
예제 #5
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());
        }
예제 #6
0
        private bool CreateDevice()
        {
            int num = 1;

            foreach (DeviceInstance device in Controller.directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                if (num == (int)CONTROLLERTYPE.Pad1)
                {
                    this.stick = new Joystick(Controller.directInput, device.InstanceGuid);
                    this.stick.SetCooperativeLevel(form, CooperativeLevel.Exclusive | Controller.ctl);
                    break;
                }
                ++num;
            }
            if (this.stick != null)
            {
                foreach (DeviceObjectInstance deviceObjectInstance in this.stick.GetObjects())
                {
                    if ((uint)(deviceObjectInstance.ObjectType & ObjectDeviceType.Axis) > 0U)
                    {
                        this.stick.GetObjectPropertiesById((int)deviceObjectInstance.ObjectType).SetRange(-1000, 1000);
                    }
                }
            }
            return(this.stick != null);
        }
예제 #7
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());
        }
예제 #8
0
 public void poll()
 {
     while (true)
     {
         if (stick != null)
         {
             if (input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly).ToArray().Length > 0)
             {
                 stickHandle(stick);
             }
             else
             {
                 Invoke(new MethodInvoker(delegate
                 {
                     debug.Text += "Controller Disconnected";
                 }));
                 stick  = null;
                 sticks = GetSticks();
             }
         }
         else
         {
             try
             {
                 Invoke(new MethodInvoker(delegate
                 {
                     debug.Text += "Controller Disconnected";
                 }));
                 sticks = GetSticks();
             }
             catch (Exception e) { }
         }
         Thread.Sleep(13);
     }
 }
예제 #9
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();

        }
예제 #10
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);
        }
예제 #11
0
        public InputController(InputCore core, DI.Joystick dev, int index)
        {
            if (core == null)
            {
                throw new ArgumentNullException("core");
            }
            if (dev == null)
            {
                throw new ArgumentNullException("dev");
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", "assertion failed: index >= 0");
            }

            Core   = core;
            Device = dev;

            ApplyExclusivity();
            Device.Acquire();

            ID      = index;
            Buttons = new ButtonActions[ButtonCount];
            Axes    = new AxisActions[AxisCount];

            for (int i = 0, max = Buttons.Length; i < max; ++i)
            {
                Buttons[i] = new ButtonActions(Core, false, i + 1);
            }
            for (int i = 0, max = Axes.Length; i < max; ++i)
            {
                Axes[i] = new AxisActions(Core, false, true, i + 1);
            }
        }
예제 #12
0
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            // List<Microsoft.DirectX.DirectInput.Joystick> mSticks = new List<Microsoft.DirectX.DirectInput.Joystick>();
            foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                    stick.Acquire();
                    sticks.Add(stick);
                    Invoke(new MethodInvoker(delegate
                    {
                        debug.Text = "Controller Connected.";

                        /*foreach (bool btn in buttons) {
                         *  controlOut.Text += (btn ? "t " : "f ") ;
                         * }*/
                    }));
                }
                catch (Exception e) { debug.Text += e.ToString(); }
            }
            return(sticks.ToArray());
        }
예제 #13
0
        private SlimDX.DirectInput.Joystick _CreateDevice()
        {
            SlimDX.DirectInput.Joystick device;

            using (DirectInput directInput = new DirectInput()) {
                // Try to get the device specified.
                DeviceInstance deviceInstance =
                    directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly).FirstOrDefault(
                        x => x.InstanceGuid == DeviceId);

                if (deviceInstance == null)
                {
                    throw new Exception("Specified joystick not found.");
                }

                device = new SlimDX.DirectInput.Joystick(directInput, deviceInstance.InstanceGuid);
                // This needs System.Windows.Forms.Control.
                device.SetCooperativeLevel(IntPtr.Zero, CooperativeLevel.Nonexclusive | CooperativeLevel.Background);
                device.Properties.SetRange((int)Position.MinValue, (int)Position.MaxValue);

                Inputs = _GetInputs(device).ToArray();
            }

            return(device);
        }
예제 #14
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);
                }
            }
        }
        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());
        }
예제 #16
0
파일: Gamepad.cs 프로젝트: Brzobohaty/Robot
        /// <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);
        }
예제 #17
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();
        }
        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());
        }
예제 #19
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);
            }
        }
예제 #20
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());
        }
예제 #21
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());
        }
예제 #22
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();
        }
예제 #23
0
파일: Gamepad.cs 프로젝트: Brzobohaty/Robot
 /// <summary>
 /// Inicializace gamepad
 /// </summary>
 /// <returns>true pokud se inicializace povedla</returns>
 public override bool inicialize()
 {
     onOff(false);
     try
     {
         gamepad = getGamepad();
         setJoystickObserver();
         return true;
     }
     catch (DirectInputException)
     {
         return false;
     }
 }
예제 #24
0
파일: Joystick.cs 프로젝트: stewmc/vixen
		public void Acquire()
		{
			if (!IsAcquired) {
				_waitHandle = new AutoResetEvent(false);
				_device = _CreateDevice();
				// Must be done before acquisition.
				_device.SetNotification(_waitHandle);
				_device.Acquire();
				// Must be set before any notifications come through.
				IsAcquired = true;
				Thread thread = new Thread(_UpdateThread) {Name = "Joystick"};
				thread.Start();
			}
		}
예제 #25
0
 /// <summary>
 /// Inicializace gamepad
 /// </summary>
 /// <returns>true pokud se inicializace povedla</returns>
 public override bool inicialize()
 {
     onOff(false);
     try
     {
         gamepad = getGamepad();
         setJoystickObserver();
         return(true);
     }
     catch (DirectInputException)
     {
         return(false);
     }
 }
예제 #26
0
파일: Joystick.cs 프로젝트: stewmc/vixen
		private void _UpdateThread()
		{
			while (IsAcquired) {
				lock (_device) {
					_UpdateState();
				}
				_waitHandle.WaitOne();
			}

			_device.Dispose();
			_device = null;

			_waitHandle.Close();
			_waitHandle.Dispose();
			_waitHandle = null;
		}
예제 #27
0
        private void _UpdateThread()
        {
            while (IsAcquired)
            {
                lock (_device) {
                    _UpdateState();
                }
                _waitHandle.WaitOne();
            }

            _device.Dispose();
            _device = null;

            _waitHandle.Close();
            _waitHandle.Dispose();
            _waitHandle = null;
        }
예제 #28
0
 public void Acquire()
 {
     if (!IsAcquired)
     {
         _waitHandle = new AutoResetEvent(false);
         _device     = _CreateDevice();
         // Must be done before acquisition.
         _device.SetNotification(_waitHandle);
         _device.Acquire();
         // Must be set before any notifications come through.
         IsAcquired = true;
         Thread thread = new Thread(_UpdateThread)
         {
             Name = "Joystick"
         };
         thread.Start();
     }
 }
예제 #29
0
        public TabMateDevice()
        {
            // Find the device
            /// The DirectInput interface
            SlimDX.DirectInput.DirectInput input = new SlimDX.DirectInput.DirectInput();

            /// For every device connected using DirectInput that:
            /// -   is a "game controller"
            /// -   and is connected
            foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                /// Get the TabMate from the input interface, and bind its ref via the guid
                tab_mate = new Joystick(input, device.InstanceGuid);
                break;
            }

            event_timer.Elapsed += Event_timer_Elapsed;
            event_timer.Enabled  = true;
            event_timer.Interval = 1;
        }
예제 #30
0
        /// <summary>
        /// Constructor, do the following to loop through and select your joystick
        /// SlimDX.DirectInput.DirectInput dinput = new SlimDX.DirectInput.DirectInput();
        /// foreach (SlimDX.DirectInput.DeviceInstance device in dinput.GetDevices(SlimDX.DirectInput.DeviceClass.GameController, SlimDX.DirectInput.DeviceEnumerationFlags.AttachedOnly))
        /// {
        ///     Console.WriteLine("Controller:" + device.InstanceName);
        ///
        ///     DirectX.Joystick.onLog = logMessages;
        ///     DirectX.Joystick.debug = false;
        ///     joystick = new DirectX.Joystick(dinput, device);
        ///
        ///     joystick.axisX.min = 1;
        ///     joystick.axisX.max = 99;
        ///     joystick.axisX.deadZone = 0;
        ///     joystick.axisX.saturation = 10000;
        ///
        ///     joystick.setJoystickValues();
        ///
        ///     controlHelper = new controllHelper(joystick);
        ///     joystick.start();
        ///
        ///     break;
        /// }
        /// </summary>
        /// <param name="dInput"></param>
        /// <param name="joystickInstance"></param>
        public Joystick(DirectInput dInput, DeviceInstance joystickInstance)
        {
            this.joystickInstance = joystickInstance;
            JoystickName          = joystickInstance.ProductName;

            joystickDevice = new SlimDX.DirectInput.Joystick(dInput, joystickInstance.InstanceGuid);  // slimDX replacement
            joystickDevice.SetNotification(onJoystickEvent);
            joystickDevice.Acquire();

            axisY     = new axis();
            axisX     = new axis();
            axisZ     = new axis();
            axisRy    = new axis();
            axisRx    = new axis();
            axisRz    = new axis();
            axisExtra = new axis();

            /* initiate default joystick values.*/
            setJoystickValues();
        }
예제 #31
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);
        }
예제 #32
0
파일: IRob.cs 프로젝트: eastcm/RobSim
        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();
            }
        }
        /// <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;
        }
        /// <summary>Initialises the joystick.</summary>
        private void InitialiseJoystick()
        {
            List<DI.Joystick> joysticks = GetAttachedJoysticks();

            if(ControllerIndex < joysticks.Count)
            {
                _joystick = joysticks[ControllerIndex];
                _joystickState = _joystick.GetCurrentState();

                System.Diagnostics.Debug.WriteLine("Using joystick: " + _joystick.Information.InstanceName);
            }

            _joystickConnected = (_joystickState != null);
        }
예제 #35
0
파일: Joystick.cs 프로젝트: stewmc/vixen
		private SlimDX.DirectInput.Joystick _CreateDevice()
		{
			SlimDX.DirectInput.Joystick device;

			using (DirectInput directInput = new DirectInput()) {
				// Try to get the device specified.
				DeviceInstance deviceInstance =
					directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly).FirstOrDefault(
						x => x.InstanceGuid == DeviceId);

				if (deviceInstance == null) {
					throw new Exception("Specified joystick not found.");
				}

				device = new SlimDX.DirectInput.Joystick(directInput, deviceInstance.InstanceGuid);
				// This needs System.Windows.Forms.Control.
				device.SetCooperativeLevel(IntPtr.Zero, CooperativeLevel.Nonexclusive | CooperativeLevel.Background);
				device.Properties.SetRange((int) Position.MinValue, (int) Position.MaxValue);

				Inputs = _GetInputs(device).ToArray();
			}

			return device;
		}
예제 #36
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            for (int i = 0; i < reverserSwitchKeyTexts.Length; i++)
            {
                addKey(reverserSwitchKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    0, i
                });
            }
            for (int i = 0; i < cabSwitchKeyTexts.Length; i++)
            {
                addKey(cabSwitchKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    -1, i
                });
            }
            for (int i = 0; i < atsKeyTexts.Length; i++)
            {
                addKey(atsKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    -2, i
                });
            }
            for (int i = 0; i < gameControlKeyTexts.Length; i++)
            {
                addKey(gameControlKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    -3, i
                });
            }

            selectDropDownList(buttonSKeyList, DenshadeGoInterface.settings.ButtonS);
            selectDropDownList(buttonPKeyList, DenshadeGoInterface.settings.ButtonP);
            selectDropDownList(buttonAKeyList, DenshadeGoInterface.settings.ButtonA);
            selectDropDownList(buttonBKeyList, DenshadeGoInterface.settings.ButtonB);
            selectDropDownList(buttonCKeyList, DenshadeGoInterface.settings.ButtonC);

            bool added = false;
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in DenshadeGoInterface.input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    controllerList.Items.Add(device.ProductName);
                    Joystick stick = new SlimDX.DirectInput.Joystick(DenshadeGoInterface.input, device.InstanceGuid);
                    stick.Acquire();
                    int size = stick.GetCurrentState().GetButtons().Length;
                    buttonSize.Add(size);
                    if (device.ProductName.Equals(DenshadeGoInterface.settings.ControllerName))
                    {
                        added = true;
                        controllerList.SelectedIndex = controllerList.Items.Count - 1;
                        resizeUpDown(size);
                    }
                    stick.Unacquire();
                }
                catch (DirectInputException)
                {
                }
            }
            if (!added)
            {
                buttonSize.Add(128);
                controllerList.Items.Add(DenshadeGoInterface.settings.ControllerName);
                controllerList.SelectedIndex = controllerList.Items.Count - 1;
                resizeUpDown(128);
            }
            loadUpDown();
        }
예제 #37
0
 public Joystick(Guid controllerGuid)
 {
     gamepad = new SlimDX.DirectInput.Joystick(directInput, controllerGuid);
 }