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

Proposal: Add KeyboardScope option #1512

Closed
kamranayub opened this issue Apr 28, 2020 · 2 comments · Fixed by #2660
Closed

Proposal: Add KeyboardScope option #1512

kamranayub opened this issue Apr 28, 2020 · 2 comments · Fixed by #2660
Labels
enhancement Label applied to enhancements or improvements to existing features proposal Applied to issues that are a proposal for an implementation

Comments

@kamranayub
Copy link
Member

Context

There are some issues using Excalibur in cross-origin iframes, see #1294 and #1510

What happens is that event listeners cannot listen on the top frame. In order to provide a fallback, we specific window as the target to listen to key events but that doesn't work either (according to our code, it never did).

There are some approaches we can try that will enable keyboard events when embedding cross-origin, which is by targeting the canvas or an element surrounding the canvas.

Proposal

Option 1: Try to use tabindex and focus

According to this SO answer it should be possible to attach listeners if the canvas has tabindex="1" and is in focus. I couldn't get this to work, though, but I didn't have time to explore it much.

Option 2: Use a wrapping element

The other way is by targeting an element that wraps the canvas. A <div> or a <span> may work (I like the inline element since it should wrap to the bounds of the canvas better).

This does work and I got it to work by adding a new option to allow passing in the element ID or element to target keyboard events on. In Storybook, this is document.getElementById('root').

However, I thought it may be better to implement the same idea we have for PointerScope and do a KeyboardScope and automatically generate the wrapping capture element

This presents some issues though, when the canvas is detached from the DOM. It can't be re-parented to be under the capture element.

Ideally, this keyboard initialization logic would run when the game boots up in the DOM for the first time and I got close but not quite in my spike of doing this.

export enum KeyboardScope {
  Document,
  Canvas
}
@kamranayub kamranayub added the enhancement Label applied to enhancements or improvements to existing features label Apr 28, 2020
@kamranayub kamranayub added the proposal Applied to issues that are a proposal for an implementation label Apr 30, 2020
@github-actions
Copy link

This issue hasn't had any recent activity lately and is being marked as stale automatically.

@github-actions github-actions bot added the stale This issue or PR has not had any activity recently label Dec 27, 2020
@jedeen jedeen added this to the vNext milestone Oct 9, 2021
@eonarheim eonarheim removed this from the vNext milestone May 27, 2022
@github-actions github-actions bot removed the stale This issue or PR has not had any activity recently label Jan 22, 2023
@eonarheim
Copy link
Member

Workaround for itch.io games and codesandbox.io

Add this snippet

engine.start().then(() => {
  window.focus();
});

eonarheim added a commit that referenced this issue Jun 14, 2023
This PR adds a new engine option (which is on by default) to grab the window focus when hosted in an iframe. This allows keyboard events to work by default.

Confirmed on itch.io https://eonarheim.itch.io/iframe-test

Closes #1512
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Label applied to enhancements or improvements to existing features proposal Applied to issues that are a proposal for an implementation
Projects
None yet
3 participants