当前位置: 首页>>代码示例>>C#>>正文


C# Joystick.Poll方法代码示例

本文整理汇总了C#中Joystick.Poll方法的典型用法代码示例。如果您正苦于以下问题:C# Joystick.Poll方法的具体用法?C# Joystick.Poll怎么用?C# Joystick.Poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Joystick的用法示例。


在下文中一共展示了Joystick.Poll方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleJoystick

        private static void HandleJoystick()
        {
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                        DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

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

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Debug.WriteLine("No Gamepad found.");
                Environment.Exit(1);
            }

            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Debug.WriteLine("Found Gamepad with GUID: {0}", joystickGuid);

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

            // Acquire the joystick
            joystick.Acquire();

            // Poll events from joystick every 1 millisecond
            var timer = Observable.Interval(TimeSpan.FromMilliseconds(1));

            timer
                // Get all joystick input
                .SelectMany(_ => { joystick.Poll(); return joystick.GetBufferedData(); })
                // Filter only menu key button (xbox one controller)
                .Where(a => a.Offset == JoystickOffset.Buttons6)
                // Input will contain UP and Down button events
                .Buffer(2)
                // If button was pressed longer than a second
                .Where(t => (TimeSpan.FromMilliseconds(t.Last().Timestamp) - TimeSpan.FromMilliseconds(t.First().Timestamp)).TotalSeconds >= 1)
                // Press and hold F key for 1.5s
                .Subscribe(t =>
                {
                    SendKeyDown(KeyCode.KEY_F);
                    System.Threading.Thread.Sleep(1500);
                    SendKeyUp(KeyCode.KEY_F);
                });
        }
开发者ID:FloatInCodeBlog,项目名称:GamepadAsKeyboard,代码行数:56,代码来源:MainWindow.xaml.cs

示例2: MainForJoystick

        static void MainForJoystick()
        {
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

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

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                Console.ReadKey();
                Environment.Exit(1);
            }

            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

            // Query all suported ForceFeedback effects
            var allEffects = joystick.GetEffects();
            foreach (var effectInfo in allEffects)
                Console.WriteLine("Effect available {0}", effectInfo.Name);

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

            // Acquire the joystick
            joystick.Acquire();

            // Poll events from joystick
            while (true)
            {
                joystick.Poll();
                var datas = joystick.GetBufferedData();
                foreach (var state in datas)
                    Console.WriteLine(state);
            }
        }
开发者ID:numo16,项目名称:SharpDX,代码行数:49,代码来源:Program.cs

示例3: pollJoystickLoop

        private void pollJoystickLoop(Joystick joystick)
        {
            joystick.Properties.BufferSize = 128;
            joystick.Acquire();

            try
            {
                while (true)
                {
                    joystick.Poll();
                    var data = joystick.GetBufferedData();
                    foreach (var state in data)
                    {
                        FireJoystickInputEvent(state);
                    }
                }
            }
            catch(SharpDX.SharpDXException)
            {
                mLogger.Info("Gamepad disconnected");
            }
        }
开发者ID:redsoxfantom,项目名称:emulator-manager,代码行数:22,代码来源:JoystickComponent.cs

示例4: _Joystic

        public static void _Joystic()
        {

            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;


            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                    DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
                _is_joysticConnect = true;

            }
            // If Gamepad not found, look for a Joystick
            if (joystickGuid == Guid.Empty)
                foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
                        DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
                _is_joysticConnect = true;
            }
            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                _is_joysticConnect = false;
            }

            if (_is_joysticConnect)
            {
                var joystick = new Joystick(directInput, joystickGuid);

                Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

                // Query all suported ForceFeedback effects
                var allEffects = joystick.GetEffects();
                foreach (var effectInfo in allEffects)
                    Console.WriteLine("Effect available {0}", effectInfo.Name);

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

                // Acquire the joystick
                joystick.Acquire();

                // Poll events from joystick
                var joystickState = new JoystickState();
                bool work;
                while (_continue )
                {
                    work = false;
                    joystick.Poll();
                    var data = joystick.GetBufferedData();
                    foreach (var state in data)
                    {

                        if (state.Offset == JoystickOffset.X)
                        {
                            yaw_curr = ((32767 - state.Value) / 4096);
                            if (Math.Abs(yaw_curr - yaw_prev) >3 )
                            {
                                string mes = Convert.ToString(yaw_curr);
                                while (mes.Length < 5)
                                {
                                    mes = ' ' + mes;
                                }
                                _serialPort.WriteLine('y' + mes);
                                yaw_prev = yaw_curr;

                            }
                            work = true;
                        }
                        if (state.Offset == JoystickOffset.Z)
                        {
                            pitch_curr = ((32767 - state.Value) / 20) + pitch0;
                            if(Math.Abs(pitch_curr - pitch_prev)>100)
                            {
                                string mes = Convert.ToString(pitch_curr);
                                while (mes.Length < 5)
                                {
                                    mes = ' ' + mes;
                                }
                                _serialPort.WriteLine('p'+mes);
                                pitch_prev = pitch_curr;
                                
                            }
                            work = true;
                        }
                        if (state.Offset == JoystickOffset.RotationZ)
                        {
                            roll_curr = ((state.Value - 32767) / 20) + roll0;
                            if (Math.Abs(roll_curr - roll_prev) > 100)
                            {
                                string mes = Convert.ToString(roll_curr);
                                while (mes.Length < 5)
                                {
                                    mes = ' ' + mes;
//.........这里部分代码省略.........
开发者ID:vetal1808,项目名称:val_plot,代码行数:101,代码来源:Form1.cs

示例5: Main

        static void Main(string[] args)
        {
            if (client == null)
            {
                // create client instance
                client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));

                string clientId = Guid.NewGuid().ToString();
                client.Connect(clientId, "guest", "guest");

                SubscribeMessage();
            }
            // Initialize DirectInput

            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                        DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

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

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                Console.ReadKey();
                Environment.Exit(1);
            }

            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

            // Query all suported ForceFeedback effects
            var allEffects = joystick.GetEffects();
            foreach (var effectInfo in allEffects)
                Console.WriteLine("Effect available {0}", effectInfo.Name);

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

            // Acquire the joystick
            joystick.Acquire();
            const int stop = 32511;
            // Poll events from joystick
            while (true)
            {
                joystick.Poll();
                var datas = joystick.GetBufferedData();
                foreach (var state in datas)
                {
                    if (state.Offset.ToString() == "X" && state.Value > stop)
                    {
                        MoveRobot("MOVE", "R");
                        Console.WriteLine("Kanan");
                    }
                    else if(state.Offset.ToString() == "X" && state.Value < stop)
                    {
                        MoveRobot("MOVE", "L");

                        Console.WriteLine("Kiri");
                    }
                    else if (state.Offset.ToString() == "Y" && state.Value < stop)
                    {
                        MoveRobot("MOVE", "F");

                        Console.WriteLine("Maju");
                    }
                    else if (state.Offset.ToString() == "Y" && state.Value > stop)
                    {
                        MoveRobot("MOVE", "B");

                        Console.WriteLine("Mundur");
                    }
                    else
                    {
                        MoveRobot("MOVE", "S");

                        Console.WriteLine("Stop");
                    }

                }

            }
        }
开发者ID:Gravicode,项目名称:RCGadgeteer,代码行数:94,代码来源:Program.cs

示例6: MainForJoystick

        static void MainForJoystick()
        {
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

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

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                Console.ReadKey();
                Environment.Exit(1);
            }
            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

            // Query all suported ForceFeedback effects
            var allEffects = joystick.GetEffects();
            foreach (var effectInfo in allEffects)
                Console.WriteLine("Effect available {0}", effectInfo.Name);

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

            // Acquire the joystick
            joystick.Acquire();
            string s = "there is a cat";
            string[] words = s.Split(' ');
            foreach (string word in words)
            {
                Console.WriteLine(word);
            }
            // Poll events from joystick
            var originX = 0;
            var originY = 0;
            var lastStateX = -1;
            var lastStateY = -1;
            var startFlag = false;
            while (true)
            {
                joystick.Poll();
                var datas = joystick.GetBufferedData();
                double theta = 0.0;
                double radius = 0.0;

                foreach (var state in datas)
                {
                    if (state.Sequence == 1)
                    {
                        s = (state.Offset).ToString();
                        if (s == "X")
                            originX = state.Value;
                        else if (s == "Y")
                            originY = state.Value;
                    }
                    s = (state.Offset).ToString();
                    if (s == "X")
                        lastStateX = state.Value;
                    else if (s == "Y")
                        lastStateY = state.Value;
                    startFlag = true;
                }
                if (startFlag && datas.Length == 0 && lastStateX != -1 && lastStateY != -1)
                {
                    var slope = 0.0;
                    if ((lastStateX - originX) == 0)
                        slope = Math.PI / 2;
                    else
                        slope = (lastStateY - originY) / (lastStateX - originX);
                    theta = Math.Atan(slope);
                    radius = Math.Sqrt(Math.Pow(originX - lastStateX, 2) + Math.Pow(originY - lastStateY, 2));
                    originX = lastStateX;
                    originY = lastStateY;
                    Console.WriteLine(theta);
                    Console.WriteLine(radius);
                    startFlag = false;
                }
            }
        }
开发者ID:RASC-AL,项目名称:Interface,代码行数:91,代码来源:Program.cs

示例7: RefreshJoystickState

        private void RefreshJoystickState(Joystick joystick, ZXSpectrum.Joystick zxJoystick)
        {
            // Set BufferSize in order to use buffered data.
            // joystick.Properties.BufferSize = 128;
            // Poll events from joystick
            // joystick.Poll();
            // var datas = joystick.GetBufferedData();

            joystick.Poll();
            JoystickState currentState = joystick.GetCurrentState();

            ZXSpectrum.JoystickHorizontalPosition newHorizontalPosition = ZXSpectrum.JoystickHorizontalPosition.Center;
            if (currentState.X < 16383)
            {
                newHorizontalPosition = ZXSpectrum.JoystickHorizontalPosition.Left;
            }
            else if (currentState.X > 49150)
            {
                newHorizontalPosition = ZXSpectrum.JoystickHorizontalPosition.Right;
            }
            ZXSpectrum.JoystickVerticalPosition newVerticalPosition = ZXSpectrum.JoystickVerticalPosition.Center;
            if (currentState.Y < 16383)
            {
                newVerticalPosition = ZXSpectrum.JoystickVerticalPosition.Up;
            }
            else if (currentState.Y > 49150)
            {
                newVerticalPosition = ZXSpectrum.JoystickVerticalPosition.Down;
            }
            bool newButtonPressedState = currentState.Buttons[0];

            zxJoystick.RefreshCurrentPosition(newVerticalPosition, newHorizontalPosition, newButtonPressedState);
        }
开发者ID:laurentprudhon,项目名称:ZxSpectrumSimulator,代码行数:33,代码来源:JoystickAdapter.cs

示例8: pollJoystick

        private void pollJoystick(Guid joystickGuid)
        {
            Joystick joystick;

            // Instantiate the joystick
            joystick = new Joystick(directInput, joystickGuid);
            joystick.Properties.BufferSize = 128;

            Console.WriteLine("Joystick {0}", joystick.Properties.ProductName.ToString());
            Console.WriteLine("GUID: {0}", joystickGuid);

            // Query all suported ForceFeedback effects
            // TODO - maybe look into vibration effects on gamepads?
            var allEffects = joystick.GetEffects();
            foreach (var effectInfo in allEffects)
            {
                Console.WriteLine("Effect available {0}", effectInfo.Name);
            }

            // Acquire the joystick
            joystick.Acquire();

            // Poll events from joystick
            while (true)
            {
                joystick.Poll();
                var data = joystick.GetBufferedData();
                foreach (var state in data)
                {
                    //Null characters in the device name clogs up the write buffer when not run as a Console app. Replace all \0 with empty string to fix this.
                    Console.WriteLine("{0} - {1} - {2}", joystick.Properties.InstanceName.Replace("\0", ""), state.Offset, state.Value);

                    if (state.Offset.ToString().StartsWith("Button"))
                    {

                        buttonPressed(state.Offset, state.Value);
                        Console.WriteLine("Button {0} state changed to {1}", state.Offset, state.Value);

                    }
                    else if (state.Offset.ToString().StartsWith("Point"))
                    {

                        // POV controller
                        povChanged(state.Offset, state.Value);

                    }
                    else
                    {

                        //is an axis
                        int range = 32767; // this is for main joysticks. Might have to change if there are issues
                        axisMoved(state.Offset, state.Value, range);

                    }

                }
            }
        }
开发者ID:LiveFlightApp,项目名称:Connect-Windows,代码行数:58,代码来源:JoystickHelper.cs

示例9: joystickManagerThreadStart

        private void joystickManagerThreadStart()
        {
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                        DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

            foreach (var deviceInstance in directInput.GetDevices())
            {
                if (deviceInstance.InstanceName.Contains("X52 Pro"))
                {
                    Console.WriteLine(deviceInstance.InstanceName+", "+deviceInstance.InstanceGuid);
                    joystickGuid = deviceInstance.InstanceGuid;
                }
            }

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                Console.ReadKey();
                Environment.Exit(1);
            }
            // Instantiate the joystick
            joystick = new Joystick(directInput, joystickGuid);

            Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

            // Query all suported ForceFeedback effects
            var allEffects = joystick.GetEffects();
            foreach (var effectInfo in allEffects)
                Console.WriteLine("Effect available {0}", effectInfo.Name);

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

            // Acquire the joystick
            joystick.Acquire();
            // Poll events from joystick
            while (true)
            {
                joystick.Poll();
                var datas = joystick.GetBufferedData();
                foreach (var state in datas)
                {
                    if (state.Offset == JoystickOffset.Y)
                    {
                        Y_Axis = (state.Value - (Y_MAX_VAL / 2.0)) / (Y_MAX_VAL / 2.0);
                        if (Math.Abs(Y_Axis) <= 0.025)
                            Y_Axis = 0;
                        if (Y_INVERT)
                            Y_Axis *= -1;
                    }

                    if (state.Offset == JoystickOffset.X)
                    {
                        X_Axis = (state.Value - (X_MAX_VAL / 2.0)) / (X_MAX_VAL / 2.0);
                        if (Math.Abs(X_Axis) <= DEAD_ZONE)
                            X_Axis = 0;
                        if (X_INVERT)
                            X_Axis *= -1;
                    }

                    if(state.Offset == JoystickOffset.Z)
                    {
                        throttle = (state.Value) / ((double)X_MAX_VAL);
                        if (THROTTLE_INVERT)
                            throttle = (1 - throttle);
                    }

                    if(state.Offset == JoystickOffset.Buttons0)
                    {
                        if (state.Value == 128)
                            fire = true;
                        else
                            fire = false;
                    }
                }
            }
        }
开发者ID:kdolan,项目名称:Retaliator,代码行数:85,代码来源:frm_Primary.cs


注:本文中的Joystick.Poll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。