首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gamepad PoV控件进入睡眠

Gamepad PoV控件进入睡眠
EN

Stack Overflow用户
提问于 2018-11-05 14:32:51
回答 1查看 269关注 0票数 0

当轮询我的游戏垫(它在一两分钟内什么也不做)时,PoV控件进入某种睡眠模式,什么也不返回,但是选择一个按钮唤醒它。这是否正常,是否有一种防止PoV睡眠的方法?

Ckeckbox激活。

代码语言:javascript
复制
  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);
        }
    }

主回路轮询游戏垫..。

代码语言:javascript
复制
  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);
        }
    }

投票游戏垫..。

代码语言:javascript
复制
    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)
        {            }
    }

找到附加的游戏垫..。

代码语言:javascript
复制
    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();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-06 08:13:58

这是一个实用的例子,我选择第一根棍子.按钮0停止循环,我测试pov。

代码语言:javascript
复制
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);
                }
            });
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53156434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档