Class Button
- java.lang.Object
-
- edu.wpi.first.wpilibj2.command.button.Trigger
-
- edu.wpi.first.wpilibj2.command.button.Button
-
- Direct Known Subclasses:
InternalButton,JoystickButton,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 Summary
Constructors Constructor Description Button()Default constructor; creates a button that is never pressed (unlessTrigger.get()is overridden).Button(BooleanSupplier isPressed)Creates a new button with the given condition determining whether it is pressed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ButtoncancelWhenPressed(Command command)Cancels the command when the button is pressed.ButtontoggleWhenPressed(Command command)Toggles the command whenever the button is pressed (on then off then on).ButtontoggleWhenPressed(Command command, boolean interruptible)Toggles the command whenever the button is pressed (on then off then on).ButtonwhenHeld(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.ButtonwhenHeld(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.ButtonwhenPressed(Command command)Starts the given command whenever the button is newly pressed.ButtonwhenPressed(Command command, boolean interruptible)Starts the given command whenever the button is newly pressed.ButtonwhenPressed(Runnable toRun, Subsystem... requirements)Runs the given runnable whenever the button is newly pressed.ButtonwhenReleased(Command command)Starts the command when the button is released.ButtonwhenReleased(Command command, boolean interruptible)Starts the command when the button is released.ButtonwhenReleased(Runnable toRun, Subsystem... requirements)Runs the given runnable when the button is released.ButtonwhileHeld(Command command)Constantly starts the given command while the button is held.ButtonwhileHeld(Command command, boolean interruptible)Constantly starts the given command while the button is held.ButtonwhileHeld(Runnable toRun, Subsystem... requirements)Constantly runs the given runnable while the button is held.-
Methods inherited from class edu.wpi.first.wpilibj2.command.button.Trigger
and, cancelWhenActive, get, negate, or, toggleWhenActive, toggleWhenActive, whenActive, whenActive, whenActive, whenInactive, whenInactive, whenInactive, whileActiveContinuous, whileActiveContinuous, whileActiveContinuous, whileActiveOnce, whileActiveOnce
-
-
-
-
Constructor Detail
-
Button
public Button()
Default constructor; creates a button that is never pressed (unlessTrigger.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 Detail
-
whenPressed
public Button whenPressed(Command command, boolean interruptible)
Starts the given command whenever the button is newly pressed.- Parameters:
command- the command to startinterruptible- 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 runrequirements- 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 startinterruptible- 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 runrequirements- 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 startinterruptible- 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 startinterruptible- 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 runrequirements- 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 startinterruptible- whether the command is interruptible
-
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
-
-