Button#

Introduction#

The button class is derived from the controller base class and provides access to the IQ Controller’s buttons, allowing your robot to monitor presses to the controller’s buttons.

A VEX IQ Controller buttons highlighted yellow.

Access#

The controller class provides twelve button objects. Each object is an instance of the button class and can be accessed through a controller instance.

Object

Example Usage

Description

ButtonEDown

Controller.ButtonEDown.pressing()

E Down button

ButtonEUp

Controller.ButtonEUp.pressing()

E Up button

ButtonFDown

Controller.ButtonFDown.pressing()

F Down button

ButtonFUp

Controller.ButtonFUp.pressing()

F Up button

ButtonL3

Controller.ButtonL3.pressing()

L3 button (Only on the 2nd Gen Controller)

ButtonLDown

Controller.ButtonLDown.pressing()

L Down button

ButtonLUp

Controller.ButtonLUp.pressing()

L Up button

ButtonR3

Controller.ButtonR3.pressing()

R3 button (Only on the 2nd Gen Controller)

ButtonRDown

Controller.ButtonRDown.pressing()

R Down button

ButtonRUp

Controller.ButtonRUp.pressing()

R Up button

Notes#

  • The button object is provided by the controller. It is not constructed directly.

Member Functions#

The Button class includes the following member functions:

  • pressing — Returns whether or not the specified button is being pressed.

  • pressed — Registers a function to be called when the specified button is pressed.

  • released — Registers a function to be called when the specified button is released.

Before calling any button member functions, a controller instance must be created, as shown below:

// Create a controller instance
controller Controller = controller();

pressing#

Returns whether a specific controller button is currently being pressed.

Available Functions
bool pressing() const;

Parameters

This function does not accept any parameters.

Return Values

Returns an integer indicating whether the specified button is being pressed:

  • 1 — The button is being pressed.
  • 0 — The button is not being pressed.
Examples

This function can be called on any controller button object, for example:

  • Controller.ButtonEDown
  • Controller.ButtonEUp
  • Controller.ButtonFDown
  • Controller.ButtonFUp
  • Controller.ButtonL3
  • Controller.ButtonLDown
  • Controller.ButtonLUp
  • Controller.ButtonR3
  • Controller.ButtonRDown
  • Controller.ButtonRUp
// Turn right while RUp is pressed
while (true) {
    if (Controller.ButtonRUp.pressing()) {
        Drivetrain.turn(right);
    } else {
        Drivetrain.stop();
    }
    wait(5, msec);
}

pressed#

Registers a callback function that runs when a specific controller button is pressed.

Available Functions
void pressed( 
  void (* callback)(void) ) const;

Parameters

Parameter

Type

Description

callback

void (*)(void)

A function that is called when the specified button is pressed.

Return Values

This function does not return a value.

Examples

This function can be called on any controller button object, for example:

  • Controller.ButtonEDown
  • Controller.ButtonEUp
  • Controller.ButtonFDown
  • Controller.ButtonFUp
  • Controller.ButtonL3
  • Controller.ButtonLDown
  • Controller.ButtonLUp
  • Controller.ButtonR3
  • Controller.ButtonRDown
  • Controller.ButtonRUp

Define the callback function (outside of int main())

// Drive forward when FUp is pressed
void driveForward() {
    Drivetrain.driveFor(forward, 100, mm);
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call driveForward when the FUp button is pressed
  Controller.ButtonFUp.pressed(driveForward);
}

released#

Registers a callback function that runs when a specific controller button is released.

Available Functions
void released( 
  void (* callback)(void) ) const;

Parameters

Parameter

Type

Description

callback

void (*)(void)

A function that is called when the specified button is released.

Return Values

This function does not return a value.

Examples

This function can be called on any controller button object, for example:

  • Controller.ButtonEDown
  • Controller.ButtonEUp
  • Controller.ButtonFDown
  • Controller.ButtonFUp
  • Controller.ButtonL3
  • Controller.ButtonLDown
  • Controller.ButtonLUp
  • Controller.ButtonR3
  • Controller.ButtonRDown
  • Controller.ButtonRUp

Define the callback function (outside of int main())

// Drive backward when FDown is released
void backUp() {
    Drivetrain.driveFor(reverse, 100, mm);
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call backUp when the FDown button is released
  Controller.ButtonA.released(backUp);
}