使用 C# 和 DirectX 使用教程从操纵杆获取输入
Obtaining input from Joystick with C# and DirectX using Tutorial
我正在使用 Visual Studio 2019 我的问题与此 Tutorial 的使用密切相关。我已经完成了所有步骤,例如:
- 在 C# 中创建 Window 表单
- 添加现有文件Joystick.cs
- 添加参考项
- 添加了应用程序配置文件并粘贴了提供的代码
- 将示例代码粘贴到 Form1.cs
我的问题:我错过了什么?我无法访问操纵杆 Screenshot of Error
的库
这是完整的 Form1.cs 代码:
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);
}
}
}
}
如果还有什么需要我提供的,请通知我。
谢谢!
编辑:我通过比较教程文件和博客中的分步教程文件解决了这个问题。
您可以下载您提到的文章中的项目查看代码。
根据我的测试,您可以在将theJoystick.cs
添加到您的项目后,在form1.cs代码中使用以下代码。
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;
}
}
}
}
}
我正在使用 Visual Studio 2019 我的问题与此 Tutorial 的使用密切相关。我已经完成了所有步骤,例如:
- 在 C# 中创建 Window 表单
- 添加现有文件Joystick.cs
- 添加参考项
- 添加了应用程序配置文件并粘贴了提供的代码
- 将示例代码粘贴到 Form1.cs
我的问题:我错过了什么?我无法访问操纵杆 Screenshot of Error
的库这是完整的 Form1.cs 代码:
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);
}
}
}
}
如果还有什么需要我提供的,请通知我。
谢谢!
编辑:我通过比较教程文件和博客中的分步教程文件解决了这个问题。
您可以下载您提到的文章中的项目查看代码。
根据我的测试,您可以在将theJoystick.cs
添加到您的项目后,在form1.cs代码中使用以下代码。
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;
}
}
}
}
}