htmlgwtiframeplayn

PlayN application in IFrame doesn't have keyboard input


I have made my first PlayN app and it works just great until it is embedded somewhere with iframe.

In my init() function I have this code for keyboard:

Keyboard k = PlayN.keyboard();

k.setListener(new Listener() {

    @Override
    public void onKeyUp(Event event) {
}

    @Override
    public void onKeyDown(Event event) {
        /* long long code was here */
    }

   @Override
   public void onKeyTyped(TypedEvent event) {
   }
});

This works well, but not with iframe. It simply doesn't have the focus or something.

I found a workaround for this: quickly press F5 and quickly ckick the iframe few times before it is loaded. But I want something to do it automatically.

Examples: with iframe (this have input problems), no iframe (this works well).


Solution

  • I've had this issue as well. I believe the fix that worked for me was adding the following to the main .html file:

    <script src="mygamesource.nocache.js"></script> 
    <!-- right below your nocache script -->   
    <script>  
       function handleMouseDown(evt) {  
         window.focus();   
         evt.preventDefault();  
         evt.stopPropagation();  
         evt.target.style.cursor = 'default';  
       }  
       document.getElementById('body').addEventListener('mousedown', handleMouseDown, false);  
    </script>
    

    Credit to this post if it works for you.