Iframe focus

0 favourites
  • 3 posts
From the Asset Store
A game template with a great amount of assets! You will be able to create a full game just with the assets provided!
  • I'm trying to create an iframe while the game is running, and loading a .html file that's included in my Files folder in the project manager.

    It works great, but I couldn't get it to focus properly. I tried adding

    <script type="text/javascript">
    window.focus();
    </script>

    In the .html file, and it kinda does the trick, but only if I press any key on my keyboard after the creating of the iframe. Also, it somewhat works with gamepads, but for some reason the sound won't play, as if the iframe isn't properly focused.

    Here's the code I currently have, trying to run a GB emulator inside an iframe:

    file.io/guOlZD5OxvWA

    Any ideas are appreciated!!

    Tagged:

  • To prevent abuse, both windows and iframes cannot normally assign focus to themselves automatically. The user must click or touch inside an iframe to intentionally give it focus. You can encourage that with a "click here to start" type button. Unfortunately as gamepads aren't able to specifically direct input to a window, I don't think you can use a gamepad to transfer focus.

    One exception may be that the parent frame may be able to transfer its own focus to the iframe with something like iframeElem.contentWindow.focus(). That might require a user input too, but I'm not sure, I can't remember the browser rules around that off the top of my head.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • That's too bad, this makes iframes pratically unusable for gamepad supported games :(

    I tried iframeElem.contentWindow.focus(); too but it didn't work properly. The only way I could figure out to make it work was to add window.focus(); in the <script> parts of the html page. This makes the window focused enough for gamepad and keyboard to work on it, but for some reason if you're only using gamepads, there's no sound.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)