Beispiel #1
0
        private void getCurrentController()
        {
            //Checks, if controller is connected before detecting a new controller
            bool wasConnected = false;

            if (xController != null)
            {
                wasConnected = true;
            }

            XInputDevice getDevice = new XInputDevice();

            xController = getDevice.getActiveController();

            if (xController != null)
            {
                if (wasConnected == false)
                {
                    //When new a controller is detected
                    Console.WriteLine("New XInput controller has been detected and has a listener has been attached.");

                    Console.WriteLine("Starting xListener thread!");
                    xListener.RunWorkerAsync();
                }
            }
            else
            {
                Console.WriteLine("No controllers detected");
                if (wasConnected == true)
                {
                    Console.WriteLine("XInput controller was unplugged.");
                }
            }
        }
Beispiel #2
0
        private void xListener_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("xListener Started");

            XInputDevice xDevice       = new XInputDevice();
            var          previousState = xController.GetState();

            string lastKey = "None";

            while (xController.IsConnected)
            {
                var buttons = xController.GetState().Gamepad.Buttons;

                //Check for buttons here!
                if (xDevice.getPressedButton(buttons) != "None")
                {
                    string currentKey = xDevice.getPressedButton(buttons);

                    if (currentKey != lastKey)
                    {
                        ControllerUnsetButton(lastKey);
                        ControllerSendButton(currentKey);
                        lastKey = currentKey;
                    }
                    else
                    {
                        lastKey = currentKey;
                    }
                }
                else
                {
                    ControllerUnsetButton(lastKey);
                }

                Thread.Sleep(PollingRate);
            }

            Console.WriteLine("Disposing of xListener thread!");
        }