C#| SharpDX.XInput |如何检测控制器上的按钮是否被按下过一次并且没有被按住

本文关键字:一次 按钮 何检测 XInput SharpDX 检测 控制器 是否 | 更新日期: 2024-09-21 16:01:34

最近我尝试创建一个XBOX 360控制器驱动程序。我需要检测一下是按下了按钮还是按住了按钮。因此,如果你按住它,它也会调用一次特定的函数,就像你按下按钮一样。这是我的代码(我在另一篇文章中找到了这个解决方案,但它对我不起作用)

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using SharpDX.XInput;
using System.Runtime.InteropServices;
namespace Windows_XBOX_Controller_Driver
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
        private static Controller controller;
        private State stateNew;
        private State stateOld;
        private int x, y;
        public Form1()
        {
            InitializeComponent();
            Setup();
        }
        private void loop_Tick(object sender, EventArgs e)
        {
            if(!enabledcb.Checked)
            {
                return;
            }
            //
            stateNew = controller.GetState();
            ///////////////////////////////////
            x = stateNew.Gamepad.RightThumbX;
            y = stateNew.Gamepad.RightThumbY;
            x /= 1000;
            y /= 1000;
            //////////////////////////////
            int movementX = x;
            int movementY = y;
            movementX *= (int)sensnud.Value;
            movementY *= (int)sensnud.Value;
            movementX /= 2;
            movementY /= 2;
            movementY *= -1;
            ////////////////////////
            if (x > buffernud.Value || x < -buffernud.Value)
            {
                Cursor.Position = new Point(Cursor.Position.X + movementX, Cursor.Position.Y);
            }
            if(y > buffernud.Value || y < -buffernud.Value)
            {
                Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + movementY);
            }
            //buttons
            CheckButtons();
        }
        private void CheckButtons()
        {
            //a left click
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.LeftShoulder && stateNew.Gamepad.Buttons == GamepadButtonFlags.LeftShoulder)
            {
                LeftClick();
            }
            //b right click
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.RightShoulder && stateNew.Gamepad.Buttons == GamepadButtonFlags.RightShoulder)
            {
                RightClick();
            }
            //dpad up button sens up
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.DPadUp && stateNew.Gamepad.Buttons == GamepadButtonFlags.DPadUp)
            {
                sensnud.UpButton();
            }
            //dpad down button sens down
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.DPadDown && stateNew.Gamepad.Buttons == GamepadButtonFlags.DPadDown)
            {
                sensnud.DownButton();
            }
            stateOld = stateNew;
        }
        private void LeftClick()
        {
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            mouse_event(0x02 | 0x04, (uint)X, (uint)Y, 0, 0);
            Thread.Sleep(100);
        }
        private void RightClick()
        {
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            mouse_event(0x08 | 0x10, (uint)X, (uint)Y, 0, 0);
            Thread.Sleep(100);
        }
        private void LoadController()
        {
            controller = new Controller(UserIndex.One);
            if(!controller.IsConnected)
            {
                MessageBox.Show("Error", "It seems like there is no XBOX 360 Controller connected via USB!");
                Application.Exit();
            }
        }
        private void updateLoop_Tick(object sender, EventArgs e)
        {
            xlbl.Text = "Offset X: " + x;
            ylbl.Text = "Offset Y: " + y;
        }
        private void applybtn_Click(object sender, EventArgs e)
        {
            loop.Interval = (int)updatenud.Value;
        }
        private void Setup()
        {
            LoadController();
        }
    }
}

使用此代码,您仍然可以按住按钮,它将垃圾邮件点击功能或调用的任何功能。

我还尝试使用一个"按下"的布尔值,当按下时设置为true,而当不按下时则设置为false。它只在'pressed'为==false时执行调用。

也没用。

你有其他解决这个问题的办法吗?

如果这是一个糟糕的问题,很抱歉。我阅读了规则和问题提示,并搜索了重复的问题,但除了第一个解决方案之外,没有找到任何问题。

C#| SharpDX.XInput |如何检测控制器上的按钮是否被按下过一次并且没有被按住

你有其他解决这个问题的办法吗?

这是我前几天为另一个引擎写的一节课。但我认为它同样适用于任何其他发动机。我希望这能帮助你:

基本上,它检查关键帧在当前帧中是否保持,在最后一帧中是否未保持。它们的关键状态存储在字典中

以下是重要部分:

public bool IsKeyPress(Keys key)
{
    bool isCurrentlyPressed = WaveServices.Input.KeyboardState.IsKeyPressed(key);
    bool previouslyReleased = this.previousKeyStates[key] == ButtonState.Release;
    this.previousKeyStates[key] = isCurrentlyPressed ? ButtonState.Pressed : ButtonState.Release;
    return  isCurrentlyPressed && previouslyReleased;
}

我已经想好了愚蠢的错误:我忘记反转按钮的if子句中的第一个条件