Skip to content

Commit ef81fe1

Browse files
authored
Merge pull request #67 from scratchcpp/handle_any_key
Handle any key correctly
2 parents 392b9d2 + 02767f7 commit ef81fe1

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

src/projectscene.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,42 @@ void ProjectScene::handleMouseRelease()
7373

7474
void ProjectScene::handleKeyPress(Qt::Key key, const QString &text)
7575
{
76-
if (m_engine) {
77-
auto it = SPECIAL_KEY_MAP.find(key);
76+
m_pressedKeys.insert(key);
7877

79-
if (it == SPECIAL_KEY_MAP.cend())
80-
m_engine->setKeyState(text.toStdString(), true);
81-
else {
82-
KeyEvent event(it->second);
83-
m_engine->setKeyState(event.name(), true);
78+
if (m_engine) {
79+
if (!text.isEmpty()) {
80+
auto it = SPECIAL_KEY_MAP.find(key);
81+
82+
if (it == SPECIAL_KEY_MAP.cend())
83+
m_engine->setKeyState(text.toStdString(), true);
84+
else {
85+
KeyEvent event(it->second);
86+
m_engine->setKeyState(event.name(), true);
87+
}
8488
}
89+
90+
m_engine->setAnyKeyPressed(!m_pressedKeys.empty());
8591
}
8692
}
8793

8894
void ProjectScene::handleKeyRelease(Qt::Key key, const QString &text)
8995
{
90-
if (m_engine) {
91-
auto it = SPECIAL_KEY_MAP.find(key);
96+
m_pressedKeys.erase(key);
9297

93-
if (it == SPECIAL_KEY_MAP.cend())
94-
m_engine->setKeyState(text.toStdString(), false);
95-
else {
96-
KeyEvent event(it->second);
97-
m_engine->setKeyState(event.name(), false);
98+
if (m_engine) {
99+
if (!text.isEmpty()) {
100+
auto it = SPECIAL_KEY_MAP.find(key);
101+
102+
if (it == SPECIAL_KEY_MAP.cend())
103+
m_engine->setKeyState(text.toStdString(), false);
104+
else {
105+
KeyEvent event(it->second);
106+
m_engine->setKeyState(event.name(), false);
107+
}
98108
}
109+
110+
if (m_pressedKeys.empty()) // avoid setting 'true' when a key is released
111+
m_engine->setAnyKeyPressed(false);
99112
}
100113
}
101114

src/projectscene.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <QQuickPaintedItem>
6+
#include <unordered_set>
67

78
namespace libscratchcpp
89
{
@@ -47,6 +48,7 @@ class ProjectScene : public QQuickItem
4748
libscratchcpp::IEngine *m_engine = nullptr;
4849
double m_stageScale = 1;
4950
KeyEventHandler *m_keyHandler = nullptr;
51+
std::unordered_set<Qt::Key> m_pressedKeys;
5052
};
5153

5254
} // namespace scratchcpprender

test/projectscene/projectscene_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,41 @@ TEST(ProjectSceneTest, HandleKeyPressAndRelease)
8181
for (const auto &[qtKey, scratchKey] : SPECIAL_KEY_MAP) {
8282
KeyEvent event(scratchKey);
8383
EXPECT_CALL(engine, setKeyState(event.name(), true));
84+
EXPECT_CALL(engine, setAnyKeyPressed(true));
8485
scene.handleKeyPress(qtKey, "test");
8586

8687
EXPECT_CALL(engine, setKeyState(event.name(), false));
88+
EXPECT_CALL(engine, setAnyKeyPressed(false));
8789
scene.handleKeyRelease(qtKey, "test");
8890
}
8991

9092
EXPECT_CALL(engine, setKeyState("a", true));
93+
EXPECT_CALL(engine, setAnyKeyPressed(true));
9194
scene.handleKeyPress(Qt::Key_A, "a");
9295

9396
EXPECT_CALL(engine, setKeyState("a", false));
97+
EXPECT_CALL(engine, setAnyKeyPressed(false));
9498
scene.handleKeyRelease(Qt::Key_A, "a");
9599

96100
EXPECT_CALL(engine, setKeyState("0", true));
101+
EXPECT_CALL(engine, setAnyKeyPressed(true));
97102
scene.handleKeyPress(Qt::Key_0, "0");
98103

99104
EXPECT_CALL(engine, setKeyState("0", false));
105+
EXPECT_CALL(engine, setAnyKeyPressed(false));
100106
scene.handleKeyRelease(Qt::Key_0, "0");
107+
108+
EXPECT_CALL(engine, setAnyKeyPressed(true));
109+
scene.handleKeyPress(Qt::Key_Control, "");
110+
111+
EXPECT_CALL(engine, setKeyState("a", true));
112+
EXPECT_CALL(engine, setAnyKeyPressed(true));
113+
scene.handleKeyPress(Qt::Key_A, "a");
114+
115+
EXPECT_CALL(engine, setKeyState("a", false));
116+
EXPECT_CALL(engine, setAnyKeyPressed).Times(0);
117+
scene.handleKeyRelease(Qt::Key_A, "a");
118+
119+
EXPECT_CALL(engine, setAnyKeyPressed(false));
120+
scene.handleKeyRelease(Qt::Key_Control, "");
101121
}

0 commit comments

Comments
 (0)