Input(輸入系統 · 客戶端)
ll/api/input/、ll/api/event/input/(src-client/)· 僅客戶端
概述
Input 模塊為客戶端提供鍵盤和鼠標輸入綁定。允許模組註冊自定義按鍵綁定及回調,並處理輸入事件。
頭文件
| 頭文件 | 說明 |
|---|---|
ll/api/input/KeyRegistry.h | 按鍵綁定註冊表單例 |
ll/api/input/KeyHandle.h | 單個按鍵綁定句柄 |
ll/api/event/input/KeyInputEvent.h | 鍵盤輸入事件 |
ll/api/event/input/MouseInputEvent.h | 鼠標輸入事件 |
核心類
KeyRegistry
C++
namespace ll::input {
class KeyRegistry {
public:
using ButtonDownHandler = KeyHandle::ButtonDownHandler;
using ButtonUpHandler = KeyHandle::ButtonUpHandler;
static KeyRegistry& getInstance();
KeyHandle& getOrCreateKey(
std::string_view name,
std::vector<int> const& defaultKeyCodes,
bool allowRemap = true,
std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
);
bool hasKey(std::string_view name);
std::vector<std::string> getRegisteredKeys() const;
};
}KeyHandle
C++
namespace ll::input {
class KeyHandle {
public:
using ButtonDownHandler = std::function<void(::FocusImpact, ::IClientInstance&)>;
using ButtonUpHandler = std::function<void(::FocusImpact, ::IClientInstance&)>;
std::string const& getName() const;
std::shared_ptr<mod::Mod> getMod() const;
std::string getFullName() const;
std::vector<int> const& getKeyCodes() const;
bool isValid() const;
bool isAllowRemap() const;
void setAllowRemap(bool allowRemap);
void registerButtonDownHandler(ButtonDownHandler handler, bool suspendable = false);
void registerButtonUpHandler(ButtonUpHandler handler, bool suspendable = false);
void setInputMappingStack(std::vector<std::string> stacks);
bool containsInputMappingStack(std::string_view stack) const;
};
}輸入事件
KeyInputEvent
C++
namespace ll::event::inline input {
class KeyInputEvent final : public Cancellable<InputEvent> {
public:
enum class Action : int { Up = 0, Down = 1 };
HIDController& controller() const;
int& keyCode() const;
Action action() const;
bool isDown() const;
};
}MouseInputEvent
C++
namespace ll::event::inline input {
class MouseInputEvent final : public Cancellable<InputEvent> {
public:
char& actionButtonId() const;
schar& buttonData() const;
short& x() const;
short& y() const;
short& dx() const;
short& dy() const;
};
}使用方法
註冊按鍵綁定
C++
#include "ll/api/input/KeyRegistry.h"
void registerKeys() {
auto& registry = ll::input::KeyRegistry::getInstance();
// 默认绑定 K 键;所属模组默认为当前模组
auto& myKey = registry.getOrCreateKey("toggle", {'K'});
myKey.registerButtonDownHandler([](FocusImpact, IClientInstance&) {
// 按键按下时调用
});
myKey.registerButtonUpHandler([](FocusImpact, IClientInstance&) {
// 按键释放时调用
});
}監聽輸入事件
C++
#include "ll/api/event/EventBus.h"
#include "ll/api/event/input/KeyInputEvent.h"
void listenToInput() {
auto& bus = ll::event::EventBus::getInstance();
bus.emplaceListener<ll::event::KeyInputEvent>(
[](ll::event::KeyInputEvent& event) {
if (event.keyCode() == 'E' && event.isDown()) {
// E 键按下
event.cancel(); // 阻止默认行为
}
}
);
}鼠標輸入
C++
#include "ll/api/event/EventBus.h"
#include "ll/api/event/input/MouseInputEvent.h"
void listenToMouse() {
auto& bus = ll::event::EventBus::getInstance();
bus.emplaceListener<ll::event::MouseInputEvent>(
[](ll::event::MouseInputEvent& event) {
auto x = event.x();
auto y = event.y();
// 处理鼠标输入
}
);
}平臺說明
- 此模塊僅限客戶端,在服務端構建中不可用
- 按鍵代碼遵循 Windows 虛擬鍵代碼
- 輸入事件可以被取消以阻止默認行為
相關模塊
- Event(事件系統) — 輸入事件是事件系統的一部分