按钮#

介绍#

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.

IQ 第二代控制器的正面和背面视图,所有按钮均以黄色高亮显示。控制器表面左上角和右上角各有两个摇杆按钮,左侧摇杆下方是 E 上下键,右侧摇杆下方是 F 上下键。控制器背面左侧和右侧分别是 L 上下键和 R 上下键。

使用权#

The controller class provides the following button objects. Each object is an instance of the button class.

目的

用法示例

描述

ButtonEDown

Controller.ButtonEDown.pressing()

E ▼ 按钮。

ButtonEUp

Controller.ButtonEUp.pressing()

E ▲ 按钮。

ButtonFDown

Controller.ButtonFDown.pressing()

F ▼ 按钮。

ButtonFUp

Controller.ButtonFUp.pressing()

F ▲ 按钮。

ButtonL3

Controller.ButtonL3.pressing()

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

ButtonLDown

Controller.ButtonLDown.pressing()

L ▼ 按钮。

ButtonLUp

Controller.ButtonLUp.pressing()

L ▲ 按钮。

ButtonR3

Controller.ButtonR3.pressing()

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

ButtonRDown

Controller.ButtonRDown.pressing()

R ▼ 按钮。

ButtonRUp

Controller.ButtonRUp.pressing()

R ▲ 按钮。

笔记#

  • Before using a button member function, create a controller object.

  • ButtonL3 and ButtonR3 are only available on the IQ (2nd gen) Controller.

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

成员功能#

The button class includes the following member functions:

Getter — 读取按钮状态。

  • pressing — Returns whether the button is being pressed.

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

  • pressed — Registers a function to run when the button is pressed.

  • released — Registers a function to run when the button is released.

Getter#

紧迫#

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 R Up is pressed
while (true) {
  if (Controller.ButtonRUp.pressing()) {
    Drivetrain.turn(right);
  }
  else {
    Drivetrain.stop();
  }

  wait(20, msec);
}

回调函数#

按下#

pressed registers a function that runs when the button is pressed.

可用功能

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

范围

类型

描述

callback

void (*)(void)

按钮按下时运行的函数。

返回值

此函数不返回值。

例子

Define the callback function outside of main.

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

Register the callback inside main.

int main() {
  vexcodeInit();

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

发布#

released registers a function that runs when the button is released.

可用功能

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

范围

类型

描述

callback

void (*)(void)

按钮释放时运行的函数。

返回值

此函数不返回值。

例子

Define the callback function outside of main.

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

Register the callback inside main.

int main() {
  vexcodeInit();

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