1

Code so far:

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);
        }

The problem is that the info string contains only the value 0 for state.X/Y/Z and nothing in regards to buttons.

I need something like this: button_down & button_release in order to get 2 or more simultanious buttons pressed. And the axis pozisions.

Also I only use the DirectX SKD no SlimDX or anyting else.

Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43
  • Well generally speaking Gamepad input is for games. I've only used gamepads through XNA. Could you specify what type of gamepad you're using? Xbox Controller, flight stick, generic D-Pad/Analog/4Button/Triggers? – Will Custode Nov 01 '13 at 12:48
  • I'm using a wireless genius gamepad http://s1.emagst.ro/products/24/23618/images/img50860_02092009105416_350x350c_71dt.jpg – Mihai Bratulescu Nov 01 '13 at 12:51

1 Answers1

1

You'll probably have to interface with DirectX in a managed form. Check out this article on SO for more details. But essentially it's just polling for input and then processing your own events. If you have further questions beyond the "how do I get this going" feel free to comment and I'll edit if I can.

Community
  • 1
  • 1
Will Custode
  • 4,576
  • 3
  • 26
  • 51
  • I cant seem to get started with writing 1 line of code. It seems that without XNA nothing works but I dont want to make an XNA project because I'm not making a game. Can you give me a little project in visual studio that can detect gamepad input and create a msg box everytime you press a key? – Mihai Bratulescu Nov 18 '13 at 14:54
  • What you're asking for is out of order for SO. I've provided an article with sample code and other references. I can't write a project that does it for you. – Will Custode Nov 18 '13 at 19:00
  • I've changed my question – Mihai Bratulescu Nov 26 '13 at 01:00