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 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
Modifier and Type Method Description Button
cancelWhenPressed(Command command)
Cancels the command when the button is pressed.Button
toggleWhenPressed(Command command)
Toggles the command whenever the button is pressed (on then off then on).Button
toggleWhenPressed(Command command, boolean interruptible)
Toggles the command whenever the button is pressed (on then off then on).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.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.Button
whenPressed(Command command)
Starts the given command whenever the button is newly pressed.Button
whenPressed(Command command, boolean interruptible)
Starts the given command whenever the button is newly pressed.Button
whenPressed(Runnable toRun, Subsystem... requirements)
Runs the given runnable whenever the button is newly pressed.Button
whenReleased(Command command)
Starts the command when the button is released.Button
whenReleased(Command command, boolean interruptible)
Starts the command when the button is released.Button
whenReleased(Runnable toRun, Subsystem... requirements)
Runs the given runnable when the button is released.Button
whileHeld(Command command)
Constantly starts the given command while the button is held.Button
whileHeld(Command command, boolean interruptible)
Constantly starts the given command while the button is held.Button
whileHeld(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, debounce, debounce, get, getAsBoolean, negate, or, toggleWhenActive, toggleWhenActive, whenActive, whenActive, whenActive, whenInactive, whenInactive, whenInactive, whileActiveContinuous, whileActiveContinuous, whileActiveContinuous, whileActiveOnce, whileActiveOnce
-
Constructor Details
-
Button
public Button()Default constructor; creates a button that is never pressed (unlessTrigger.get()
is overridden). -
Button
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
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
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
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
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
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
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
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
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
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
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
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
Toggles the command whenever the button is pressed (on then off then on).- Parameters:
command
- the command to startinterruptible
- whether the command is interruptible- Returns:
- this button, so calls can be chained
-
toggleWhenPressed
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
Cancels the command when the button is pressed.- Parameters:
command
- the command to start- Returns:
- this button, so calls can be chained
-