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

时间:2016-04-16 10:31:55

标签: c# sharpdx gamepad xinput

最近我尝试创建一个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();
        }
    }
}

使用此代码,您仍然可以按住按钮,它会对点击功能或任何被调用的内容发送垃圾邮件。

我也试过使用&#39; press&#39;按下时设置为true的布尔值,未按下时设置为false。并且只有在按下时才会拨打电话&#39;是== false。

也没用。

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

对不起,如果这是一个不好的问题。我阅读了问题的规则和提示,我搜索了重复项目,但除了我从中得到第一个解决方案之外,我找不到任何问题。

2 个答案:

答案 0 :(得分:2)

  

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

Here这是我前几天为另一个引擎编写的课程。但我认为它同样适用于任何其他引擎。我希望这会对你有所帮助:

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

这是重要的部分:

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;
}

答案 1 :(得分:1)

我已经弄清楚了 愚蠢的错误:我忘了按下按钮按下的if子句中的第一个条件