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

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 |
描述 |
|---|---|---|
|
|
A button |
|
|
B button |
|
|
Down button |
|
|
L1 button |
|
|
L2 button |
|
|
L3 button |
|
|
R1 button |
|
|
R2 button |
|
|
R3 button |
|
|
Up button |
Notes#
The
buttonobject 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 an EXP controller instance
controller Controller = controller();
pressing#
Returns whether a specific controller button is currently being pressed.
Available Functionsbool pressing() const;
This function does not accept any parameters.
Return ValuesReturns an integer indicating whether the specified button is being pressed:
1— The button is being pressed.0— The button is not being pressed.
This function can be called on any controller button object, for example:
Controller.ButtonAController.ButtonBController.ButtonDownController.ButtonL1Controller.ButtonL2Controller.ButtonL3Controller.ButtonR1Controller.ButtonR2Controller.ButtonR3Controller.ButtonUp
// Turn right while L1 is pressed
while (true) {
if (Controller.ButtonL1.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 Functionsvoid pressed(
void (* callback)(void) ) const;
范围 |
Type |
描述 |
|---|---|---|
|
|
A function that is called when the specified button is pressed. |
This function does not return a value.
ExamplesThis function can be called on any controller button object, for example:
Controller.ButtonAController.ButtonBController.ButtonDownController.ButtonL1Controller.ButtonL2Controller.ButtonL3Controller.ButtonR1Controller.ButtonR2Controller.ButtonR3Controller.ButtonUp
Define the callback function (outside of
int main())// Drive forward when A 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 A button is pressed Controller.ButtonA.pressed(driveForward); }
released#
Registers a callback function that runs when a specific controller button is released.
Available Functionsvoid released(
void (* callback)(void) ) const;
范围 |
Type |
描述 |
|---|---|---|
|
|
A function that is called when the specified button is released. |
This function does not return a value.
ExamplesThis function can be called on any controller button object, for example:
Controller.ButtonAController.ButtonBController.ButtonDownController.ButtonL1Controller.ButtonL2Controller.ButtonL3Controller.ButtonR1Controller.ButtonR2Controller.ButtonR3Controller.ButtonUp
Define the callback function (outside of
int main())// Drive backward when A 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 A button is released Controller.ButtonA.released(backUp); }