If I load the webplayer on a page in Safari or IE, the keyboard input does not work until I click the webplayer. In Firefox the webplayer automatically receives focus.
I thought this was no big deal because I can use the Javascript focus() function upon load. However, this does not seem to work.
I tried several options like:
- GetUnity().focus();
- window.document.UnityObject.focus();
- document.getElementById(“UnityObject”).focus();
- window.document.UnityEmbed.focus();
- document.getElementById(“UnityEmbed”).focus();
I tried this in body onload"…"
and I tried it delayed with a setTimeout(…)
Any ideas?
After some more testing it appears that only on Safari the webplayer does not receive keyboard input automatically. I am still unable to give the focus other than by letting the user click the webplayer.
function tryToFocus() {
var obj = null;
if(focusattempt==0) {
obj=document.getElementById("UnityObject");
} else if(focusattempt==1) {
obj=document.getElementById("UnityEmbed");
} else { // tried it all
return;
}
if(obj) obj.focus();
document.getElementById("debug").innerHTML="trying "+focusattempt+":"+obj+"
focus on:"+document.activeElement;
focusattempt++;
setTimeout('tryToFocus()',1000);
}
The result is:
trying 0:[object HTMLObjectElement]
focus on:[object HTMLBodyElement]
trying 1:[object HTMLEmbedElement]
focus on:[object HTMLBodyElement]
And making the user click the webplayer is a bit weird in my case since the entire game has keyboard control. In fact, the first thing you see is an animated model of the 4 cursor keys of your keyboard, inviting the user to press one of them. If they do this on Safari nothing happens until they click the webplayer first.
Ok found it. Safari doesnt give focus because it thinks the object simply isn’t editable.
This did the trick for me.
function TryToFocus() {
var obj = GetUnity();
if(obj) {
obj.contentEditable=true;
obj.focus();
}
}