如何从C#向操纵杆发送反馈/效果?

时间:2016-12-08 16:23:50

标签: c# directx slimdx

我找到了这个答案,但我使用的是SlimDX,答案是使用DirectX:

Answer with directx

也许我应该安装directx sdk?如果是这样从哪里下载呢?

到目前为止,我使用slimdx执行的代码是:

问题是SlimDX中的Effect类与Directx中的Effect类不同。所以我不能在链接中使用答案,因为SlimDX效果中不存在DirectX中效果的所有属性。

我的目标是将命令自动发送到连接到我的电脑usb的操纵杆。命令我的意思是按下按钮效果,例如方形按钮或三角形按钮或圆圈或X和其他按钮。

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


        }
    }
}

更新

我也试过使用SharpDX,但是我遇到了错误而不确定如何继续使用它。首先我试图列出连接到我的电脑但却出错的操纵杆。后来我想做我的主要目标:

我的目标是将命令自动发送到连接到我的电脑usb的操纵杆。命令我的意思是按下按钮效果,例如方形按钮或三角形按钮或圆形或X和其他按钮。

但我想这段代码甚至不是开始的正确方法。

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

        }
}

0 个答案:

没有答案