Input not recognised until window resize triggered or change focus


after the update to 4.2 overnight my scene does not respond to keyboard input (to move camera etc) until after I trigger a window resize. Is there a workaround I can include to get around this or something I can do?

(huge congrats on 4.2 BTW)

1 Like

my previous post was written quickly between meetings. I have tried again and it it not a just a resize that enables input but changing focus to another window and back again will also do it.
I tried adding a engine.resize(); before engine.runRenderLoop to force a resize but that did not work…come to think of it I should try that after starting the renderloop.

In addition, moving the camera via mouse drag works but not keyboard input.

I saw some similar threads talking about keyboard events not getting added if the canvas already had focus. I have a workaround but it is not pretty and would be good to sort out why this is happening now (since launch of 4.2 but not before) and to know if there is any problem this workaround may introduce.

My workaround is to add this after calling runRenderLoop

canvas.addEventListener("keydown", scene._inputManager._onKeyDown, false);
canvas.addEventListener("keyup", scene._inputManager._onKeyUp, false);

Pinging @RaananW and @PolygonalSun who may help in this matter.

That shouldn’t happen, of course :slight_smile:
Is there any reproduction you can provide us on the playground? Something we can work with to test this?

I will try and recreate in a PG in the next few days. It may have something to do with the fact that I have multiple scenes, some not BS scenes but playing a video on an element with a higher z-order…but it was working before 4.2…it might be difficult to reproduce in PG but the process of trying that might even reveal the problem :slight_smile:


This is our main reason to ask a repro :smiley:

1 Like

I have done some hacking in my project by disabling code etc and I think I have isolated the problem and can recreate in the PG (at least it exhibits the same behaviour and is the sequence of calls I make amongst others so presumably it is the same problem).

The PG is here
as noted in the comments if you run this on 4.1 all is good but on 4.2 you get the behaviour I have described, ie keyboard input event listeners not reset/not firing until canvas looses and regains focus.

Problem found, deploying a fix very soon.


I am having a similar problem. Using a canvas with babylon at zIndex 0 and a canvas with p5.js at zIndex 1 for hud/gui. On mouse click, I engine.enterPointerLock() and the universal camera seems to work from the mouse only ie I can look around. keyboard input does not work. if I press tab once after the pointer is locked, the keyboard inputs then begin to respond. calling babylonCanvas.focus() after entering pointer lock does not fix this either. I am on latest versions. If I call canvas.focus() each frame, then the keyboard works too. but if I do a set timeout for focus after entering pointer lock, then it doesnt work. If I call for focus on mouseup event, then it works. Reproducing this on PG would probably be difficult due to using external libraries.

→ why do mouse inputs work on the universal camera without proper focus, but keyboard inputs do not?

So my understanding is that when you enterPointerLock, it will set the pointerLockElement for the page to the canvas element. When a pointerlock is active, all pointer input is routed to that element but focus is never set to that element. Without that active focus, none of the keyboard inputs are being routed to the canvas. If feels like requestPointerLock() (which enterPointerLock calls) should automatically focus on the canvas when executed but it doesn’t. Lemme test out and make sure that there isn’t are reason why we’re also not doing this.

PR to update enterPointerlock to call focus: Added function call to focus on canvas when setting PointerLock by PolygonalSun · Pull Request #12418 · BabylonJS/Babylon.js (