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)
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();
}
}
}
Using this code you can still hold down the buttons and it will spam the click functions or whatever is being called.
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.
Didn't work either.
Do you have any other solution for that problem ?
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.
Do you have any other solution for that problem?
Here's a class I wrote the other day for another Engine. But I figured it would apply equally well to any other engine. I hope this helps you:
Basically, it checks if the key is held in the current frame and not held in the last frame. They key states are stored in a Dictionary
Here's the important part:
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;
}