手柄 PoV 控制进入休眠状态

Gamepad PoV control going to sleep

当轮询我的游戏手柄时,它在一两分钟内没有任何反应,PoV 控件进入某种睡眠模式并且 returns 没有任何反应,但选择一个按钮将其唤醒。这正常吗?有没有办法让 PoV 不休眠?

Ckeckbox 激活....

  private void CheckBoxJoystick_Checked(object sender, EventArgs e)
    {
        if (CheckboxJoystick.IsChecked.HasValue & CheckboxJoystick.IsChecked == true)
        {
            var windowHandle = Process.GetCurrentProcess().MainWindowHandle;
            _gamepad = new Gamepad(windowHandle);
            if (!_gamepad.IsAvailable) return;
            ctsGamepad?.Cancel();
            ctsGamepad = new CancellationTokenSource();
            ThreadPool.QueueUserWorkItem(DoGamepadWork, ctsGamepad.Token);
        }
    }

轮询游戏手柄的主循环...

  private void DoGamepadWork(object obj)
    {
        if (!_gamepad.IsAvailable) return;
        var token = (CancellationToken)obj;
        var buttontocheck = -1;
        var povtocheck = new PovPair(-1,0);
        while (true)
        {
            if (token.IsCancellationRequested)
            {
                break;
            }
            _gamepad.Poll();
            // Check buttons...
            // Check PoVs...
            Thread.Sleep(100);
        }
    }

轮询手柄....

    public void Poll()
    {
        try
        {
            if (!IsAvailable) return;
            joystick.Acquire();
            joystick.Poll();
            State = joystick.GetCurrentState();
            Buttons = State.Buttons;
            Povs = State.PointOfViewControllers;
            Datas = joystick.GetBufferedData();
        }
        catch(Exception ex)
        {            }
    }

找到附加的游戏手柄...

    private void Find()
    {
        foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly))
            joystickGuid = deviceInstance.InstanceGuid;

        // If Gamepad not found, look for a Joystick
        if (joystickGuid == Guid.Empty)
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly))
                joystickGuid = deviceInstance.InstanceGuid;

        // If Joystick not found
        if (joystickGuid == Guid.Empty)
        {
            IsAvailable = false;
            return;
        }

        // Instantiate the joystick
        joystick = new Joystick(directInput, joystickGuid);
        joystick.SetCooperativeLevel(hWnd, CooperativeLevel.Background | CooperativeLevel.NonExclusive);

        // Set BufferSize in order to use buffered data.
        joystick.Properties.BufferSize = 128;

        // Acquire the joystick
        joystick.Acquire();
        IsAvailable = true;
        Load_Settings();
    }

这是一个功能示例,我 select 第一个摇杆...按钮 0 停止循环,我测试 pov[0]

using SharpDX.DirectInput;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var controller = new Controller();
        }
    }

    public class Controller
    {
        private Task pollingTask;
        private bool running;

        private JoystickState state;

        public JoystickState State => state ?? (state = controller.GetCurrentState());
        public Joystick controller;
        public Controller()
        {
            var directInput = new DirectInput();
            var handle = Process.GetCurrentProcess().MainWindowHandle;
            var diDevices = directInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly);
            controller = new Joystick(directInput, diDevices[0].InstanceGuid);
            controller.SetCooperativeLevel(handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
            controller.Acquire();
            running = true;
            PollJoystick();

            if (pollingTask != null)
            {
                pollingTask.Wait();
            }
            Console.WriteLine("fini");
            Console.ReadKey();
        }
        TimeSpan period = TimeSpan.FromMilliseconds(30);
        public int[] Pov => State.PointOfViewControllers;
        public bool stop => State.Buttons[0];
        public void PollJoystick()
        {
            pollingTask = Task.Factory.StartNew(() => {
                while (running)
                {
                    state = null;
                    running = !stop;
                    if (Pov[0] != -1)
                        Console.WriteLine(Pov[0]);
                    Task.Delay(period);
                }
            });
        }
    }
}