按钮#

介绍#

button 类是 controller 类的派生类。它提供对控制器按钮状态和按钮事件回调的访问。

button 对象是通过 controller 对象访问的,它不是直接构造的。

第一代 IQ 控制器有八个主要按钮。如果将第二代 IQ 控制器连接到第一代 IQ 主机,则摇杆按钮 ButtonL3 和 ButtonR3 也可以使用。

IQ 控制器所有按钮高亮显示。

使用权#

controller 类提供以下按钮对象。每个对象都是 button 类的一个实例。

目的

用法示例

描述

ButtonEDown

Controller.ButtonEDown.pressing()

E ▼ 按钮。

ButtonEUp

Controller.ButtonEUp.pressing()

E ▲ 按钮。

ButtonFDown

Controller.ButtonFDown.pressing()

F ▼ 按钮。

ButtonFUp

Controller.ButtonFUp.pressing()

F ▲ 按钮。

ButtonLDown

Controller.ButtonLDown.pressing()

L ▼ 按钮。

ButtonLUp

Controller.ButtonLUp.pressing()

L ▲ 按钮。

ButtonRDown

Controller.ButtonRDown.pressing()

R ▼ 按钮。

ButtonRUp

Controller.ButtonRUp.pressing()

R ▲ 按钮。

ButtonL3

Controller.ButtonL3.pressing()

左摇杆按钮。仅适用于 IQ(第二代)控制器。

ButtonR3

Controller.ButtonR3.pressing()

右摇杆按钮。仅适用于 IQ(第二代)控制器。

笔记#

  • 在使用 button 成员函数之前,请创建一个 controller 对象。

  • ButtonL3 和 ButtonR3 仅在连接 IQ(第二代)控制器时可用。

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

成员功能#

button 类包含以下成员函数:

Getter — 读取按钮状态。

  • pressing — 返回按钮是否被按下。

回调函数——当按钮事件发生时运行的函数。

  • pressed — 注册一个在按钮被按下时运行的函数。

  • released — 注册一个在按钮释放时运行的函数。

Getter#

pressing#

pressing 返回按钮当前是否被按下。

可用功能

bool pressing() const;

参数

此函数没有参数。

返回值

返回布尔值。

  • true — 按钮被按下。

  • false — 按钮未被按下。

例子

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

  wait(20, msec);
}

回调函数#

pressed#

pressed 注册一个在按钮按下时运行的函数。

可用功能

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

范围

类型

描述

callback

void (*)(void)

按钮按下时运行的函数。

返回值

此函数不返回值。

例子

在 main之外定义回调函数。

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

在 main中注册回调。

int main() {
  vexcodeInit();

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

released#

released 注册一个在按钮释放时运行的函数。

可用功能

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

范围

类型

描述

callback

void (*)(void)

按钮释放时运行的函数。

返回值

此函数不返回值。

例子

在 main之外定义回调函数。

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

在 main中注册回调。

int main() {
  vexcodeInit();

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