The class provides simple API for enumerating available joysticks and checking their current status.

The class provides simple access to joysticks (game controllers) through using Win32 API, which allows to enumerate available devices and query their status (state of all buttons, axes, etc).

Sample usage:

// enumerate available devices List<Joystick.DeviceInfo> devices = Joystick.GetAvailableDevices( ); foreach ( Joystick.DeviceInfo di in devices ) { System.Diagnostics.Debug.WriteLine( string.Format( "{0} : {1} ({2} axes, {3} buttons)", di.ID, di.Name, di.Axes, di.Buttons ) ); } // create new joystick and initialize it Joystick joystick = new Joystick( 0 ); // get its current status Joystick.Status status = joystick.GetCurrentStatus( ); // check if 1st button is pressed if ( status.IsButtonPressed( Joystick.Buttons.Button1 ) ) { // 1st button is pressed }
        private void LabelUpLeft_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                joystick = new Joystick(0);
                botonesActivos = new Thread(detectarBotones);
                labelIndex = 1;
                botonesActivos.Start();
            }
            catch (Exception err)
            {

                MessageBox.Show(err.Message);
            }
           
            
        }
        private void CargaJoystick()
        {
            try
            {
                joystick = new Joystick(0);
                botonesActivos = new Thread(detectarBotones);


                botonesActivos.Start();
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message); 
            }
            
          
        }
Пример #3
0
        private bool GetAvaliableJoystickControls()
        {
            // enumerate available devices
            List<Joystick.DeviceInfo> devices = Joystick.GetAvailableDevices();

            foreach (Joystick.DeviceInfo di in devices)
            {
                this.send_UI_message(string.Format("{0} : {1} ({2} axes, {3} buttons)", di.ID, di.Name, di.Axes, di.Buttons));
            }

            try
            {
                // create new joystick and initialize it
                joystick = new Joystick(0);
                // get its current status
                joystick.Init(0);
                return true;
            }
            catch (Exception ex)
            {
                this.send_UI_message("No joystick found." + ex.Message.ToString());
                return false;

            }
        }
Пример #4
0
        /// <summary>
        /// Scanning routine for device input
        /// </summary>
        /// <param name="sender">Event Source</param>
        /// <param name="e">Event arguments</param>
        private void Scan(object sender, DoWorkEventArgs e)
        {
            Joystick joystick = new Joystick(0);
            Joystick.Status status;
            String buttons = "";
            BackgroundWorker worker = (BackgroundWorker) sender;
            while (!worker.CancellationPending)
            {
                status = joystick.GetCurrentStatus();
                buttons = status.Buttons.ToString();
                // action button
                if (buttons != "0")
                {
                    List<string> inputs = buttons.Split(new[] { "Button", ", " }, StringSplitOptions.RemoveEmptyEntries).ToList();

                    SetClickCallBack callBack = SetClick;
                    for (int i = 0; i < Buttons.Length; i++)
                    {
                        Buttons[i].Dispatcher.Invoke(callBack,
                            new object[] {i.ToString(), 
                                inputs.Contains((i+1).ToString())});
                    }
                }
                else
                {
                    SetClickCallBack callBack = SetClick;
                    for (int i = 0; i < Buttons.Length; i++)
                    {
                        Buttons[i].Dispatcher.Invoke(callBack,
                            new object[] {i.ToString(), false});
                    }
                }

                try
                {
                    // navigation button
                    switch ((int)status.XAxis)
                    {
                        case -1:
                            Left.Dispatcher.Invoke(() =>
                                {
                                    Left.Click = true;
                                }
                                );
                            break;
                        case 1:
                            Left.Dispatcher.Invoke(() =>
                                {
                                    Right.Click = true;
                                }
                                );
                            break;
                        default:
                            Left.Dispatcher.Invoke(() =>
                                {
                                    Left.Click = false;
                                }
                                );
                            Left.Dispatcher.Invoke(() =>
                                {
                                    Right.Click = false;
                                }
                                );
                            break;
                    }
                    switch ((int)status.YAxis)
                    {
                        case -1:
                            Up.Dispatcher.Invoke(() =>
                                {
                                    Up.Click = true;
                                }
                                );
                            break;
                        case 1:
                            Down.Dispatcher.Invoke(() =>
                                {
                                    Down.Click = true;
                                }
                                );
                            break;
                        default:
                            Up.Dispatcher.Invoke(() =>
                                {
                                    Up.Click = false;
                                }
                                );
                            Down.Dispatcher.Invoke(() =>
                                {
                                    Down.Click = false;
                                }
                                );
                            break;
                    }
                }
                catch (TaskCanceledException )
                {
                    break;
                }

            }
        }
 private void LabelDownRight_GotFocus(object sender, RoutedEventArgs e)
 {
     joystick = new Joystick(0);
     botonesActivos = new Thread(detectarBotones);
     labelIndex = 5;
     botonesActivos.Start();
 }