Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release pressed events when the window is blurred on HTML5 platform #52809

Merged
merged 1 commit into from
Sep 29, 2021

Conversation

selgesel
Copy link
Contributor

Fixes #49021

On the JS/HTML5 platform, when the user switches to another tab/window, any keys that were held down at the time would stay held down, so when the user switched back to the game window they would have to press the same keys again to release them.

This was due to the fact that we weren't calling the release_pressed_events() of the Input singleton like we do on other platforms. Browsers fire a blur event on the window object when it loses focus, and it's possible to add a callback handler to the same event in Emscripten.

So in this patch we simply add a callback handler to this event and invoke release_pressed_events(). In contrast to the focusout event which occurs before the focus is lost (and doesn't fire from the window object anyway), the blur event can bubble, so we just return false from the callback.

Also, we already propagate the focus and blur events and turn them into window notifications via the godot_js_display_notification_cb() JavaScript function, so we don't have to do it in the new callback.

I don't think we can apply the same patch to the 3.x branch with a cherry-pick because both the location of the patch and the way of accessing the input singleton are a little bit different. Once we're good to go on this I can open another PR for that branch.

See:

Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. CC @Faless for review/merge.

Copy link
Collaborator

@Faless Faless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good 👍 🥇 !

Small side note, unlike what mentioned in the OP (probably a typo) the blur event does not bubble and is not cancellable (see spec).
Returning false is correct, because it tells emscripten not to preventDefault (which would not be allowed).

@Faless Faless merged commit e2e1a50 into godotengine:master Sep 29, 2021
@Faless
Copy link
Collaborator

Faless commented Sep 29, 2021

Thanks!

@selgesel
Copy link
Contributor Author

This looks really good 👍 🥇 !

Small side note, unlike what mentioned in the OP (probably a typo) the blur event does not bubble and is not cancellable (see spec). Returning false is correct, because it tells emscripten not to preventDefault (which would not be allowed).

Whoops, that's right, it doesn't even make sense for it to bubble in the first place 😅
Thanks for the merge!

@selgesel selgesel deleted the patch1 branch September 29, 2021 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Input.is_key_pressed() returns true for control and tab after losing focus on HTML5
3 participants