コード例 #1
0
ファイル: Form1.cs プロジェクト: sql2018/JoystickLearning
        /// <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());
                }
            }
        }
コード例 #2
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);
                }
            }
        }
コード例 #3
-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 ();
            }
        }