检测游戏手柄输入

时间:2013-11-01 12:45:13

标签: c# gamepad

到目前为止

代码:

Device gamepad;
public bool initializeGamePad()
        {
            foreach ( DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly) )
            {
                gamepad = new Device(di.InstanceGuid);
                break;
            }

            if (gamepad==null)//no gamepads detected
                return false;
            else
            {
                configureGamePad();
                return true;
            }
        }

        public void configureGamePad()
        {
            //Set axis ranges
            foreach (DeviceObjectInstance doi in gamepad.Objects)
            {
                if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
                {
                    gamepad.Properties.SetRange(ParameterHow.ById, doi.ObjectId, new InputRange(-5000, 5000));
                }
            }

            //Set joystick axis mode absolute
            gamepad.Properties.AxisModeAbsolute = true;

            //set cooperative level.
            gamepad.SetCooperativeLevel(new Form1(), CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);

            //Acquire devices for capturing
            gamepad.Acquire();

            UpdateJoystick();
        }

        private void UpdateJoystick()
        {
            string info = "Joystick: ";

            //Get Mouse State
            JoystickState state = gamepad.CurrentJoystickState;

            //Capture Position
            info += "X:" + state.X + " ";
            info += "Y:" + state.Y + " ";
            info += "Z:" + state.Z + " ";
            info += "ARx:" + state. + "\n";

            //Capture Buttons
            byte[] buttons = state.GetButtons();
            for (int i = 0; i < buttons.Length; i++)
            {
                if (buttons[i] != 0)
                {
                    info += "Button:" + i + " ";
                }
            }

            MessageBox.Show(info);
        }

问题是信息字符串仅包含state.X / Y / Z的值0,而没有关于按钮的值。

我需要这样的东西:button_down&amp; button_release以便按下2个或更多同时按钮。和轴的pozisions。

此外,我只使用DirectX SKD no SlimDX或其他任何内容。

1 个答案:

答案 0 :(得分:1)

您可能需要以托管形式与DirectX连接。有关详细信息,请查看SO上的this article。但实质上它只是轮询输入然后处理自己的事件。如果除了“我如何实现这一目标”之外还有其他问题,请随时发表评论,如果可以,我会进行编辑。