按钮#
介绍#
The button class is derived from the brain base class and provides access to the IQ (2nd gen) Brain’s buttons, allowing your robot to monitor presses to the brain’s buttons.
使用权#
The brain class provides twelve button objects. Each object is an instance of the button class and can be accessed through a brain instance.
目的 |
用法示例 |
描述 |
|---|---|---|
|
|
勾选按钮 |
|
|
左键 |
|
|
右键 |
笔记#
The
buttonobject is provided by the brain. It is not constructed directly.
成员功能#
The Button class includes the following member functions:
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.pressing— Returns whether the specified button is being pressed.
Before calling any button member functions, a brain instance must be created, as shown below:
// Create a brain instance
brain();
按下#
注册一个回调函数,当按下特定的脑部按钮时运行该函数。
Available Functionsvoid pressed(
void (* callback)(void) ) const;
范围 |
类型 |
描述 |
|---|---|---|
|
|
按下指定按钮时调用的函数。 |
此函数不返回值。
Examples这个函数可以对任何大脑按钮对象调用,例如:
Brain.buttonCheckBrain.buttonLeftBrain.buttonRight
Define the callback function (outside of
int main())// Drive forward when check 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 check button is pressed Brain.buttonCheck.pressed(driveForward); }
发布#
注册一个回调函数,当特定的脑部按钮被释放时,该函数将运行。
Available Functionsvoid released(
void (* callback)(void) ) const;
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
当指定的按钮被释放时,将调用该函数。 |
此函数不返回值。
Examples这个函数可以对任何大脑按钮对象调用,例如:
Brain.buttonCheckBrain.buttonLeftBrain.buttonRight
Define the callback function (outside of
int main())// Drive backward when the left button 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 left button is released Brain.buttonLeft.released(backUp); }
紧迫#
返回特定大脑按钮当前是否被按下。
Available Functionsbool pressing() const;
此函数不接受任何参数。
Return Values返回一个布尔值,指示指定的按钮是否被按下:
true— The button is being pressed.false— The button is not being pressed.
这个函数可以对任何大脑按钮对象调用,例如:
Brain.buttonCheckBrain.buttonLeftBrain.buttonRight
// Turn right while right is pressed
while (true) {
if (Brain.buttonRight.pressing()) {
Drivetrain.turn(right);
} else {
Drivetrain.stop();
}
wait(5, msec);
}