ボタン#

導入#

button クラスは controller クラスの派生クラスです。コントローラーのボタンの状態とボタンのイベントコールバックへのアクセスを提供します。

button オブジェクトは、 controller オブジェクトを介してアクセスされます。直接構築されるものではありません。

IQ第2世代コントローラーの前面と背面図。すべてのボタンが黄色で強調表示されています。コントローラーの表面には、左上と右上に2つのジョイスティックボタンがあり、左ジョイスティックの下にE(上下)、右ジョイスティックの下にF(上下)ボタンがあります。コントローラーの背面には、左側にL(上下)、右側にR(上下)ボタンがあります。

アクセス#

controller クラスは、以下のボタン オブジェクトを提供します。各オブジェクトは button クラスのインスタンスです。

物体

使用例

説明

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(第2世代)コントローラーのみ。

ButtonLDown

Controller.ButtonLDown.pressing()

L ▼ボタン。

ButtonLUp

Controller.ButtonLUp.pressing()

L▲ボタン。

ButtonR3

Controller.ButtonR3.pressing()

右ジョイスティックボタン。IQ(第2世代)コントローラーのみ。

ButtonRDown

Controller.ButtonRDown.pressing()

R ▼ボタン。

ButtonRUp

Controller.ButtonRUp.pressing()

R▲ボタン。

注記#

  • button メンバー関数を使用する前に、 controller オブジェクトを作成します。

  • ButtonL3 と ButtonR3 は、IQ (第 2 世代) コントローラーでのみ使用可能です。

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

メンバーの機能#

button クラスには、以下のメンバ関数が含まれています。

ゲッター — ボタンの状態を読み取る。

  • pressing — ボタンが押されているかどうかを返します。

コールバック — ボタンイベントが発生したときに実行される関数。

  • pressed — ボタンが押されたときに実行される関数を登録します。

  • released — ボタンが離されたときに実行される関数を登録します。

ゲッター#

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