Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

events - How to Poll For Controller Input In UWP App

I'm unsure about the best practice for obtaining and updating input received from a controller monitored using the GamePad class in UWP.

I've seen a couple of examples of people using Dispatch Timers and async loops inside the GamePadAdded event. In Win32 applications, I would have handled input in the WinMain update/message loop, but in UWP apps I don't know of anything similar.

Is there a loop in UWP apps that input should be collected/handled like in Win32 apps? What is the recommended protocol for polling for input from a input device (nominally a Xbox One controller)?

I'm happy to read more about UWP app development but I'm unsure of any guides that reference something like this.

Edit: It would be productive if, instead of downvoting and moving on, you shared thoughts on why this question deserved a downvote.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've seen a couple of examples of people using Dispatch Timers and async loops inside the GamePadAdded event

This is the right way in UWP app to read Gamepad data. A little suggestion is, move the loop reading part on UI thread if you need to update UI frequently. See the solution in this blog

Is there a loop in UWP apps that input should be collected/handled like in Win32 apps

You may make a wrapper with custom event, see the open source implementation: XBoxGamepad

public class XBoxGamepad
{
    private List<Gamepad> _controllers = new List<Gamepad>();
    private bool _running = true;

    Task backgroundWorkTask;

    public event EventHandler<GamepadButtons> OnXBoxGamepadButtonPressA;
    //omitted......

    public XBoxGamepad()
    {
        Gamepad.GamepadAdded += Gamepad_GamepadAdded;
        Gamepad.GamepadRemoved += Gamepad_GamepadRemoved;
        backgroundWorkTask = Task.Run(() => PollGamepad());
    }

    //omitted......

    private void Start()
    {
        _running = true;
    }

    public void Stop()
    {
        _running = false;
    }

    public async Task PollGamepad()
    {
        while (true)
        {
            if (_running)
            {
                foreach (Gamepad controller in _controllers)
                {
                    if (controller.GetCurrentReading().Buttons == GamepadButtons.A)
                    {
                        OnXBoxGamepadButtonPressA(controller, controller.GetCurrentReading().Buttons);
                    }
                    //omitted......
                }
            }
            await Task.Delay(50);
        }
    }

    private void Gamepad_GamepadRemoved(object sender, Gamepad e)
    {
        _controllers.Remove(e);
    }

    private void Gamepad_GamepadAdded(object sender, Gamepad e)
    {
        _controllers.Add(e);
    }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...