最近我尝试创建一个 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();
}
}
}
使用此代码,您仍然可以按住按钮,它会向点击函数或正在调用的任何内容发送垃圾邮件。
我还尝试使用“按下”布尔值,按下时设置为真,不按下时设置为假。并且它仅在“按下”== false 时调用。
也没有用。
你对这个问题还有其他解决方案吗?
对不起,如果这是一个不好的问题。我阅读了问题的规则和提示,并搜索了重复项,但除了我得到第一个解决方案的那个之外,没有找到任何重复项。