跳到正文
本頁目錄

Input API

作用

Input API 讓原生模組監聽觸摸、按鍵、鼠標事件,並控制 Android 軟鍵盤。

頭文件

C++
#include <pl/Input.hpp>

事件

C++
struct TouchEvent {
  int action;
  int pointerId;
  float x;
  float y;
};

struct KeyEvent {
  int keyCode;
  unsigned int unicodeChar;
  bool isKeyDown;
};

struct MouseEvent {
  int button;
  bool isDown;
};

callback 返回 true 表示消費事件。

函數

C++
void registerTouchCallback(pl::input::TouchCallback callback);
void registerKeyCallback(pl::input::KeyCallback callback);
void registerMouseCallback(pl::input::MouseCallback callback);

void showKeyboard();
void hideKeyboard();

示例

C++
#include <pl/Input.hpp>

bool MyMod::enable() {
  pl::input::registerTouchCallback([](const pl::input::TouchEvent &event) {
    (void)event;
    return false;
  });

  pl::input::registerKeyCallback([](const pl::input::KeyEvent &event) {
    return event.isKeyDown && event.keyCode == 111;
  });

  return true;
}

注意

  • 當前沒有 unregister API;請每進程只註冊一次,或通過模組自有 enabled 狀態轉發。
  • callback 保持短小,不要阻塞。
  • 軟鍵盤函數需要遊戲 Activity 可用。