Metatron Posted November 15, 2015 Share Posted November 15, 2015 Hello I need to set events only for pixi.js canvas. how can i do it?document.addEventListener('keydown', function(event) { });This method start leasten all window and block html inputs. Thank you Quote Link to comment Share on other sites More sharing options...
xerver Posted November 15, 2015 Share Posted November 15, 2015 Add the event to the canvas element instead of the document. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted November 15, 2015 Share Posted November 15, 2015 You cant focus on canvas if there's no tabindex on it , am I right? <canvas tabIndex=1> </canvas> renderer.view.setAttribute("tabIndex", 1); also its good to check if canvas is focused, and if not then draw "CLICK TO FOCUS" label in the middle of it, like https://ga.me does it. Quote Link to comment Share on other sites More sharing options...
Metatron Posted November 15, 2015 Author Share Posted November 15, 2015 Add the event to the canvas element instead of the document.$("#world").html(this.renderer.view)$("#world").bind( "keydown", function( event ) { event.preventDefault(); console.log(event);});$('#world').focus();But, its not help, no keyboard event, when i press Quote Link to comment Share on other sites More sharing options...
Metatron Posted November 16, 2015 Author Share Posted November 16, 2015 I solved the problem using this method this.renderer.view.setAttribute("id", "worldCanvas"); $("#world").html(this.renderer.view); var lastDownTarget, canvas; canvas = document.getElementById('worldCanvas'); lastDownTarget = canvas; document.addEventListener('mousedown', function(event) { lastDownTarget = event.target; }, false); document.addEventListener('keydown', function(event) { if(lastDownTarget == canvas) { event.preventDefault(); event.stopPropagation(); wi.doKeyDown(event); } }, false); Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted November 16, 2015 Share Posted November 16, 2015 You forgot to add "event.preventDefault(); event.stopPropagation();" in that if. Quote Link to comment Share on other sites More sharing options...
Metatron Posted November 16, 2015 Author Share Posted November 16, 2015 You forgot to add "event.preventDefault(); event.stopPropagation();" in that if.True, Thank you Quote Link to comment Share on other sites More sharing options...
mattstyles Posted November 16, 2015 Share Posted November 16, 2015 Holding a variable just to check focus sounds like a terrible idea, the `tabindex` method would surely be more intuitive (although still a hack, but, whats a hack between friends anyway?)? I've thrown it into a plunk and it works fine, I havent checked browser compatibility but I'd imagine it works everywhere (I'd say that mobile could cause a problem, but you're not using keyboard interface there anyways, actually, given new tablets this could be viable, particularly in the near future). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.