Trouble detecting PlayStation controller using SharpDX in C#

176 views Asked by At

Problem:

I am working on a C# application to detect and handle input from a PlayStation controller using SharpDX and DirectInput. However, the code I have written doesn't seem to detect any controllers, even though I am sure the controller is connected and working perfectly (tested with DS4Windows).

Code:

            var directInput = new DirectInput();
            var devices = directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices);
            foreach (var deviceInstance in devices)
            {
                MessageBox.Show("Device: " + deviceInstance.InstanceName);
            }
            var joystick = new Joystick(directInput, devices[0].InstanceGuid);
            joystick.Acquire();
            while (true)
            {
                joystick.Poll();
                var data = joystick.GetBufferedData();

                foreach (var state in data)
                {
                    MessageBox.Show(state.Value.ToString());
                }
            }

Observations:

I have verified that the PlayStation controller is recognized by DS4Windows. The application doesn't detect any controllers when running the provided code.

What I've Tried:

Closing DS4Windows before running the application. Running the application with administrative privileges. Checking for exclusivity settings in DS4Windows. Updating SharpDX to the latest version.

Edit:

I'm open to use any method for reading inputs from a dualshock. (and it would be better if i also could send outputs too)

1

There are 1 answers

0
Raul On

PlayStation controllers, especially DualShock 4, often have compatibility issues with DirectInput, as they are not natively supported by Windows in the same way Xbox controllers are. Here are some suggestions to troubleshoot and potentially resolve your issue:

  1. Use RawInput Instead of DirectInput:

    • PlayStation controllers are often better supported through RawInput rather than DirectInput. You might need to switch your approach and use the RawInput API to detect and read inputs from your controller.
  2. Check the DeviceType:

    • In your current code, you are using DeviceType.Gamepad. PlayStation controllers are sometimes recognized as DeviceType.Joystick instead of DeviceType.Gamepad. Try changing this in your code to see if it makes a difference.
  3. Use DS4Windows API or ViGEmBus:

    • DS4Windows works by converting PS controller inputs into Xbox controller inputs, which are more easily recognized by Windows. You might consider using the DS4Windows API directly in your application, or using ViGEmBus, a virtual gamepad emulation bus, which can be used to emulate Xbox controllers and might provide better compatibility.
  4. Ensure Controller is in Exclusive Mode:

    • Sometimes a PlayStation controller needs to be in exclusive mode, in order to be properly recognized. Make sure that DS4Windows is properly configuring the controller in exclusive mode.
  5. Check for Driver Issues:

    • There could be driver-related issues preventing the controller from being detected properly. Ensure that the drivers for the controller are up to date and there are no conflicts in the device manager.

Handling PlayStation controllers in a Windows environment can be tricky due to the lack of native support and often requires a workaround like emulating an Xbox controller.