示例#1
0
        private void OnStartJoystickButtonClicked(object sender, EventArgs e)
        {
            JoystickInfo joystickInfo = JoystickInfo.ConfiguredJoystick;

            if (joystickInfo == null)
            {
                MessageBox.Show("Ensure that joystick is connected and enter settings", "No joystick configured",
                                MessageBoxButtons.OK);
                return;
            }

            if (String.IsNullOrEmpty(Settings.Default.JoystickButton))
            {
                MessageBox.Show("Ensure that joystick is connected and enter settings and detect the button", "No joystick button configured",
                                MessageBoxButtons.OK);
                return;
            }

            if (!Enum.TryParse(Settings.Default.JoystickButton, out _joystickOffset))
            {
                MessageBox.Show("Ensure that joystick is connected and enter settings and detect the button", "Incorrect joystick button configured",
                                MessageBoxButtons.OK);
                return;
            }

            _startJoystickButton.Enabled = false;

            _joystickObserver = new JoystickObserver(joystickInfo);
            _joystickObserver.OnJoystickUpdate += OnJoystickUpdated;

            _joystickObserver.Start();

            _stopJoystickButton.Enabled = true;
        }
        private async void OnDetectJoystickButtonClickedAsync(object sender, EventArgs e)
        {
            var joystickInfo = _joystickComboBox.SelectedItem as JoystickInfo;

            if (joystickInfo == null)
            {
                MessageBox.Show("Please select a joystick and try again", "No joystick selected", MessageBoxButtons.OK);
                return;
            }

            _detectJoystickButton.Enabled = false;
            try
            {
                using (var observer = new JoystickObserver(joystickInfo))
                {
                    observer.Start();
                    await Task.Delay(100);

                    JoystickOffset?offset = null;

                    var cancellationTokenSource = new CancellationTokenSource();

                    observer.OnJoystickUpdate += (o, update) =>
                    {
                        offset = update.Offset;
                        cancellationTokenSource.Cancel();
                    };

                    try
                    {
                        await Task.Delay(5000, cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException)
                    {
                    }

                    if (offset.HasValue)
                    {
                        _joystickButtonTextbox.Text = offset.Value.ToString();
                    }
                    else
                    {
                        MessageBox.Show("Press the detect button again and press the desired joystick button", "No button deteced", MessageBoxButtons.OK);
                    }
                }
            }
            finally
            {
                _detectJoystickButton.Enabled = true;
            }
        }