Game Development Community

Why are Gamepad button presses easier than Keyboard button presses?

by Randy Lutcavich · in Torque X 2D · 06/15/2010 (10:41 pm) · 3 replies

Why is it that I can call a function (_OnBackButton) with just a Gamepad button press but when I want to do something after a Keyboard button press I have to work with the Move variable and ticks??

if (gamepadId >= 0)
            {
                inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.LeftThumbX, MoveMapTypes.StickAnalogHorizontal, 0);
                inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.LeftThumbY, MoveMapTypes.StickAnalogVertical, 0);
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Back, _OnBackButton);
            }


            if (keyboardId >= 0)
            {
                inputMap.BindMove(keyboardId, (int)Keys.H, MoveMapTypes.Button, 0);
                inputMap.BindMove(keyboardId, (int)Keys.D, MoveMapTypes.Button, 1);
                inputMap.BindMove(keyboardId, (int)Keys.S, MoveMapTypes.Button, 2);
                inputMap.BindMove(keyboardId, (int)Keys.Enter, MoveMapTypes.Button, 3);
                inputMap.BindMove(keyboardId, (int)Keys.Left, MoveMapTypes.Button, 4);
                inputMap.BindMove(keyboardId, (int)Keys.Right, MoveMapTypes.Button, 5);
            }

I just want to deal a card with a button press but I'm having trouble working with ticks. Why can't I just call a function when the player presses Enter??

#1
06/16/2010 (6:28 am)
You don't. The difference is in the bindMove vs bindAction command.
#2
06/16/2010 (2:18 pm)
You should be able to use bindAction for keyboards and gamepads as far as I know.

Try
inputMap.BindAction(keyboardId, (int)Keys.Enter, _OnEnterButton);

instead of
inputMap.BindMove(keyboardId, (int)Keys.Enter, MoveMapTypes.Button, 3);

Then just make a method:
private void _OnEnterButton(float val) {
         if(val > 0.0f) {
            // your stuff here
         }
      }

Or you could do:

inputMap.BindCommand(keyboardId, (int)Keys.Enter, onEnterDown, onEnterUp);

      public void onEnterDown() {
         // your stuff here
      }

      public void onEnterUp() {
         // your stuff here
      }
#3
06/16/2010 (11:03 pm)
Thank you both.

I used your second suggestion Cosmic and it works perfectly.