Gamepad PoV控件要入睡

时间:2018-11-05 14:32:52

标签: c# wpf sharpdx joystick gamepad

轮询我的游戏手柄一分钟或两分钟后,它什么也不做,PoV控件进入某种睡眠模式,什么也不返回,但是选择一个按钮将其唤醒。这是正常现象吗,有没有办法使PoV保持睡眠状态?

激活Beckbox。...

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

1 个答案:

答案 0 :(得分:1)

它是一个功能示例,我选择第一个操纵杆...按钮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);
                }
            });
        }
    }
}