I found this answer but I'm using SlimDX and the answer is using 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)
{
}
}