Class Trigger
java.lang.Object
edu.wpi.first.wpilibj2.command.button.Trigger
- All Implemented Interfaces:
BooleanSupplier
- Direct Known Subclasses:
Button
public class Trigger extends Object implements BooleanSupplier
This class provides an easy way to link commands to conditions.
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.
Triggers can easily be composed for advanced functionality using the and(BooleanSupplier)
, or(BooleanSupplier)
, negate()
operators.
This class is provided by the NewCommands VendorDep
-
Constructor Summary
Constructors Constructor Description Trigger()
Deprecated.Trigger(EventLoop loop, BooleanSupplier condition)
Creates a new trigger based on the given condition.Trigger(BooleanSupplier condition)
Creates a new trigger based on the given condition. -
Method Summary
Modifier and Type Method Description Trigger
and(BooleanSupplier trigger)
Composes two triggers with logical AND.Trigger
cancelWhenActive(Command command)
Deprecated.Instead, pass this as an end condition toCommand.until(BooleanSupplier)
.Trigger
debounce(double seconds)
Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.Trigger
debounce(double seconds, Debouncer.DebounceType type)
Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.boolean
getAsBoolean()
Trigger
negate()
Creates a new trigger that is active when this trigger is inactive, i.e.Trigger
onFalse(Command command)
Starts the given command whenever the condition changes from `true` to `false`.Trigger
onTrue(Command command)
Starts the given command whenever the condition changes from `false` to `true`.Trigger
or(BooleanSupplier trigger)
Composes two triggers with logical OR.Trigger
toggleOnFalse(Command command)
Toggles a command when the condition changes from `true` to `false`.Trigger
toggleOnTrue(Command command)
Toggles a command when the condition changes from `false` to `true`.Trigger
toggleWhenActive(Command command)
Deprecated.UsetoggleOnTrue(Command)
instead.Trigger
whenActive(Command command)
Deprecated.UseonTrue(Command)
instead.Trigger
whenActive(Runnable toRun, Subsystem... requirements)
Deprecated.Replace withonTrue(Command)
, creating the InstantCommand manuallyTrigger
whenInactive(Command command)
Deprecated.UseonFalse(Command)
instead.Trigger
whenInactive(Runnable toRun, Subsystem... requirements)
Deprecated.Construct the InstantCommand manually and replace withonFalse(Command)
Trigger
whileActiveContinuous(Command command)
Deprecated.UsewhileTrue(Command)
withRepeatCommand
, or bindcommand::schedule
toBooleanEvent.ifHigh(Runnable)
(passing no requirements).Trigger
whileActiveContinuous(Runnable toRun, Subsystem... requirements)
Deprecated.UsewhileTrue(Command)
and construct a RunCommand manuallyTrigger
whileActiveOnce(Command command)
Deprecated.UsewhileTrue(Command)
instead.Trigger
whileFalse(Command command)
Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.Trigger
whileTrue(Command command)
Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.
-
Constructor Details
-
Trigger
Creates a new trigger based on the given condition.- Parameters:
loop
- The loop instance that polls this trigger.condition
- the condition represented by this trigger
-
Trigger
Creates a new trigger based on the given condition.Polled by the default scheduler button loop.
- Parameters:
condition
- the condition represented by this trigger
-
Trigger
Deprecated.Creates a new trigger that is always `false`.
-
-
Method Details
-
onTrue
Starts the given command whenever the condition changes from `false` to `true`.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
onFalse
Starts the given command whenever the condition changes from `true` to `false`.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
whileTrue
Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.Doesn't re-start the command if it ends while the condition is still `true`. If the command should restart, see
RepeatCommand
.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
whileFalse
Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.Doesn't re-start the command if it ends while the condition is still `false`. If the command should restart, see
RepeatCommand
.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
toggleOnTrue
Toggles a command when the condition changes from `false` to `true`.- Parameters:
command
- the command to toggle- Returns:
- this trigger, so calls can be chained
-
toggleOnFalse
Toggles a command when the condition changes from `true` to `false`.- Parameters:
command
- the command to toggle- Returns:
- this trigger, so calls can be chained
-
whenActive
Deprecated.UseonTrue(Command)
instead.Starts the given command whenever the trigger just becomes active.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
whenActive
Deprecated.Replace withonTrue(Command)
, creating the InstantCommand manuallyRuns the given runnable whenever the trigger just becomes active.- Parameters:
toRun
- the runnable to runrequirements
- the required subsystems- Returns:
- this trigger, so calls can be chained
-
whileActiveContinuous
Deprecated.UsewhileTrue(Command)
withRepeatCommand
, or bindcommand::schedule
toBooleanEvent.ifHigh(Runnable)
(passing no requirements).Constantly starts the given command while the button is held.Command.schedule()
will be called repeatedly while the trigger is active, and will be canceled when the trigger becomes inactive.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
whileActiveContinuous
Deprecated.UsewhileTrue(Command)
and construct a RunCommand manuallyConstantly runs the given runnable while the button is held.- Parameters:
toRun
- the runnable to runrequirements
- the required subsystems- Returns:
- this trigger, so calls can be chained
-
whileActiveOnce
Deprecated.UsewhileTrue(Command)
instead.Starts the given command when the trigger initially becomes active, and ends it when it becomes inactive, but does not re-start it in-between.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
whenInactive
Deprecated.UseonFalse(Command)
instead.Starts the command when the trigger becomes inactive.- Parameters:
command
- the command to start- Returns:
- this trigger, so calls can be chained
-
whenInactive
Deprecated.Construct the InstantCommand manually and replace withonFalse(Command)
Runs the given runnable when the trigger becomes inactive.- Parameters:
toRun
- the runnable to runrequirements
- the required subsystems- Returns:
- this trigger, so calls can be chained
-
toggleWhenActive
Deprecated.UsetoggleOnTrue(Command)
instead.Toggles a command when the trigger becomes active.- Parameters:
command
- the command to toggle- Returns:
- this trigger, so calls can be chained
-
cancelWhenActive
Deprecated.Instead, pass this as an end condition toCommand.until(BooleanSupplier)
.Cancels a command when the trigger becomes active.- Parameters:
command
- the command to cancel- Returns:
- this trigger, so calls can be chained
-
getAsBoolean
- Specified by:
getAsBoolean
in interfaceBooleanSupplier
-
and
Composes two triggers with logical AND.- Parameters:
trigger
- the condition to compose with- Returns:
- A trigger which is active when both component triggers are active.
-
or
Composes two triggers with logical OR.- Parameters:
trigger
- the condition to compose with- Returns:
- A trigger which is active when either component trigger is active.
-
negate
Creates a new trigger that is active when this trigger is inactive, i.e. that acts as the negation of this trigger.- Returns:
- the negated trigger
-
debounce
Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.- Parameters:
seconds
- The debounce period.- Returns:
- The debounced trigger (rising edges debounced only)
-
debounce
Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.- Parameters:
seconds
- The debounce period.type
- The debounce type.- Returns:
- The debounced trigger.
-