我正在寻找一个程序,当我按下我的xbox控制器上的按钮时,即使程序没有聚焦,也会启动一个wpf窗口。
以下是我目前正在使用的代码:
private readonly List<Joystick> joysticks = new List<Joystick>();
private readonly DirectInput directInput = new DirectInput();
private readonly Timer timer = new Timer(1000);
public ControlManagement()
{
var devices = this.directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly);
foreach (var device in devices)
{
var joystick = new Joystick(this.directInput, device.InstanceGuid);
joystick.Acquire();
this.joysticks.Add(joystick);
}
timer.Enabled = true;
timer.Elapsed += TimerOnElapsed;
timer.Start();
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
foreach (var joystick in this.joysticks)
{
var state = joystick.GetCurrentState();
var buttons = state.GetButtons();
if (buttons.Any(b => b))
{
MessageBox.Show("You pressed a button!");
busy = false;
return;
}
}
}