//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());
    }
Beispiel #2
0
        /// <summary>
        /// Autodetect button and joystick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoDetect2(object sender, EventArgs e)         //(object sender, EventArgs e) ??? make this function run when hitting button
        {
            //Change color of button to indicate that autotedect is running
            c_autoDetect.BackColor = System.Drawing.Color.Aquamarine;
            c_autoDetect.Update();

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            //if none of the axes changes enough within 3 seconds, stop this autodetect processs and use -1 as index and joystick
            int joystickIndex = -1;
            int buttonIndex   = -1;

            while (stopWatch.ElapsedMilliseconds < 3000 && buttonIndex == -1)
            {
                jh.Update();

                //for each joystick, check each axis
                for (int ji = 0; ji < jh.joystick.Length; ji++)
                {
                    TJoystick joystick = jh.joystick[ji];

                    for (int bi = 0; bi < joystick.button.Length; bi++)
                    {
                        bool buttonValue    = joystick.button[bi];
                        bool oldButtonValue = joystick.buttonPrev[bi];

                        //if a button is pressed, set this as the button to use
                        if (buttonValue != oldButtonValue)
                        {
                            joystickIndex = ji;
                            buttonIndex   = bi;
                        }
                    }
                }
            }

            if (buttonIndex != -1)             //if a button was detected, save this as the button to use
            {
                c_joystick.SelectedIndex = joystickIndex;
                c_button.Value           = buttonIndex;
            }

            c_autoDetect.UseVisualStyleBackColor = true;
            c_autoDetect.Update();
        }
Beispiel #3
0
        /// <summary>
        /// Autodetect axis and joystick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoDetect2(object sender, EventArgs e)         //(object sender, EventArgs e) ??? make this function run when hitting button
        {
            //detection threshold
            float dth = 255;

            //Change color of button to indicate that autotedect is running
            c_autoDetect.BackColor = System.Drawing.Color.Aquamarine;
            c_autoDetect.Update();

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            //if none of the axes changes enough within 3 seconds, stop this autodetect processs and use -1 as index and joystick
            int joystickIndex = -1;
            int axisIndex     = -1;

            while (stopWatch.ElapsedMilliseconds < 3000 && axisIndex == -1)
            {
                jh.Update();

                //for each joystick, check each axis
                for (int ji = 0; ji < jh.joystick.Length; ji++)
                {
                    TJoystick joystick = jh.joystick[ji];

                    for (int ai = 0; ai < joystick.axis.Length; ai++)
                    {
                        int axisValue    = joystick.axis[ai];
                        int oldAxisValue = joystick.axisPrev[ai];

                        //if the value of the axis is over a sertain value, save thisjoystick and axis as the detected axis
                        if (axisValue < oldAxisValue - dth || axisValue > oldAxisValue + dth)
                        {
                            joystickIndex = ji;
                            axisIndex     = ai;
                        }
                    }
                }
            }

            c_joystick.SelectedIndex             = joystickIndex;
            c_axis.SelectedIndex                 = axisIndex;
            c_autoDetect.UseVisualStyleBackColor = true;
            c_autoDetect.Update();
        }
Beispiel #4
0
        /// <summary>
        /// Autodetect axis
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoDetect(object sender, EventArgs e)         //(object sender, EventArgs e) ??? make this function run when hitting button
        {
            //if there is no joystick selected, sop auto detecting
            if (this.joystick_idx == -1)
            {
                return;
            }

            TJoystick joystick = jh.joystick[this.joystick_idx];

            int  index       = -1;
            bool buttonValue = false;

            //Change color of button to indicate that autotedect is running
            c_autoDetect.BackColor = System.Drawing.Color.Aquamarine;
            c_autoDetect.Update();

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            //if none of the axes changes enough within 3 seconds, stop this autodetect processs and use -1 as index
            while (stopWatch.ElapsedMilliseconds < 3000 && index == -1)
            {
                jh.Update();

                for (int i = 0; i < joystick.button.Length; i++)
                {
                    buttonValue = joystick.button[i];
                    if (buttonValue)
                    {
                        index = i;
                    }
                }
            }

            if (index == -1)
            {
                index = 0;
            }

            c_button.Value = index;
            c_autoDetect.UseVisualStyleBackColor = true;
            c_autoDetect.Update();
        }