Exemplo n.º 1
0
        /// <summary>
        /// Polls the joystick.
        /// </summary>
        private void PollJoystick()
        {
            pollingTask = Task.Factory.StartNew(() =>
            {
                while (!IsQuitPolling)
                {
                    try
                    {
                        joystick.Poll();
                        JoystickUpdate[] data = joystick.GetBufferedData();
                        foreach (JoystickUpdate joystickState in data.Where(IsRelevantUpdate))
                        {
                            // button pressed down
                            JoystickButtonPressedEventArgs args = new JoystickButtonPressedEventArgs
                            {
                                ButtonOffset = joystickState.RawOffset,
                                PovValue     = joystickState.Value,
                                NamedOffset  = joystickState.Offset.ToString().Trim(),
                                TimeStamp    = DateTime.Now
                            };

                            OnJoystickButtonPressed(args);
                        }
                    }
                    catch (SharpDX.SharpDXException)
                    {
                        StopCapture();
                        return;
                    }

                    Task.Delay(250);
                }
            });
        }
Exemplo n.º 2
0
 protected virtual void OnJoystickStartButtonPressed(JoystickButtonPressedEventArgs e)
 {
     JoystickStartButtonPressed?.Invoke(this, e);
 }
        /// <summary>
        /// Handles the JoystickButtonPressed event of the joystickHelper control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="JoystickButtonPressedEventArgs"/> instance containing the event data.</param>
        private void JoystickHelper_JoystickButtonPressed(object sender, JoystickButtonPressedEventArgs e)
        {
            // Translate joystick buttons presses to keyboard keys presses, because WPF does not handle joysticks well for UI navigation.

            // Buttons and POV support.
            switch (e.ButtonOffset)
            {
            case JoystickConstants.A_BUTTON_OFFSET:
            case JoystickConstants.START_BUTTON_OFFSET:
                Application.Current.Dispatcher.Invoke((Action) delegate
                {
                    SendKeys.Send(Key.Enter);
                });
                break;

            case JoystickConstants.BACK_BUTTON_OFFSET:
            case JoystickConstants.B_BUTTON_OFFSET:
                Application.Current.Dispatcher.Invoke((Action) delegate
                {
                    Close();
                });
                break;

            case JoystickConstants.POV_BUTTONS_OFFSET:
                switch (e.PovValue)
                {
                case JoystickConstants.POV_UP_BUTTON_VALUE:
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Up);
                    });
                    break;

                case JoystickConstants.POV_DOWN_BUTTONS_VALUE:
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Down);
                    });
                    break;

                case JoystickConstants.POV_LEFT_BUTTONS_VALUE:
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Left);
                    });
                    break;

                case JoystickConstants.POV_RIGHT_BUTTONS_VALUE:
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Right);
                    });
                    break;
                }
                break;

            default:
                break;
            }
            // X and Y axis support (left stick/joystick)
            switch (e.NamedOffset)
            {
            case "X":
                if (e.PovValue == 0)
                {
                    // Left
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Left);
                    });
                }
                else if (e.PovValue == UInt16.MaxValue)
                {
                    // Right
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Right);
                    });
                }
                break;

            case "Y":
                if (e.PovValue == 0)
                {
                    // Up
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Up);
                    });
                }
                else if (e.PovValue == UInt16.MaxValue)
                {
                    // Down
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        SendKeys.Send(Key.Down);
                    });
                }
                break;
            }
        }
 /// <summary>
 /// Handles the JoystickLapButtonPressed event of the joystickHelper control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="JoystickButtonPressedEventArgs"/> instance containing the event data.</param>
 private void JoystickHelper_JoystickLapButtonPressed(object sender, JoystickButtonPressedEventArgs e)
 {
     MessageBox.Show("joystickHelper_JoystickLapButtonPressed");
 }