C# | SharpDX.XInput | How to detect if a button on the controller was pressed once and is not getting hold down
<p>Lately i tried creating a XBOX 360 Controller Driver.
I need to detect if a button is pressed or held down. So if you hold down it also calls a specific function once just as if u would press the button once.
Here is my code (i found this solution on another post but it didn't work for me)</p>

<pre><code>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 &gt; buffernud.Value || x &lt; -buffernud.Value)
            {
                Cursor.Position = new Point(Cursor.Position.X + movementX, Cursor.Position.Y);
            }
            if(y &gt; buffernud.Value || y &lt; -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 &amp;&amp; stateNew.Gamepad.Buttons == GamepadButtonFlags.LeftShoulder)
            {
                LeftClick();
            }

            //b right click
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.RightShoulder &amp;&amp; stateNew.Gamepad.Buttons == GamepadButtonFlags.RightShoulder)
            {
                RightClick();
            }

            //dpad up button sens up
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.DPadUp &amp;&amp; stateNew.Gamepad.Buttons == GamepadButtonFlags.DPadUp)
            {
                sensnud.UpButton();
            }

            //dpad down button sens down
            if (stateOld.Gamepad.Buttons == GamepadButtonFlags.DPadDown &amp;&amp; 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();
        }
    }
}
</code></pre>

<p>Using this code you can still hold down the buttons and it will spam the click functions or whatever is being called.</p>

<p>I also tried using a 'pressed' Boolean that is being set to true when pressed and to false when not pressed. And it only does the call when 'pressed' is == false.</p>

<p>Didn't work either.</p>

<p>Do you have any other solution for that problem ?</p>

<p>Sorry if this is a bad Question. I read the rules and tips for questions and i searched for duplicates but didn't find any except for the one i got the first solution from.</p>