Button#

Introduction#

The button class is a derived class of the controller class. It provides access to controller button states and button event callbacks.

A button object is accessed through a controller object. It is not constructed directly.

A front and back side view of the IQ 2nd gen Controller with all buttons highlighted yellow. The surface of the controller contains two joystick buttons in the upper left and right corners, with E up and down below the left joystick and F up and down below the right. On the back of the controller are L up and down and R up and down on the left and right sides respectively.

Access#

The controller class provides the following button objects. Each object is an instance of the button class.

Object

Example Usage

Description

ButtonEDown

Controller.ButtonEDown.pressing()

The E ▼ button.

ButtonEUp

Controller.ButtonEUp.pressing()

The E ▲ button.

ButtonFDown

Controller.ButtonFDown.pressing()

The F ▼ button.

ButtonFUp

Controller.ButtonFUp.pressing()

The F ▲ button.

ButtonL3

Controller.ButtonL3.pressing()

The Left Joystick button. IQ (2nd gen) Controller only.

ButtonLDown

Controller.ButtonLDown.pressing()

The L ▼ button.

ButtonLUp

Controller.ButtonLUp.pressing()

The L ▲ button.

ButtonR3

Controller.ButtonR3.pressing()

The Right Joystick button. IQ (2nd gen) Controller only.

ButtonRDown

Controller.ButtonRDown.pressing()

The R ▼ button.

ButtonRUp

Controller.ButtonRUp.pressing()

The R ▲ button.

Notes#

  • Before using a button member function, create a controller object.

  • ButtonL3 and ButtonR3 are only available on the IQ (2nd gen) Controller.

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

Member Functions#

The button class includes the following member functions:

Getter — Read button state.

  • pressing — Returns whether the button is being pressed.

Callbacks — Run functions when button events occur.

  • pressed — Registers a function to run when the button is pressed.

  • released — Registers a function to run when the button is released.

Getter#

pressing#

pressing returns whether the button is currently being pressed.

Available Function

bool pressing() const;

Parameters

This function does not have parameters.

Return Value

Returns a Boolean value.

  • true — The button is being pressed.

  • false — The button is not being pressed.

Example

// Turn right while R Up is pressed
while (true) {
  if (Controller.ButtonRUp.pressing()) {
    Drivetrain.turn(right);
  }
  else {
    Drivetrain.stop();
  }

  wait(20, msec);
}

Callbacks#

pressed#

pressed registers a function that runs when the button is pressed.

Available Function

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

Parameter

Type

Description

callback

void (*)(void)

A function that runs when the button is pressed.

Return Value

This function does not return a value.

Example

Define the callback function outside of main.

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

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run driveForward when F Up is pressed
  Controller.ButtonFUp.pressed(driveForward);
}

released#

released registers a function that runs when the button is released.

Available Function

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

Parameter

Type

Description

callback

void (*)(void)

A function that runs when the button is released.

Return Value

This function does not return a value.

Example

Define the callback function outside of main.

// Stop the drivetrain when F Up is released
void stopDriving() {
  Drivetrain.stop();
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run stopDriving when F Up is released
  Controller.ButtonFUp.released(stopDriving);
}