Exemplo n.º 1
0
        private Joystick joystick; //joystick object

        #endregion Fields

        #region Constructors

        public Form1()
        {
            InitializeComponent();

            joystick = new Joystick(this.Handle);

            if (Joystick.connectToJoystick(joystick, joystickTimer) == false)
            {
                MessageBox.Show("Device Not found!");
                this.Dispose(); //Free resources
                this.Close();   //Terminate the program
            }
        }
        /// <summary>
        /// Find the attached joystick
        /// </summary>
        /// <param name="joystick">This will be the handles joystick object</param>
        /// <param name="joyStickTimer">JoystickTimer (Control) that ticks for the joystick</param>
        /// <returns></returns>
        public static bool connectToJoystick(Joystick joystick, Timer joyStickTimer)
        {
            while (true)
            {
                string sticks = joystick.FindJoysticks();

                if (sticks != null)
                {
                    if (joystick.AcquireJoystick(sticks))
                    {
                        joyStickTimer.Enabled = true;
                        break;  //Joystick found
                    }
                }
                else
                {
                    return false;   //Joystick not found
                }

            }
            return true;
        }
        /// <summary>
        /// Returns the joystick button, will be used for debouncing, check if the return is button.notRecognized
        /// </summary>
        /// <param name="js">Joystick object</param>
        /// <param name="jsTimer">JoystickTimer (Control) that ticks for the joystick</param>
        public static buttons getPressedButton(Joystick js, Timer jsTimer)
        {
            previousState = currentState;

            try
            {
                js.UpdateStatus();
                joystickButtons = js.availableButtons;   //Get joystick all buttons in a bool array

                if (js.Xaxis == 0)
                {
                    currentState = buttons.Left;
                }

                if (js.Xaxis == axis_Max_Val)
                {
                    currentState = buttons.Right;
                }

                if (js.Yaxis == 0)
                {
                    currentState = buttons.Up;
                }

                if (js.Yaxis == axis_Max_Val)
                {
                    currentState = buttons.Down;
                }

                //Find the pressed button
                for (int i = 0; i < joystickButtons.Length; i++)
                {
                    if (joystickButtons[i] == true) //The pressed button will have a true value
                    {
                        currentState = (buttons)(i + 1); //Save the pressed button
                    }
                }
            }
            catch
            {
                jsTimer.Enabled = false;    //Disable the timer
                Joystick.connectToJoystick(js, jsTimer);    //Reconnect to the joystick
            }
            return currentState;    //Return the current button
        }