Skip to content

Commit

Permalink
Workaround for #4
Browse files Browse the repository at this point in the history
- The workaround implemented, releases all keys when "Super" is released. Although not a perfect
  solution, it guarantees that the state is eventually consistent
  • Loading branch information
ypujante committed Jun 25, 2024
1 parent 7722ad4 commit f4ca7de
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5)

set(emscripten-glfw_RELEASE_YEAR "2024")
set(emscripten-glfw_RELEASE_MONTH "06" )
set(emscripten-glfw_RELEASE_DAY "17" )
set(emscripten-glfw_RELEASE_DAY "25" )

set(emscripten-glfw_GLFW_VERSION "3.4.0")

Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Introduction
This project is an emscripten port of GLFW written in C++ for the web/wasm platform. The currently supported
GLFW API is 3.4.

[![Latest - 3.4.0.20240617](https://img.shields.io/badge/Latest-3.4.0.20240617-blue)](https://github.com/pongasoft/emscripten-glfw/releases/latest)
[![Latest - 3.4.0.20240625](https://img.shields.io/badge/Latest-3.4.0.20240625-blue)](https://github.com/pongasoft/emscripten-glfw/releases/latest)
[![GLFW - 3.4.0](https://img.shields.io/badge/GLFW-3.4.0-blue)](https://www.glfw.org/)
[![emscripten - TBD](https://img.shields.io/badge/emscripten-TBD-blue)](https://emscripten.org)
![Compiles](https://github.com/pongasoft/emscripten-glfw/actions/workflows/main.yml/badge.svg)
Expand Down Expand Up @@ -154,6 +154,7 @@ emcc --use-port=contrib.glfw3:disableWarning=true:disableMultiWindow=true main.c
> #### Note about availability in emscripten
> | this port | emscripten |
> |----------------|------------|
> | 3.4.0.20240625 | TBD |
> | 3.4.0.20240617 | TBD |
> | 3.4.0.20240616 | TBD |
> | 3.4.0.20240601 | TBD |
Expand Down Expand Up @@ -223,10 +224,18 @@ LDFLAGS += -s USE_WEBGPU=1 --js-library $(EMS_GLFW3_DIR)/src/js/lib_emscripten_g
Release Notes
-------------
#### 3.4.0.20240625 - 2024-06-25 | emscripten TBD
- Implemented workaround for [#4](https://github.com/pongasoft/emscripten-glfw/issues/4): _Using Super + "Key" on macOS results in "Key" not being released_.
Due to the [broken state](https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser) of
javascript handling the `Super/Meta` key, there is no good solution. The workaround implemented, releases all keys when `Super` is released. Although not a perfect
solution, it guarantees that the state is _eventually_ consistent:
- if "Key" was released while "Super" was held, then when "Super" gets released, "Key" is released (later than when actually released, final state is consistent: "Key" in `Release` state)
- if "Key" is still held when "Super" is released, "Key" is released when "Super" gets released, but immediately gets a down event (Up/Down event, final state is consistent": "Key" in `Pressed` state)
#### 3.4.0.20240617 - 2024-06-17 | emscripten TBD
- Fixed [#3](https://github.com/pongasoft/emscripten-glfw/issues/3): glfwGetKey must return one of `GLFW_PRESS` or `GLFW_RELEASE`
- Fixed [#3](https://github.com/pongasoft/emscripten-glfw/issues/3): _glfwGetKey must return one of `GLFW_PRESS` or `GLFW_RELEASE`_
#### 3.4.0.20240616 - 2024-06-16 | emscripten TBD
Expand Down
4 changes: 2 additions & 2 deletions port/emscripten-glfw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import os
from typing import Dict

TAG = '3.4.0.20240617'
HASH = '148609b09e322e7f2433f5335431ba142ffe570248f296eb3a89d3d671476112657e30ebfe2ae23c09b99e7b0eddf875bfef149b43dbd50d5f58ccfc22770f89'
TAG = '3.4.0.20240625'
HASH = '318cb351628286fd2aa98bfbf76949f91114fabd2b13a6468109d97c138689f73ae05526cb083d4644746f661cc81f17270680b4636457943e3cb646eafb49bd'
ZIP_URL = f'https://github.com/pongasoft/emscripten-glfw/releases/download/v{TAG}/emscripten-glfw3-{TAG}.zip'

# contrib port information (required)
Expand Down
33 changes: 33 additions & 0 deletions src/cpp/emscripten/glfw3/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ bool Keyboard::onKeyUp(GLFWwindow *iWindow, EmscriptenKeyboardEvent const *iKeyb

const glfw_key_state_t state = GLFW_RELEASE;

bool wasSuperPressed = isSuperPressed();

if(fKeyStates[key] != GLFW_RELEASE && fKeyStates[key] != kStickyPress)
{
fKeyStates[key] = fStickyKeys ? kStickyPress : state;
Expand All @@ -126,6 +128,9 @@ bool Keyboard::onKeyUp(GLFWwindow *iWindow, EmscriptenKeyboardEvent const *iKeyb
fKeyCallback(iWindow, key, scancode, state, computeCallbackModifierBits(iKeyboardEvent));
}

if(wasSuperPressed && !isSuperPressed())
resetKeysOnSuperRelease(iWindow);

return true;
}

Expand Down Expand Up @@ -164,4 +169,32 @@ void Keyboard::setStickyKeys(bool iStickyKeys)
}
}

//------------------------------------------------------------------------
// Keyboard::resetKeysOnSuperRelease
//------------------------------------------------------------------------
void Keyboard::resetKeysOnSuperRelease(GLFWwindow *iWindow)
{
auto modifiedBits = computeCallbackModifierBits();

for(auto key = 0; key < fKeyStates.size(); key++)
{
if(key == GLFW_KEY_LEFT_SHIFT || key == GLFW_KEY_RIGHT_SHIFT ||
key == GLFW_KEY_LEFT_CONTROL || key == GLFW_KEY_RIGHT_CONTROL ||
key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_RIGHT_ALT ||
key == GLFW_KEY_LEFT_SUPER || key == GLFW_KEY_RIGHT_SUPER)
{
// the special keys properly receive key up events...
continue;
}

if(fKeyStates[key] != GLFW_RELEASE && fKeyStates[key] != kStickyPress)
{
fKeyStates[key] = GLFW_RELEASE;

if(fKeyCallback)
fKeyCallback(iWindow, key, getKeyScancode(key), GLFW_RELEASE, modifiedBits);
}
}
}

}
1 change: 1 addition & 0 deletions src/cpp/emscripten/glfw3/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Keyboard
bool onKeyDown(GLFWwindow *iWindow, const EmscriptenKeyboardEvent *iKeyboardEvent);
bool onKeyUp(GLFWwindow *iWindow, const EmscriptenKeyboardEvent *iKeyboardEvent);
void resetAllKeys(GLFWwindow *iWindow);
void resetKeysOnSuperRelease(GLFWwindow *iWindow);

void setStickyKeys(bool iStickyKeys);
bool getStickyKeys() const { return fStickyKeys; };
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/emscripten/glfw3/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace emscripten::glfw3 {

#define D_EMSCRIPTEN_GLFW_VERSION_STR "3.4.0.20240617"
#define D_EMSCRIPTEN_GLFW_VERSION_STR "3.4.0.20240625"

}

Expand Down

0 comments on commit f4ca7de

Please sign in to comment.