Telling If a Button is Held Down tOnegod

Hi there, I have a small 4 button control panel, up down left right, you get it.

Im trying to have a boolean read true while the button is held down, and once released to read false.

The background behind the project, is it’s a simple control pad for an android application.

Currently the results (as expected) end in a toggle result, in which pressing the button essentially makes you go that direction until otherwise told.

Here is the culprit method

[java] public void createControlPad(){

ButtonAdapter upButton = new ButtonAdapter( screen, "UpButton", new Vector2f(15, 15) ) {
  @Override
    public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
      if (!toggled)
      player.speed = -20;
      else
      player.speed = 0;
      }
    };

ButtonAdapter downButton = new ButtonAdapter( screen, "DownButton", new Vector2f(15, 15) ) {
  @Override
    public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
      if (!toggled)
      player.speed = 5;
      else
      player.speed = 0;
      }
    };

ButtonAdapter leftButton = new ButtonAdapter( screen, "LeftButton", new Vector2f(15, 15) ) {
  @Override
    public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
      if (!toggled)
      player.turn = 10;
      }
  };

 ButtonAdapter rightButton = new ButtonAdapter( screen, "RightButton", new Vector2f(15, 15) ) {
   @Override
    public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
      if (!toggled)
      player.turn = -10;
      else
      player.turn = 0;
      }
    };
}

[/java]

Any button can be set to do this sort of thing by calling:

[java]
ButtonAdapter.setInterval(float intervalsPerSecond);
[/java]

Then override:

[java]
@Override
public void onButtonStillPressedInterval() {
// This is called at the set interval while the button is pressed.
}
[/java]

This way it can be controlled by the actually press/hold/release instead of trying to use toggle state.

Might be a dumb question, is there an onButtonRelease method?

@BigBob said: Might be a dumb question, is there an onButtonRelease method?

Yep yep…

[java]
public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled);
public void onButtonMouseRightUp(MouseButtonEvent evt, boolean toggled);
[/java]

If you implement MouseWheelListener and TouchListener, you also have:

[java]
void onMouseWheelReleased(MouseButtonEvent evt);
void onTouchUp(TouchEvent evt);
[/java]

Though, the touch listener isn’t necessary, as screen will create and forward mouse events to the mouse listener’s left button methods for you