简体   繁体   中英

Obtaining input from Joystick with C# and DirectX using Tutorial

I'm using Visual Studio 2019 my question is closely aligned with the use of this Tutorial . I've done all the steps such as:

  1. Creation of a Window Form in C#
  2. Adding existing file Joystick.cs
  3. Adding Reference item
  4. Added Application Configuration File and pasted the code provided
  5. Pasted example code into Form1.cs

My question: What am I missing? I can't access the library for joystick Screenshot of Error

Here is the whole Form1.cs code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RunJoyStickOnLocalMachine{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void joystickTimer_Tick_1(object sender, EventArgs e)
    {
        try
        {
            joystick.UpdateStatus();
            joystickButtons = joystick.buttons;

            if (joystick.Xaxis == 0)
                output.Text += "Left\n";

            if (joystick.Xaxis == 65535)
                output.Text += "Right\n";

            if (joystick.Yaxis == 0)
                output.Text += "Up\n";

            if (joystick.Yaxis == 65535)
                output.Text += "Down\n";

            for (int i = 0; i < joystickButtons.Length; i++)
            {
                if (joystickButtons[i] == true)
                    output.Text += "Button " + i + " Pressed\n";
            }
        }
        catch
        {
            joystickTimer.Enabled = false;
            connectToJoystick(joystick);
        }
    }
}
}

If there is anything else that I need to provide please notify me.

Thank You!


Edit: I've solved this problem by comparing both the tutorial file and the step by step tutorial file that the blog has.

You can download the project from the article you mentioned to check the code.

Based on my test, you can use the following code in the form1.cs code after you add theJoystick.cs to your project.

public partial class Form1 : Form
    {

        private Joystick joystick;        // define the type Joystick
        private bool[] joystickButtons;  // here  define the bool array
        public Form1()
        {
            InitializeComponent();
            joystick = new Joystick(this.Handle);
            connectToJoystick(joystick);
        }

        private void joystickTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                joystick.UpdateStatus();
                joystickButtons = joystick.buttons;

                if (joystick.Xaxis == 0)
                    output.Text += "Left\n";    // output is the name of richtextbox

                if (joystick.Xaxis == 65535)
                    output.Text += "Right\n";

                if (joystick.Yaxis == 0)
                    output.Text += "Up\n";

                if (joystick.Yaxis == 65535)
                    output.Text += "Down\n";

                for (int i = 0; i < joystickButtons.Length; i++)
                {
                    if (joystickButtons[i] == true)
                        output.Text += "Button " + i + " Pressed\n";
                }
            }
            catch
            {
                joystickTimer.Enabled = false;
                connectToJoystick(joystick);
            }
        }
        private void enableTimer()
        {
            if (this.InvokeRequired)
            {
                BeginInvoke(new ThreadStart(delegate ()
                {
                    joystickTimer.Enabled = true;
                }));
            }
            else
                joystickTimer.Enabled = true;
        }
        private void connectToJoystick(Joystick joystick)
        {
            while (true)
            {
                string sticks = joystick.FindJoysticks();
                if (sticks != null)
                {
                    if (joystick.AcquireJoystick(sticks))
                    {
                        enableTimer();
                        break;
                    }
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM