1

I found this answer but I'm using SlimDX and the answer is using DirectX:

Answer with directx

Maybe I should install the directx sdk ? If so from where to download it ?

What I did so far with my code using slimdx is:

The problem is that the Effect class in SlimDX is not the same Effect class in Directx. So i can't use the answer in the link since all the properties of the Effect in DirectX not exist in SlimDX Effect.

My goal is to send commands automatic to the joystick that is connected to my pc usb. Commands i mean like a button was pressed effect for example the square button or the triangle button or the circle or the X and the other buttons too.

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 SlimDX;
using SlimDX.DirectInput;
using System.Runtime.InteropServices;


namespace Ps4_Controller
{
    public partial class Form1 : Form
    {

        DirectInput input = new DirectInput();
        Joystick stick;
        Joystick[] sticks;
        bool mouseClicked = false;

        int yValue = 0;
        int xValue = 0;
        int zValue = 0;

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern void mouse_event(uint flag, uint _x, uint _y, uint btn, uint exInfo);
        private const int MOUSEEVENT_LEFTDOWN = 0x02;
        private const int MOUSEEVENT_LEFTUP = 0x04;

        public Form1()
        {
            InitializeComponent();

            GetSticks();
            sticks = GetSticks();
            timer1.Enabled = true;
        }

        public Joystick[] GetSticks()
        {
            List<Joystick> sticks = new List<Joystick>();

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

                    Effect effect = new Effect(stick, device.InstanceGuid);
                    Effects(effect);

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


                }
                catch(DirectInputException)
                {

                }
            }
            return sticks.ToArray();
        }

        void stickHandle(Joystick stick, int id)
        {
            JoystickState state = new JoystickState();
            state = stick.GetCurrentState();

            yValue = -state.Y;
            xValue = state.X;
            zValue = state.Z;
            MouseMove(xValue, yValue);
            bool[] buttons = state.GetButtons();

            if (id == 0)
            {
                if (buttons[0])
                {
                    if (mouseClicked == false)
                    {
                        mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
                        mouseClicked = true;
                    }
                    else
                    {
                        if (mouseClicked == true)
                        {
                            mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
                            mouseClicked = false;
                        }
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Joystick[] joystick = GetSticks();
        }

        public new void MouseMove(int posx, int posy)
        {
            Cursor.Position = new Point(Cursor.Position.X + posx/3, Cursor.Position.Y + posy/3);
            Cursor.Clip = new Rectangle(this.Location, this.Size);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < sticks.Length; i++)
            {
                stickHandle(sticks[i], i);
            }
        }

        private void Effects(Effect effect)
        {


        }
    }
}

Update

I tried also to use the SharpDX but i'm getting errors and not sure how to get on with it. First thing i tried to List the joysticks that are connected to my pc but getting errors. Later on i want to do my main goal:

My goal is to send commands automatic to the joystick that is connected to my pc usb. Commands i mean like a button was pressed effect for example the square button or the triangle button or the circle or the X and the other buttons too.

But i guess this code is not even the right way to start with.

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)
        {

        }
}
Community
  • 1
  • 1
Moses Meteor
  • 365
  • 3
  • 12
  • Ok i downloaded and installed the directx sdk january 2010 from here: https://www.microsoft.com/en-us/download/details.aspx?id=6812 then i added reference of directx.dll from C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0 but in my program i added: using Microsoft.DirecX; but i need also the Microsoft.DirectX.DirectInput; but the DirectInput is not exist. I tried to reference also other dll's of the directx but the DirectInput not exist. Why the DirectInput of the DirectX is not exist ? – Moses Meteor Dec 09 '16 at 00:30
  • I could use the SlimDX but can't find any examples and docs about how ot use the Effect class. – Moses Meteor Dec 09 '16 at 00:33
  • The old Managed DirectX 1.1 assemblies are deprecated. See [DirectX and .NET](https://blogs.msdn.microsoft.com/chuckw/2010/12/09/directx-and-net/). DirectInput is also considered legacy, as are many joysticks themselves. What particular FFB joystick are you using? – Chuck Walbourn Dec 09 '16 at 06:15
  • @ChuckWalbourn My joystick is the ps4 dualshock wireless/cable in black. I also tried to use SharpDX but i can't get on with it getting errors and not sure what to do i opened a question here with the SharpDX: http://gamedev.stackexchange.com/questions/134123/how-do-i-get-a-list-of-all-joysticks-connected-to-my-pc-in-usb-using-sharpdx-wit – Moses Meteor Dec 09 '16 at 11:31
  • If you can, try building the C++ samples from [here](https://github.com/walbourn/directx-sdk-samples) for DirectInput to see if it works with your device. – Chuck Walbourn Dec 09 '16 at 17:20

0 Answers0