Skip to content

Commit

Permalink
fixes Key + Super use-case
Browse files Browse the repository at this point in the history
- although a much rarer use-case, pressing a key then Super was not handled properly
  • Loading branch information
ypujante committed Sep 7, 2024
1 parent 9a312de commit 37b09a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
48 changes: 39 additions & 9 deletions src/cpp/emscripten/glfw3/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,11 @@ bool Keyboard::onKeyDown(GLFWwindow *iWindow, Event const &iEvent, emscripten::g
auto scancode = getKeyScancode(iEvent.code);
glfw_key_t key = getGLFWKey(scancode);

// handling Super + Key
if(iEvent.isSuperPressed() && !isSpecialKey(key))
// handling Super
if(iEvent.isSuperPressed())
{
// the issue is that the Up event is NEVER received... so we work around in multiple ways
if(fSuperPlusKeys.find(key) == fSuperPlusKeys.end() && iEvent.repeat)
{
// case when we have issued a key up event due to timeout... so we are ignoring
if(handleSuperKeyPressed(key, iEvent.repeat))
return true;
}
// we store the current time (processed in handleSuperPlusKeys)
fSuperPlusKeys[key] = {static_cast<int>(emscripten_glfw3_context_get_now()), iEvent.repeat};
}

auto handled = false;
Expand Down Expand Up @@ -204,8 +198,43 @@ void Keyboard::setStickyKeys(bool iStickyKeys)
}
}

//------------------------------------------------------------------------
// Keyboard::handleSuperKeyPressed
// Called when the super key is involved in a key down event (`e.metaKey` is `true`)
//------------------------------------------------------------------------
bool Keyboard::handleSuperKeyPressed(glfw_key_t iKey, bool iRepeat)
{
// case Super + Key (Special keys behave differently, so we ignore them)
if(!isSpecialKey(iKey))
{
// the issue is that the Up event is NEVER received... so we work around in multiple ways
if(fSuperPlusKeys.find(iKey) == fSuperPlusKeys.end() && iRepeat)
{
// case when we have issued a key up event due to timeout... so we are ignoring
return true;
}
// we store the current time (processed in handleSuperPlusKeys)
fSuperPlusKeys[iKey] = {static_cast<int>(emscripten_glfw3_context_get_now()), iRepeat};
}

// case when it is the super key itself: some keys might already be down
if(isSuperKey(iKey) && !iRepeat)
{
for(int k = 0; k < fKeyStates.size(); k++)
{
if(!isSpecialKey(k) && fKeyStates[k] != GLFW_RELEASE)
{
fSuperPlusKeys[k] = {static_cast<int>(emscripten_glfw3_context_get_now()), false};
}
}
}

return false;
}

//------------------------------------------------------------------------
// Keyboard::resetKeysOnSuperRelease
// Called when the Super key has been released to reset all keys that were down
//------------------------------------------------------------------------
void Keyboard::resetKeysOnSuperRelease(GLFWwindow *iWindow)
{
Expand All @@ -221,6 +250,7 @@ void Keyboard::resetKeysOnSuperRelease(GLFWwindow *iWindow)

//------------------------------------------------------------------------
// Keyboard::handleSuperPlusKeys
// Called every frame when fSuperPlusKeys is not empty to purge keys that have timed out
//------------------------------------------------------------------------
void Keyboard::handleSuperPlusKeys(GLFWwindow *iWindow, SuperPlusKeyTimeout const &iTimeout)
{
Expand Down
9 changes: 7 additions & 2 deletions src/cpp/emscripten/glfw3/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class Keyboard
bool onKeyDown(GLFWwindow *iWindow, Event const &iEvent, emscripten::glfw3::browser_key_fun_t const &iBrowserKeyCallback);
bool onKeyUp(GLFWwindow *iWindow, Event const &iEvent, emscripten::glfw3::browser_key_fun_t const &iBrowserKeyCallback);
void resetAllKeys(GLFWwindow *iWindow);
void resetKey(GLFWwindow *iWindow, glfw_key_t iKey, int modifierBits);
void resetKeysOnSuperRelease(GLFWwindow *iWindow);
inline bool hasSuperPlusKeys() const { return !fSuperPlusKeys.empty(); }
void handleSuperPlusKeys(GLFWwindow *iWindow, SuperPlusKeyTimeout const &iTimeout);

Expand All @@ -93,6 +91,13 @@ class Keyboard
iKey == GLFW_KEY_LEFT_ALT || iKey == GLFW_KEY_RIGHT_ALT ||
iKey == GLFW_KEY_LEFT_SUPER || iKey == GLFW_KEY_RIGHT_SUPER;
}
static constexpr bool isSuperKey(glfw_key_t iKey) {
return iKey == GLFW_KEY_LEFT_SUPER || iKey == GLFW_KEY_RIGHT_SUPER;
}

void resetKey(GLFWwindow *iWindow, glfw_key_t iKey, int modifierBits);
void resetKeysOnSuperRelease(GLFWwindow *iWindow);
bool handleSuperKeyPressed(glfw_key_t iKey, bool iRepeat);

private:
struct SuperPlusKeyTiming
Expand Down

0 comments on commit 37b09a4

Please sign in to comment.