0
\$\begingroup\$

The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using SharpDX;
using SharpDX.DirectInput;


namespace Ps4_Controller
{
    public partial class Form1 : Form
    {
        DirectInput input = new DirectInput();
        Joystick stick;

        public Form1()
        {
            InitializeComponent();

            List<Joystick> sticks = new List<Joystick>();

            foreach (DeviceInstance device in input.GetDevices
                (DeviceClass.GameControl,DeviceEnumerationFlags.AttachedOnly))
            {
                stick = new Joystick(input, device.InstanceGuid);
                stick.Acquire();



                  foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById(deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    sticks.Add(stick);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
}

I'm getting error on the line:

if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)

Severity Code Description Project File Line Suppression State Error CS0103 The name 'ObjectDeviceType' does not exist in the current

And on the line:

stick.GetObjectPropertiesById(deviceObject.ObjectType).SetRange(-100, 100);

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'System.Guid' to 'SharpDX.DirectInput.DeviceObjectId'

\$\endgroup\$
1
  • \$\begingroup\$ Just a note, the directinput library is deprecated. I see you are looking to use game pads ala ps4. The XInput interface (which is also in sharpdx) supports the gamepad interface more readily and its very simple to set up. have a look at that also. \$\endgroup\$
    – ErnieDingo
    Commented Jan 22, 2018 at 20:32

1 Answer 1

1
\$\begingroup\$

You're getting these errors because the library no longer works the way it did in whatever documentation you are using.

Joystick.GetObjects() supports a parameter that allows you to specify the ObjectDeviceType you are looking for.

Joystick.GetObjectPropertiesById() wants a DeviceObjectId as a parameter, not a Guid.

Relevant code as follows:

List<Joystick> sticks = new List<Joystick>();

foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly))
{
    Joystick stick = new Joystick(input, device.InstanceGuid);
    stick.Acquire();

    foreach (DeviceObjectInstance deviceObject in stick.GetObjects(DeviceObjectTypeFlags.Axis))
    {
        stick.GetObjectPropertiesById(deviceObject.ObjectId).Range = new InputRange(-100, 100);
    }
    sticks.Add(stick);
}

Please note that I've moved the declaration of the Joystick stick object into the form constructor. There's no reason to declare it outside of that scope.

Also note that some objects can not have their Range property set as it is readonly, so this may fail.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .