SlimDX和360控制器

时间:2018-06-06 20:57:34

标签: c# input slimdx xbox360

好吧,现在我正在使用SlimDX在visual studio中使用控制台C#app来弄清楚如何访问XBox 360控制器上的不同按钮以用于不同的项目。不过,我有点卡住,因为我无法弄清楚如何访问触发器和正确的操纵杆。这就是我所说的LoadIn,以便将键盘和控制器连接到计算机上:

    public void LoadIn()
    {
        List<SlimDX.DirectInput.Joystick> joysticks = new List<Joystick>();
        List<SlimDX.DirectInput.Keyboard> keyboards = new List<Keyboard>();
        Console.WriteLine("Loading...");
        foreach (DeviceInstance device in input.GetDevices())
        {
            try
            {
                if ((int)device.Type == 19)//keyboard
                {
                    keyboard = new SlimDX.DirectInput.Keyboard(input);
                    keyboard.Acquire();
                    foreach (DeviceObjectInstance deviceObject in keyboard.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            keyboard.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    Console.WriteLine("Adding Keyboard");
                    keyboards.Add(keyboard);
                }
                else if((int)device.Type == 21) //joystick
                {
                    stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    Console.WriteLine("Adding Joystick");
                    joysticks.Add(stick);
                }
            }
            catch(DirectInputException)
            {

            }
        }
        sticks = joysticks.ToArray();
        keyboardsUser = keyboards.ToArray();
        Console.WriteLine("Load Complete with {0} sticks found.", joysticks.Count);
    }

我知道使用XInput会让我访问Gamepad类型,但我无法弄清楚如何使用它来获取不同的附加控制器。所以这可能是我可以追求的一个解决方案。另一个是在这个功能

public void ControllerStateDisplay()
    {          
        #region Getting Button Numbers
        previousState = sticks[0].GetCurrentState();
        yValue = -previousState.Y;
        xValue = previousState.X;
        zValue = previousState.Z;
        Console.WriteLine("X/Y/Z Values \n{0}/{1}/{2}", xValue, yValue, zValue);//Not sure what changes xyz values here

        Console.WriteLine("Ready for button presses!");
        timer1 = buttonInputBuffer*100000;

        while (isRunning == true) 
        {
            buttons = sticks[0].GetCurrentState().GetButtons();
            keyStates = keyboardsUser[0].GetCurrentState().PressedKeys.ToArray();
            pointOfViewController = sticks[0].GetCurrentState().GetPointOfViewControllers();
            //Console.WriteLine(pointOfViewController.Length.ToString()); returns 4?
            joystickSliders = sticks[0].GetCurrentState().GetAccelerationSliders(); //force, velocity, regualr
            //Console.WriteLine(buttons.ToString());
            if (keyStates.Count() > 0)
            {
                for (int i = 0; i < keyStates.Count(); i++)
                {
                    if (keyStates[i] == Key.Escape)
                    {
                        Console.WriteLine("Press Enter to exit");
                        Console.ReadLine();
                        return;
                    }
                }
            }
            for (int i = 0; i < buttons.Length; i++)
            {
                if (buttons[i] && timer1 <= 50)
                {
                    Console.WriteLine("Button was just pressed: " + i.ToString());
                    buttons[i] = false;
                    timer1 = buttonInputBuffer*100000;
                } else if (timer1 > 50) {
                    timer1--;
                    //Console.WriteLine(timer1.ToString());
                }
            }
            for (int i=0; i < pointOfViewController.Length; i++)
            {
                if (pointOfViewController[i] >= 0 && timer1 <= 50) //has to be equal to zero, dpad up is 0 on 360 controller
                {
                    Console.WriteLine("{1}'s Point of view controller return: {0}", pointOfViewController[i], i);
                    pointOfViewController[0] = -1;
                    timer1 = buttonInputBuffer * 10000;

                } else if (timer1 > 50)
                {
                    timer1--;
                }
            }
            /*for (int i = 0; i < sticks.Length; i++)
            {
                if (timer1 <= 50)
                {
                    previousState = sticks[i].GetCurrentState();
                    yValue = -previousState.Y;
                    xValue = previousState.X;
                    zValue = previousState.Z;
                    Console.WriteLine("{3}'s X/Y/Z Values \n{0}/{1}/{2}", xValue, yValue, zValue, i);
                    timer1 = buttonInputBuffer * 10000;
                }else{
                    timer1--;
                }
            }*/
            /*for (int i =0; i<joystickSliders.Length; i++)
            {
                if (timer1 <= 50)
                {
                    Console.WriteLine("{0}'s slider value: {1}", i, joystickSliders[i]);
                }
                else
                {
                    timer1--;
                }
            }*/
        }
        #endregion
    }

我怀疑它与pointOfViewController有关,因为它告诉我那里有4个,但只通过第一个(想知道这是否是for和foreach循环之间的区别)。但在我为此编写完整的其他块之前,我想我会问这里,看看我是否有任何结果。

0 个答案:

没有答案