gpt4 book ai didi

C# | SharpDX.X输入法 |如何检测 Controller 上的按钮是否被按下一次并且没有被按下

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:51 26 4
gpt4 key购买 nike

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

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

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

我还尝试使用“按下” bool 值,该 bool 值在按下时设置为 true,在未按下时设置为 false。并且它仅在“按下”为 == false 时才调用。

也没用。

对于这个问题,您还有其他解决方案吗?

抱歉,如果这是一个不好的问题。我阅读了问题的规则和提示,并搜索了重复的内容,但除了我从中获得第一个解决方案的那个之外没有找到任何内容。

最佳答案

Do you have any other solution for that problem?

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

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

这是重要的部分:

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

关于C# | SharpDX.X输入法 |如何检测 Controller 上的按钮是否被按下一次并且没有被按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36662984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com