按钮#
介绍#
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.

使用权#
The controller class provides the following button objects. Each object is an instance of the button class.
目的 |
用法示例 |
描述 |
|---|---|---|
|
|
A 键。 |
|
|
B 键。 |
|
|
向下按钮。 |
|
|
向上按钮。 |
|
|
L1 按钮。 |
|
|
L2 按钮。 |
|
|
L3 按钮。 |
|
|
R1按钮。 |
|
|
R2 按钮。 |
|
|
R3按钮。 |
笔记#
Before using a
buttonmember function, create acontrollerobject.
// Create a controller object
controller Controller = controller();
成员功能#
The button class includes the following member functions:
Getter — 读取按钮状态。
pressing— Returns whether the button is being pressed.
回调函数——当按钮事件发生时运行的函数。
Getter#
pressing#
pressing returns whether the button is currently being pressed.
可用功能
bool pressing() const;
参数
此函数没有参数。
返回值
返回布尔值。
true— The button is being pressed.false— The button is not being pressed.
例子
// Turn right while L1 is pressed
while (true) {
if (Controller.ButtonL1.pressing()) {
Drivetrain.turn(right);
}
else {
Drivetrain.stop();
}
wait(20, msec);
}
回调函数#
pressed#
pressed registers a function that runs when the button is pressed.
可用功能
void pressed(void (* callback)(void)) const;
范围 |
类型 |
描述 |
|---|---|---|
|
|
按钮按下时运行的函数。 |
返回值
此函数不返回值。
例子
Define the callback function outside of main.
// Drive forward when R1 is pressed
void driveForward() {
Drivetrain.driveFor(forward, 200, mm);
}
Register the callback inside main.
int main() {
vexcodeInit();
// Run driveForward when R1 is pressed
Controller.ButtonR1.pressed(driveForward);
}
released#
released registers a function that runs when the button is released.
可用功能
void released(void (* callback)(void)) const;
范围 |
类型 |
描述 |
|---|---|---|
|
|
按钮释放时运行的函数。 |
返回值
此函数不返回值。
例子
Define the callback function outside of main.
// Stop the drivetrain when R1 is released
void stopDriving() {
Drivetrain.stop();
}
Register the callback inside main.
int main() {
vexcodeInit();
// Run stopDriving when R1 is released
Controller.ButtonR1.released(stopDriving);
}