feat(hello_module): 添加ESC键退出功能

在HelloScene中添加键盘事件监听,当按下ESC键时退出应用。同时简化了头文件引用,使用extra2d.h代替多个单独的头文件。
This commit is contained in:
ChestnutYueyue 2026-02-15 20:24:32 +08:00
parent d06c8735bd
commit e0adaa3263
1 changed files with 10 additions and 4 deletions

View File

@ -1,8 +1,5 @@
#include "hello_module.h" #include "hello_module.h"
#include <extra2d/app/application.h> #include <extra2d/extra2d.h>
#include <extra2d/scene/scene.h>
#include <extra2d/services/scene_service.h>
#include <extra2d/utils/logger.h>
using namespace extra2d; using namespace extra2d;
@ -17,6 +14,15 @@ public:
void onEnter() override { void onEnter() override {
Scene::onEnter(); Scene::onEnter();
addListener(EventType::KeyPressed, [](Event &e) {
auto &keyEvent = std::get<KeyEvent>(e.data);
if (keyEvent.keyCode == static_cast<int>(Key::Escape)) {
e.handled = true;
E2D_LOG_INFO("ESC !!!exit");
Application::get().quit();
}
});
E2D_LOG_INFO("HelloScene entered"); E2D_LOG_INFO("HelloScene entered");
setBackgroundColor(Color(0.1f, 0.1f, 0.2f, 1.0f)); setBackgroundColor(Color(0.1f, 0.1f, 0.2f, 1.0f));