使用物理操纵杆按钮触发 windows 表单上的按钮

Triggering button on windows form using a physical joystick button

背景:

摇杆-剃刀锐际大师

Visual Studio 2019

Windows C# 中的表单

我正在尝试弄清楚如何按下物理操纵杆按钮,使用 C# 在 Visual studio 中按下 windows 表单 UI 上的按钮.

物理摇杆代码:

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 == 65534)
                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++)
            {
                // Assigning buttons
                if (joystickButtons[0] == true)
                    output.Text += "Cross\n";
                

                else if(joystickButtons[1] == true)
                    output.Text += "Circle\n";

                else if(joystickButtons[2] == true)
                    output.Text += "Square\n";


                else if (joystickButtons[3] == true)
                    output.Text += "Triangle\n";
                    Btn_Emergency_Stop_Click(sender,e); //Attempt at button press
                   //Btn_Emergency_Stop_Click.Click += joystickButtons[3];
                    break;
            }
        }
        catch
        {
            joystickTimer.Enabled = false;
            connectToJoystick(joystick);
        }
    }

要按下的虚拟按钮:

private void Btn_Emergency_Stop_Click(object sender, EventArgs e)
    {

        bool showed = false;
        if (!showed)
        {
            MessageBox.Show("Emergency stop pressed!");
            showed = true;
        }

    }

如果我遗漏了任何信息,请通知我,谢谢!

我已经想出了解决这个问题的方法,基本上,我需要在 if 语句的正下方添加按钮方法的调用。否则,如果您将按钮方法的调用放在下面两行,它会无限循环。

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

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

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

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

            if (joystick.Yaxis == 65535)
                output.Text += "L-Down\n";
            
            for (int i = 0; i < joystickButtons.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        if (joystickButtons[0] == true)
                            Btn_Manual_Mode_Click(null, null);
                            break;

                    case 1:
                        if (joystickButtons[1] == true)
                            Btn_Stop_Auto_Click(null, null);
                        break;

                    case 2:
                        if (joystickButtons[2] == true)
                            Btn_Standby_Mode_Click(null, null);
                        break;

                    case 3:
                        if (joystickButtons[3] == true)
                            Btn_Auto_Mode_Click(null, null);
                        break;


                }
            }
        }
        catch
        {
            joystickTimer.Enabled = false;
            connectToJoystick(joystick);
        }
    }