Esempio n. 1
0
        private bool GetButtonState(InputDevice inputDeviceBinding)
        {
            foreach (var kpDevice in _inputDevices)
            {
                var device = kpDevice.Value;
                if (device != null &&
                    !device.IsDisposed &&
                    device.Information.InstanceGuid.Equals(inputDeviceBinding.InstanceGuid))
                {
                    try
                    {
                        if (device is Joystick)
                        {
                            device.Poll();
                            var state = (device as Joystick).GetCurrentState();

                            if (inputDeviceBinding.Button >= 128) //its a POV!
                            {
                                var pov = state.PointOfViewControllers;
                                //-128 to get POV index
                                return(pov[inputDeviceBinding.Button - 128] == inputDeviceBinding.ButtonValue);
                            }
                            else
                            {
                                return(state.Buttons[inputDeviceBinding.Button]);
                            }
                        }
                        else if (device is Keyboard)
                        {
                            var keyboard = device as Keyboard;
                            keyboard.Poll();
                            var state = keyboard.GetCurrentState();
                            return
                                (state.IsPressed(state.AllKeys[inputDeviceBinding.Button]));
                        }
                        else if (device is Mouse)
                        {
                            device.Poll();
                            var state = (device as Mouse).GetCurrentState();

                            //just incase mouse changes number of buttons, like logitech can?
                            if (inputDeviceBinding.Button < state.Buttons.Length)
                            {
                                return(state.Buttons[inputDeviceBinding.Button]);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, $"Failed to get current state of input device {device.Information.ProductName.Trim().Replace("\0", "")} " +
                                     $"(ID: {device.Information.ProductGuid}) while retrieving button state, ignoring until next restart/rediscovery");

                        MessageBox.Show(
                            $"An error occurred while querying your {device.Information.ProductName.Trim().Replace("\0", "")} input device.\nThis could for example be caused by unplugging " +
                            $"your joystick or disabling it in the Windows settings.\n\nAll controls bound to this input device will not work anymore until your restart SRS.",
                            "Input device error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);

                        device.Unacquire();
                        device.Dispose();
                    }
                }
            }
            return(false);
        }
Esempio n. 2
0
        public void AssignButton(DetectButton callback)
        {
            //detect the state of all current buttons
            Task.Run(() =>
            {
                var deviceList = _inputDevices.Values.ToList();

                var initial = new int[deviceList.Count, 128 + 4]; // for POV

                for (var i = 0; i < deviceList.Count; i++)
                {
                    if (deviceList[i] != null && !deviceList[i].IsDisposed)
                    {
                        try
                        {
                            if (deviceList[i] is Joystick)
                            {
                                deviceList[i].Poll();

                                var state = (deviceList[i] as Joystick).GetCurrentState();

                                for (var j = 0; j < state.Buttons.Length; j++)
                                {
                                    initial[i, j] = state.Buttons[j] ? 1 : 0;
                                }
                                var pov = state.PointOfViewControllers;

                                for (var j = 0; j < pov.Length; j++)
                                {
                                    initial[i, j + 128] = pov[j];
                                }
                            }
                            else if (deviceList[i] is Keyboard)
                            {
                                var keyboard = deviceList[i] as Keyboard;
                                keyboard.Poll();
                                var state = keyboard.GetCurrentState();

                                for (var j = 0; j < 128; j++)
                                {
                                    initial[i, j] = state.IsPressed(state.AllKeys[j]) ? 1 : 0;
                                }
                            }
                            else if (deviceList[i] is Mouse)
                            {
                                var mouse = deviceList[i] as Mouse;
                                mouse.Poll();

                                var state = mouse.GetCurrentState();

                                for (var j = 0; j < state.Buttons.Length; j++)
                                {
                                    initial[i, j] = state.Buttons[j] ? 1 : 0;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, $"Failed to get current state of input device {deviceList[i].Information.ProductName.Trim().Replace("\0", "")} " +
                                         $"(ID: {deviceList[i].Information.ProductGuid}) while assigning button, ignoring until next restart/rediscovery");

                            deviceList[i].Unacquire();
                            deviceList[i].Dispose();
                            deviceList[i] = null;
                        }
                    }
                }

                var device      = string.Empty;
                var button      = 0;
                var deviceGuid  = Guid.Empty;
                var buttonValue = -1;
                var found       = false;

                while (!found)
                {
                    Thread.Sleep(100);

                    for (var i = 0; i < _inputDevices.Count; i++)
                    {
                        if (deviceList[i] != null && !deviceList[i].IsDisposed)
                        {
                            try
                            {
                                if (deviceList[i] is Joystick)
                                {
                                    deviceList[i].Poll();

                                    var state = (deviceList[i] as Joystick).GetCurrentState();

                                    for (var j = 0; j < 128 + 4; j++)
                                    {
                                        if (j >= 128)
                                        {
                                            //handle POV
                                            var pov = state.PointOfViewControllers;

                                            if (pov[j - 128] != initial[i, j])
                                            {
                                                found = true;

                                                var inputDevice = new InputDevice
                                                {
                                                    DeviceName =
                                                        deviceList[i].Information.ProductName.Trim().Replace("\0", ""),
                                                    Button       = j,
                                                    InstanceGuid = deviceList[i].Information.InstanceGuid,
                                                    ButtonValue  = pov[j - 128]
                                                };
                                                Application.Current.Dispatcher.Invoke(
                                                    () => { callback(inputDevice); });
                                                return;
                                            }
                                        }
                                        else
                                        {
                                            var buttonState = state.Buttons[j] ? 1 : 0;

                                            if (buttonState != initial[i, j])
                                            {
                                                found = true;

                                                var inputDevice = new InputDevice
                                                {
                                                    DeviceName =
                                                        deviceList[i].Information.ProductName.Trim().Replace("\0", ""),
                                                    Button       = j,
                                                    InstanceGuid = deviceList[i].Information.InstanceGuid,
                                                    ButtonValue  = buttonState
                                                };

                                                Application.Current.Dispatcher.Invoke(
                                                    () => { callback(inputDevice); });


                                                return;
                                            }
                                        }
                                    }
                                }
                                else if (deviceList[i] is Keyboard)
                                {
                                    var keyboard = deviceList[i] as Keyboard;
                                    keyboard.Poll();
                                    var state = keyboard.GetCurrentState();

                                    for (var j = 0; j < 128; j++)
                                    {
                                        if (initial[i, j] != (state.IsPressed(state.AllKeys[j]) ? 1 : 0))
                                        {
                                            found = true;

                                            var inputDevice = new InputDevice
                                            {
                                                DeviceName =
                                                    deviceList[i].Information.ProductName.Trim().Replace("\0", ""),
                                                Button       = j,
                                                InstanceGuid = deviceList[i].Information.InstanceGuid,
                                                ButtonValue  = 1
                                            };

                                            Application.Current.Dispatcher.Invoke(
                                                () => { callback(inputDevice); });


                                            return;
                                        }

                                        //                                if (initial[i, j] == 1)
                                        //                                {
                                        //                                    Console.WriteLine("Pressed: "+j);
                                        //                                    MessageBox.Show("Keyboard!");
                                        //                                }
                                    }
                                }
                                else if (deviceList[i] is Mouse)
                                {
                                    deviceList[i].Poll();

                                    var state = (deviceList[i] as Mouse).GetCurrentState();

                                    //skip left mouse button - start at 1 with j 0 is left, 1 is right, 2 is middle
                                    for (var j = 1; j < state.Buttons.Length; j++)
                                    {
                                        var buttonState = state.Buttons[j] ? 1 : 0;


                                        if (buttonState != initial[i, j])
                                        {
                                            found = true;

                                            var inputDevice = new InputDevice
                                            {
                                                DeviceName =
                                                    deviceList[i].Information.ProductName.Trim().Replace("\0", ""),
                                                Button       = j,
                                                InstanceGuid = deviceList[i].Information.InstanceGuid,
                                                ButtonValue  = buttonState
                                            };

                                            Application.Current.Dispatcher.Invoke(
                                                () => { callback(inputDevice); });
                                            return;
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error(e, $"Failed to get current state of input device {deviceList[i].Information.ProductName.Trim().Replace("\0", "")} " +
                                             $"(ID: {deviceList[i].Information.ProductGuid}) while discovering button press while assigning, ignoring until next restart/rediscovery");

                                deviceList[i].Unacquire();
                                deviceList[i].Dispose();
                                deviceList[i] = null;
                            }
                        }
                    }
                }
            });
        }