I have a website that hosts typing games (.js embedded into HTML). The game pages also have ads that refresh every minute. The refresh of the ads is causing keyboard function in many of my games to lose “focus” on the game (for example, someone typing can no longer type until they click on the game again).
Is there a way to keep the keyboard’s focus on the game?
I should note: The games are coded from HaXe and compiled into .js (HTML5)
You could try setting stage.focus on each frame, though that would prevent the user from using another text input box on the page, perhaps on your site that would be fine, though
Yeah, I do have a search field on the page. So that might inhibit the search function. I tried setting the div index to 0, but that didn’t work either. Just can’t find a solution on this one…
Perhaps Event.ENTER_FRAME for setting stage.focus, unless you receive Event.ACTIVATE or Event.DEACTIVATE, though I suppose that works only when you leave the page.
Perhaps you could add a listener on the ad, so when it receives focus, you send focus back to the game? Troublesome that it steals focus like this
var ad = document.getElementById ("my-advertiser");
var game = document.getElementById ("openfl-content");
ad.addEventListener ("focus", function (e) {
e.preventDefault ();
game.focus ();
});
…though it might be a little more nuanced, since there’s the hidden input text box that we use
A hacky solution would be to steal focus every frame, but only if the mouse cursor is inside the game’s boundaries. Not sure that would work on mobile, since the “cursor” (a.k.a. the last known location of a tap) can teleport around, and you might not receive an event if it teleported out.
Another option is to use JavaScript to get the id of the current focused element. If it isn’t an id you recognize, then assume an ad stole focus and steal it back. The tough part would be updating your list of allowed ids whenever you updated the site. Actually, probably a better option would be to make a class (such as “valid-focus-target”) for it, since you can mark an unlimited number of elements as the same class. Then just check if the currently-focused element has the class “valid-focus-target.”