Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/projectscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,42 @@ void ProjectScene::handleMouseRelease()

void ProjectScene::handleKeyPress(Qt::Key key, const QString &text)
{
if (m_engine) {
auto it = SPECIAL_KEY_MAP.find(key);
m_pressedKeys.insert(key);

if (it == SPECIAL_KEY_MAP.cend())
m_engine->setKeyState(text.toStdString(), true);
else {
KeyEvent event(it->second);
m_engine->setKeyState(event.name(), true);
if (m_engine) {
if (!text.isEmpty()) {
auto it = SPECIAL_KEY_MAP.find(key);

if (it == SPECIAL_KEY_MAP.cend())
m_engine->setKeyState(text.toStdString(), true);
else {
KeyEvent event(it->second);
m_engine->setKeyState(event.name(), true);
}
}

m_engine->setAnyKeyPressed(!m_pressedKeys.empty());
}
}

void ProjectScene::handleKeyRelease(Qt::Key key, const QString &text)
{
if (m_engine) {
auto it = SPECIAL_KEY_MAP.find(key);
m_pressedKeys.erase(key);

if (it == SPECIAL_KEY_MAP.cend())
m_engine->setKeyState(text.toStdString(), false);
else {
KeyEvent event(it->second);
m_engine->setKeyState(event.name(), false);
if (m_engine) {
if (!text.isEmpty()) {
auto it = SPECIAL_KEY_MAP.find(key);

if (it == SPECIAL_KEY_MAP.cend())
m_engine->setKeyState(text.toStdString(), false);
else {
KeyEvent event(it->second);
m_engine->setKeyState(event.name(), false);
}
}

if (m_pressedKeys.empty()) // avoid setting 'true' when a key is released
m_engine->setAnyKeyPressed(false);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/projectscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include <QQuickPaintedItem>
#include <unordered_set>

namespace libscratchcpp
{
Expand Down Expand Up @@ -47,6 +48,7 @@ class ProjectScene : public QQuickItem
libscratchcpp::IEngine *m_engine = nullptr;
double m_stageScale = 1;
KeyEventHandler *m_keyHandler = nullptr;
std::unordered_set<Qt::Key> m_pressedKeys;
};

} // namespace scratchcpprender
20 changes: 20 additions & 0 deletions test/projectscene/projectscene_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,41 @@ TEST(ProjectSceneTest, HandleKeyPressAndRelease)
for (const auto &[qtKey, scratchKey] : SPECIAL_KEY_MAP) {
KeyEvent event(scratchKey);
EXPECT_CALL(engine, setKeyState(event.name(), true));
EXPECT_CALL(engine, setAnyKeyPressed(true));
scene.handleKeyPress(qtKey, "test");

EXPECT_CALL(engine, setKeyState(event.name(), false));
EXPECT_CALL(engine, setAnyKeyPressed(false));
scene.handleKeyRelease(qtKey, "test");
}

EXPECT_CALL(engine, setKeyState("a", true));
EXPECT_CALL(engine, setAnyKeyPressed(true));
scene.handleKeyPress(Qt::Key_A, "a");

EXPECT_CALL(engine, setKeyState("a", false));
EXPECT_CALL(engine, setAnyKeyPressed(false));
scene.handleKeyRelease(Qt::Key_A, "a");

EXPECT_CALL(engine, setKeyState("0", true));
EXPECT_CALL(engine, setAnyKeyPressed(true));
scene.handleKeyPress(Qt::Key_0, "0");

EXPECT_CALL(engine, setKeyState("0", false));
EXPECT_CALL(engine, setAnyKeyPressed(false));
scene.handleKeyRelease(Qt::Key_0, "0");

EXPECT_CALL(engine, setAnyKeyPressed(true));
scene.handleKeyPress(Qt::Key_Control, "");

EXPECT_CALL(engine, setKeyState("a", true));
EXPECT_CALL(engine, setAnyKeyPressed(true));
scene.handleKeyPress(Qt::Key_A, "a");

EXPECT_CALL(engine, setKeyState("a", false));
EXPECT_CALL(engine, setAnyKeyPressed).Times(0);
scene.handleKeyRelease(Qt::Key_A, "a");

EXPECT_CALL(engine, setAnyKeyPressed(false));
scene.handleKeyRelease(Qt::Key_Control, "");
}