Class Button

java.lang.Object
edu.wpi.first.wpilibj2.command.button.Trigger
edu.wpi.first.wpilibj2.command.button.Button
All Implemented Interfaces:
BooleanSupplier
Direct Known Subclasses:
InternalButton, JoystickButton, NetworkButton, POVButton

public class Button
extends Trigger
This class provides an easy way to link commands to OI inputs.

It is very easy to link a button to a command. For instance, you could link the trigger button of a joystick to a "score" command.

This class represents a subclass of Trigger that is specifically aimed at buttons on an operator interface as a common use case of the more generalized Trigger objects. This is a simple wrapper around Trigger with the method names renamed to fit the Button object use.

  • Constructor Details

    • Button

      public Button()
      Default constructor; creates a button that is never pressed (unless Trigger.get() is overridden).
    • Button

      public Button​(BooleanSupplier isPressed)
      Creates a new button with the given condition determining whether it is pressed.
      Parameters:
      isPressed - returns whether or not the trigger should be active
  • Method Details

    • whenPressed

      public Button whenPressed​(Command command, boolean interruptible)
      Starts the given command whenever the button is newly pressed.
      Parameters:
      command - the command to start
      interruptible - whether the command is interruptible
      Returns:
      this button, so calls can be chained
    • whenPressed

      public Button whenPressed​(Command command)
      Starts the given command whenever the button is newly pressed. The command is set to be interruptible.
      Parameters:
      command - the command to start
      Returns:
      this button, so calls can be chained
    • whenPressed

      public Button whenPressed​(Runnable toRun, Subsystem... requirements)
      Runs the given runnable whenever the button is newly pressed.
      Parameters:
      toRun - the runnable to run
      requirements - the required subsystems
      Returns:
      this button, so calls can be chained
    • whileHeld

      public Button whileHeld​(Command command, boolean interruptible)
      Constantly starts the given command while the button is held.

      Command.schedule(boolean) will be called repeatedly while the button is held, and will be canceled when the button is released.

      Parameters:
      command - the command to start
      interruptible - whether the command is interruptible
      Returns:
      this button, so calls can be chained
    • whileHeld

      public Button whileHeld​(Command command)
      Constantly starts the given command while the button is held.

      Command.schedule(boolean) will be called repeatedly while the button is held, and will be canceled when the button is released. The command is set to be interruptible.

      Parameters:
      command - the command to start
      Returns:
      this button, so calls can be chained
    • whileHeld

      public Button whileHeld​(Runnable toRun, Subsystem... requirements)
      Constantly runs the given runnable while the button is held.
      Parameters:
      toRun - the runnable to run
      requirements - the required subsystems
      Returns:
      this button, so calls can be chained
    • whenHeld

      public Button whenHeld​(Command command, boolean interruptible)
      Starts the given command when the button is first pressed, and cancels it when it is released, but does not start it again if it ends or is otherwise interrupted.
      Parameters:
      command - the command to start
      interruptible - whether the command is interruptible
      Returns:
      this button, so calls can be chained
    • whenHeld

      public Button whenHeld​(Command command)
      Starts the given command when the button is first pressed, and cancels it when it is released, but does not start it again if it ends or is otherwise interrupted. The command is set to be interruptible.
      Parameters:
      command - the command to start
      Returns:
      this button, so calls can be chained
    • whenReleased

      public Button whenReleased​(Command command, boolean interruptible)
      Starts the command when the button is released.
      Parameters:
      command - the command to start
      interruptible - whether the command is interruptible
      Returns:
      this button, so calls can be chained
    • whenReleased

      public Button whenReleased​(Command command)
      Starts the command when the button is released. The command is set to be interruptible.
      Parameters:
      command - the command to start
      Returns:
      this button, so calls can be chained
    • whenReleased

      public Button whenReleased​(Runnable toRun, Subsystem... requirements)
      Runs the given runnable when the button is released.
      Parameters:
      toRun - the runnable to run
      requirements - the required subsystems
      Returns:
      this button, so calls can be chained
    • toggleWhenPressed

      public Button toggleWhenPressed​(Command command, boolean interruptible)
      Toggles the command whenever the button is pressed (on then off then on).
      Parameters:
      command - the command to start
      interruptible - whether the command is interruptible
      Returns:
      this button, so calls can be chained
    • toggleWhenPressed

      public Button toggleWhenPressed​(Command command)
      Toggles the command whenever the button is pressed (on then off then on). The command is set to be interruptible.
      Parameters:
      command - the command to start
      Returns:
      this button, so calls can be chained
    • cancelWhenPressed

      public Button cancelWhenPressed​(Command command)
      Cancels the command when the button is pressed.
      Parameters:
      command - the command to start
      Returns:
      this button, so calls can be chained