如何使用SharpDX DirectInput检查操纵杆上的滑块?

时间:2018-05-09 18:02:54

标签: c# winforms sharpdx joystick directinput

我正在进行多操纵杆设置,我需要构建自定义按钮映射。甚至可以跨设备进行映射。例如,如果有一个单独的操纵杆和油门象限,我需要将'Z'或其他任何内容映射到油门输入。当我构建映射配置文件等时,我还需要能够检查并查看每个棒上是否有可用的滑块。我无法弄清楚如何只是说: if (joystick.HasSliders)

到目前为止,我尝试了不同的

变体
PropertyInfo rxprop = typeof(SharpDX.DirectInput.JoystickState).GetProperty("Sliders");

但是,如果操纵杆没有滑块,这当然会引发异常。如果它只有一个滑块,我将总是返回一个带有两个值的Sliders []的Int数组,无论该棒是否只有一个滑块。因此,不知道滑块是否存在,甚至存在多少滑块都不是一种保证。如果没有第二个实际滑块,则Sliders [1]的值始终为0,但如果有第二个滑块,则它仍然可以处于0位置。

目前这就是我一直在搞乱的地方:

usbjoysticks[k].Poll();
JoystickState currentState = null;

try
{
    currentState = usbjoysticks[k].GetCurrentState();

    //joystickmap.axisRangeMax = usbjoysticks[k].GetObjectInfoById(1);
    PropertyInfo rxprop = typeof(SharpDX.DirectInput.JoystickState).GetProperty("Sliders");
    var rzvalue = (int[])rxprop.GetValue(currentState, null);
    var mySlider1 = rzvalue[0];

    var datas = usbjoysticks[k].GetBufferedData();
    foreach (var state in datas)
    {
        Console.WriteLine(state);
        if (state.Offset == JoystickOffset.X)
        {
            Console.WriteLine(state.Value);
        }
    }
    //DeviceObjectInstance my = usbjoysticks[k].GetObjectInfoByName("Sliders1");
    var stuff = usbjoysticks[k].GetObjectPropertiesByName("Sliders0");
    //=================
    joystickmap.jX = "X";
    joystickmap.axisRangeMax = usbjoysticks[k].GetObjectPropertiesByName("Y").Range.Maximum; //65535
    joystickmap.jY = "Y";

    if (currentState.Z != 0)
    {
        joystickmap.jZ = "Z";
        var maxZrange = usbjoysticks[k].GetObjectPropertiesByName("Z").Range.Maximum;
        joystickmap.axisRangeMax = usbjoysticks[k].GetObjectPropertiesByName("Z").Range.Maximum;
    }

    if (currentState.Sliders[0] != 0 || currentState.Sliders[1] != 0) //TODO possible change to Joystick State value in class object instead of text. 
    {
        joystickmap.SLD1 = "Slider1";
        //joystickmap.jZ = "Sliders";
        joystickmap.jsSliders = currentState.Sliders;
        var maxSld1range = usbjoysticks[k].GetObjectPropertiesByName("Sliders0").Range.Maximum; 
        joystickmap.sld1 = joystickmap.jsSliders[0];
        joystickmap.sld2 = joystickmap.jsSliders[1];
    }

    if (currentState.Sliders[1] != 0)
    {
        joystickmap.SLD2 = "Slider2";
    }

    if (currentState.RotationX != 0)
    {
        joystickmap.rX = "RotationX";
    }

    if (currentState.RotationY != 0)
    {
        joystickmap.rY = "RotationY";
    }

    if (currentState.RotationZ != 0)
    {
        joystickmap.rZ = "RotationZ";
        //joystickmap.jZ = "RotationZ";
    }

    joystickmap.axisdivider = 2;

    for (var b = 0; b < 20; b++)
    {
        //joystickmap.joybuttons.Add(currentState.Buttons[b]);
        joystickmap.joybuttons[b] = (b + 1);
    }


    joystickmappings.Add(joystickmap);

    if (!File.Exists(filename))
    {
        File.WriteAllText(filename, JsonConvert.SerializeObject(joystickmap));
        // serialize JSON directly to a file
        using (StreamWriter file = File.CreateText(filename))
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(file, joystickmap);
        }
    }
}
catch (Exception e)
{
    MessageBox.Show("USB joystick map creation failure!\n" + e.Message);
    break;
}

0 个答案:

没有答案