按钮#

介绍#

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.

VEX EXP 控制器按钮以黄色高亮显示。

使用权#

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

目的

用法示例

描述

ButtonA

Controller.ButtonA.pressing()

一个按钮

ButtonB

Controller.ButtonB.pressing()

B 按钮

ButtonDown

Controller.ButtonDown.pressing()

向下按钮

ButtonL1

Controller.ButtonL1.pressing()

L1按钮

ButtonL2

Controller.ButtonL2.pressing()

L2按钮

ButtonL3

Controller.ButtonL3.pressing()

L3按钮

ButtonR1

Controller.ButtonR1.pressing()

R1按钮

ButtonR2

Controller.ButtonR2.pressing()

R2按钮

ButtonR3

Controller.ButtonR3.pressing()

R3按钮

ButtonUp

Controller.ButtonUp.pressing()

向上按钮

笔记#

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

成员功能#

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();

紧迫#

返回指定控制器按钮当前是否被按下。

Available Functions
bool pressing() const;

Parameters

此函数不接受任何参数。

Return Values

返回一个整数,指示指定的按钮是否被按下:

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

这个函数可以对任何控制器按钮对象调用,例如:

  • Controller.ButtonA
  • Controller.ButtonB
  • Controller.ButtonDown
  • Controller.ButtonL1
  • Controller.ButtonL2
  • Controller.ButtonL3
  • Controller.ButtonR1
  • Controller.ButtonR2
  • Controller.ButtonR3
  • Controller.ButtonUp
// Turn right while L1 is pressed
while (true) {
    if (Controller.ButtonL1.pressing()) {
        Drivetrain.turn(right);
    } else {
        Drivetrain.stop();
    }
    wait(5, msec);
}

按下#

注册一个回调函数,当按下特定的控制器按钮时,该函数将运行。

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

Parameters

范围

类型

描述

callback

void (*)(void)

按下指定按钮时调用的函数。

Return Values

此函数不返回值。

Examples

这个函数可以对任何控制器按钮对象调用,例如:

  • Controller.ButtonA
  • Controller.ButtonB
  • Controller.ButtonDown
  • Controller.ButtonL1
  • Controller.ButtonL2
  • Controller.ButtonL3
  • Controller.ButtonR1
  • Controller.ButtonR2
  • Controller.ButtonR3
  • Controller.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);
}

发布#

注册一个回调函数,当特定控制器按钮被释放时,该函数将运行。

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

Parameters

范围

类型

描述

callback

void (*)(void)

当指定的按钮被释放时,将调用该函数。

Return Values

此函数不返回值。

Examples

这个函数可以对任何控制器按钮对象调用,例如:

  • Controller.ButtonA
  • Controller.ButtonB
  • Controller.ButtonDown
  • Controller.ButtonL1
  • Controller.ButtonL2
  • Controller.ButtonL3
  • Controller.ButtonR1
  • Controller.ButtonR2
  • Controller.ButtonR3
  • Controller.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);
}