C# 按下按钮时发生SlimDx事件
我正在使用slimdx来解释xbox控制器的按键操作。我每隔200毫秒进行一次民意调查,以了解xbox按钮的状态和所有适合我的功能。我用C# 按下按钮时发生SlimDx事件,c#,slimdx,C#,Slimdx,我正在使用slimdx来解释xbox控制器的按键操作。我每隔200毫秒进行一次民意调查,以了解xbox按钮的状态和所有适合我的功能。我用 JoystickState state = Joystick.GetCurrentState(); // get buttons states bool[] buttonsPressed = state.GetButtons(); 是否仍然需要在按下按钮而不是轮询时生成事件?想象一下,如果我的投票时间是5秒。用
JoystickState state = Joystick.GetCurrentState();
// get buttons states
bool[] buttonsPressed = state.GetButtons();
是否仍然需要在按下按钮而不是轮询时生成事件?想象一下,如果我的投票时间是5秒。用户在第二秒内按下一个按钮并将其释放。在下一次轮询时,我的应用程序将永远不会知道按钮已按下否-在DirectX中,您必须进行轮询。要有效地执行此操作,您需要创建一个轮询线程,并使用一个类将跨线程事件引发到您的消费线程。我知道这已经有4年了,但答案不正确。最有效的方式可能是投票,但投票时可以引发事件 这是一个正在进行的工作,但它应该让人开始。将其另存为一个新类,它派生自一个计时器,因此一旦将其添加到项目中,构建它,并将其拖到要使用它的表单上,就可以订阅buttonPressed事件
public class GamePadController : Timer
{
public delegate void ButtonPressedDelegate(object sender, int ButtonNumber);
public event ButtonPressedDelegate ButtonPressed;
List<DeviceInstance> directInputList = new List<DeviceInstance>();
DirectInput directInput = new DirectInput();
List<SlimDX.DirectInput.Joystick> gamepads = new List<Joystick>();
SlimDX.DirectInput.JoystickState state;
public GamePadController()
{
this.Interval = 10;
this.Enabled = true;
this.Tick += GamePadController_Tick;
RefreshGamePads();
}
private void RefreshGamePads()
{
directInputList.Clear();
directInputList.AddRange(directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly));
gamepads.Clear();
foreach (var device in directInputList)
{
gamepads.Add(new SlimDX.DirectInput.Joystick(directInput, directInputList[0].InstanceGuid));
}
}
private void GamePadController_Tick(object sender, EventArgs e)
{
foreach (var gamepad in gamepads)
{
if (gamepad.Acquire().IsFailure)
continue;
if (gamepad.Poll().IsFailure)
continue;
if (SlimDX.Result.Last.IsFailure)
continue;
state = gamepad.GetCurrentState();
bool[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i])
{
if (ButtonPressed != null)
{
ButtonPressed(gamepad, i);
}
}
}
gamepad.Unacquire();
}
}
}
}
公共类游戏控制器:计时器
{
public委托void按钮presseddelegate(对象发送方,int按钮编号);
公共事件按钮按下Legate按钮按下;
List directInputList=新列表();
DirectInput DirectInput=新的DirectInput();
List gamepads=新列表();
SlimDX.DirectInput.JoystickState状态;
公共游戏控制器()
{
这个时间间隔=10;
this.Enabled=true;
this.Tick+=GamePadController\u Tick;
刷新游戏板();
}
专用游戏板()
{
directInputList.Clear();
directInputList.AddRange(directInput.GetDevices(DeviceClass.GameController,DeviceEnumerationFlags.AttachedOnly));
gamepads.Clear();
foreach(directInputList中的var设备)
{
gamepads.Add(新的SlimDX.DirectInput.mogage(DirectInput,directInputList[0].InstanceGuid));
}
}
私有void GamePadController_Tick(对象发送方,事件参数e)
{
foreach(gamepad中的var gamepad)
{
if(gamepad.Acquire().IsFailure)
继续;
if(gamepad.Poll().IsFailure)
继续;
if(SlimDX.Result.Last.IsFailure)
继续;
state=gamepad.GetCurrentState();
bool[]buttons=state.GetButtons();
对于(int i=0;i