How do I detect if the game is focused or not?

  • I don't want controller inputs affecting the game if it's not the currently focused browser window or whatever

  • Browser object condition

    Page is visible

    True if the page the HTML5 game is running on is currently visible. The page counts as hidden if the window is minimised, the page is in a different tab to the one being viewed, or on a mobile device when the app goes in to the background. When the page is hidden the game automatically pauses.

  • where

  • Looks like in C3 runtime these events were moved from Browser to System object. Try "System On suspended".

  • Suspended is true when another tab in the same window takes over but not a different window is opened, both windows will take controller inputs !

  • Maybe you should report it as a bug.

  • I did they said it's how browsers take controller inputs not a construct bug

  • Ok, but shouldn't "On suspended" event get triggered when browser window loses focus?

  • no it's not the same thing I think

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Perhaps there is a difference between "unfocused" and "suspended"?

    Can a window be unfocused and be running?

    Could it be

    focused: active/inactive

    suspended: working/not working?

    Manual:

    Is suspended

    True if the runtime is currently suspended, i.e. in between On suspended and On resumed. This normally means the browser/app is in the background and is not currently running.

  • Hello yoyoEleanor, this is easily possible using JavaScript...

    Here is a nice example- LINK>>

    First, on the page that runs your game, right click and save the page.

    Then open the HTML file and search for the word "iframe". If you keep scrolling you will find the id of this iframe. For Itch I think its usually 'game_drop'. Now you've got your iframe id-it's important to know which iframe you want to work on.

    In the console, there is an input textfield. For testing on one of your published projects, paste this code over there in the input field and press the enter key-

    setTimeout(
     () => {
    var GameFrame= document.getElementById('game_drop');
    if (document.hasFocus())
     console.log("Focused");
    else
     console.log("Not Focused")
     },
     4 * 1000
    );
    

    This will log the corresponding text to the console after 4 seconds- and within which you may click on your game iframe so that "Focused" is logged into the console..

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