示例#1
0
        public FizzyoInput()
        {
            directInput = new SharpDX.DirectInput.DirectInput();
            //var devices = directInput.GetDevices();
            var devices = directInput.GetDevices(SharpDX.DirectInput.DeviceType.Supplemental, SharpDX.DirectInput.DeviceEnumerationFlags.AllDevices);

            joysticks          = new SharpDX.DirectInput.Joystick[devices.Count];
            joysticksDeadzoneX = new int[devices.Count];
            for (int i = 0; i < devices.Count; i++)
            {
                var joystickGuid = devices[i].InstanceGuid;
                joysticks[i] = new SharpDX.DirectInput.Joystick(directInput, joystickGuid);
                joysticks[i].Acquire();
                var deadzone = joysticks[i].GetCurrentState();
                joysticksDeadzoneX[i] = deadzone.X - 500;
            }
        }
示例#2
0
        /// <summary>
        /// 不间断尝试重新连接手柄
        /// </summary>
        private void TJsGet()
        {
            int JoystickCount = 0;
            var dirInput      = new SharpDX.DirectInput.DirectInput();

            while (true)
            {
                if (JoystickName != "")
                {
                    //已经接入最大数量的手柄了
                    //10s检测一次手柄掉线,如果未掉线,则跳过
                    Thread.Sleep(10000);
                    continue;
                }
                var allDevices = dirInput.GetDevices();
                foreach (var item in allDevices)
                {
                    if (SharpDX.DirectInput.DeviceType.Joystick == item.Type)
                    {
                        //记录新建线程的手柄名称
                        if (JoystickName == item.ProductName)
                        {
                            //说明已经接入这个手柄了
                        }
                        else
                        {
                            JoystickName = item.ProductName;
                            curJoystick  = new SharpDX.DirectInput.Joystick(dirInput, item.InstanceGuid);
                            curJoystick.Properties.AxisMode = SharpDX.DirectInput.DeviceAxisMode.Absolute;
                            curJoystick.Acquire();
                            JoystickCount++;
                            Thread t1 = new Thread(TJsListening);
                            t1.IsBackground = true;
                            t1.Start();
                        }
                        break;
                        //curJoystick.Unacquire();//释放手柄
                    }
                }
                if (JoystickCount == 0)
                {
                    MessageBox.Show("手柄数量 " + JoystickCount.ToString());
                }
            }
        }
示例#3
0
        public override void Dispose()
        {
            lock (_sync)
            {
                if (_joystick != null)
                {
                    _joystick.Dispose();
                    _joystick = null;
                }

                if (_directInput != null)
                {
                    _directInput.Dispose();
                    _directInput = null;
                }
            }

            _form.Dispose();
            _timer.Dispose();
        }
示例#4
0
        public override bool Run()
        {
            lock (_sync)
            {
                try
                {
                    if (_directInput == null)
                    {
                        _directInput = new SharpDX.DirectInput.DirectInput();
                    }

                    if (_joystick == null)
                    {
                        var devices = _directInput.GetDevices(SharpDX.DirectInput.DeviceClass.GameControl, SharpDX.DirectInput.DeviceEnumerationFlags.AttachedOnly);

                        if (devices.Count == 0)
                        {
                            ShowError("Джойстик не обнаружен!");
                            return(false);
                        }

                        _joystick = new SharpDX.DirectInput.Joystick(_directInput, devices[0].InstanceGuid);
                        _joystick.SetCooperativeLevel(_form.Handle, SharpDX.DirectInput.CooperativeLevel.Background | SharpDX.DirectInput.CooperativeLevel.NonExclusive);
                        _joystick.Properties.BufferSize = 128;
                    }

                    _joystick.Acquire();
                    _timer.Change(Period, Period);

                    _acquired = true;

                    return(true);
                }
                catch (Exception exc)
                {
                    ShowError(exc.Message);
                    return(false);
                }
            }
        }
示例#5
0
 public static MyJoystickState CurrentJoystickState(this SharpDX.DirectInput.Joystick joystick)
 {
     return(new MyJoystickState(joystick.GetCurrentState()));
 }
示例#6
0
        private void loadJoy()
        {
            var    Joystick1 = System.Configuration.ConfigurationManager.AppSettings["Joystick1"];
            var    Joystick2 = System.Configuration.ConfigurationManager.AppSettings["Joystick2"];
            string joy1      = "";

            if (Joystick1 != null)
            {
                joy1 = Joystick1.ToString();
            }
            string joy2 = "";

            if (Joystick2 != null)
            {
                joy2 = Joystick2.ToString();
            }

            int JoystickCount = 0;
            var dirInput      = new SharpDX.DirectInput.DirectInput();

            if (curJoystick == null)
            {
                curJoystick    = new SharpDX.DirectInput.Joystick[2];
                curJoystick[0] = null;
                curJoystick[1] = null;
            }
            bool bNeadJoy = false;

            if (curJoystick[0] == null && joy1 != "")
            {
                bNeadJoy = true;
            }

            if (curJoystick[1] == null && joy2 != "")
            {
                bNeadJoy = true;
            }
            if (!bNeadJoy)
            {
                return;
            }
            var allDevices = dirInput.GetDevices();

            foreach (var item in allDevices)
            {
                if (SharpDX.DirectInput.DeviceType.Joystick == item.Type)
                {
                    //记录新建线程的手柄名称
                    if (item.ProductName == joy1)
                    {
                        curJoystick[0] = new SharpDX.DirectInput.Joystick(dirInput, item.InstanceGuid);
                        curJoystick[0].Properties.AxisMode = SharpDX.DirectInput.DeviceAxisMode.Absolute;
                        curJoystick[0].Acquire();
                        JoystickCount++;
                        if (t1 != null)
                        {
                            t1.Abort();
                        }
                        t1 = new Thread(TJsListening1);
                        t1.IsBackground = true;
                        t1.Start();
                    }
                    else if (item.ProductName == joy2)
                    {
                        curJoystick[1] = new SharpDX.DirectInput.Joystick(dirInput, item.InstanceGuid);
                        curJoystick[1].Properties.AxisMode = SharpDX.DirectInput.DeviceAxisMode.Absolute;
                        curJoystick[1].Acquire();
                        JoystickCount++;
                        if (t2 != null)
                        {
                            t2.Abort();
                        }
                        t2 = new Thread(TJsListening2);
                        t2.IsBackground = true;
                        t2.Start();
                    }

                    //curJoystick.Unacquire();//释放手柄
                }
            }
        }
示例#7
-1
文件: GamePad.cs 项目: Daramkun/Misty
        public GamePad( IWindow window )
        {
            dInput = new SharpDX.DirectInput.DirectInput ();

            Guid gamePadGuid = Guid.Empty;
            foreach ( var deviceInstance in dInput.GetDevices ( SharpDX.DirectInput.DeviceType.Gamepad,
                SharpDX.DirectInput.DeviceEnumerationFlags.AllDevices ) )
                gamePadGuid = deviceInstance.InstanceGuid;
            if ( gamePadGuid != Guid.Empty )
            {
                device = new SharpDX.DirectInput.Joystick ( dInput, gamePadGuid );
                device.SetCooperativeLevel ( ( window.Handle as Form ).Handle,
                    SharpDX.DirectInput.CooperativeLevel.Background | SharpDX.DirectInput.CooperativeLevel.NonExclusive );
                device.Properties.BufferSize = 128;
                device.Acquire ();
            }
        }