GUI / Keyboard observable issue when setting canvas.focus() immediately when creating scene

Hello, thanks as always for a great library. In my current project, I am calling canvas.focus() to set focus on the canvas once my app is loaded, in order to be able to immediately listen for keyboard observables. I have found that there is some issue with Gui components receiving keyboard observable events in this case, until I manually set focus away from the canvas and back again.

The following playground more or less reproduces the issue, though in order to reproduce the issue in the playground, I need to set focus to the canvas immediately in the createScene function, while in my own project, I am doing it after the scene has been created.

https://www.babylonjs-playground.com/#UWS0TS#28

Initially the playground will behave, but if you click ‘Run’ again and then try to interact with the input, you’ll see that your mouse events are handled properly (you can click into the InputText), but keystrokes are ignored.

I can not really repro here but could you try setting the focus after calling attach ? https://www.babylonjs-playground.com/#UWS0TS#30

2 Likes

That seems to work. Actually, for the scene in question I didn’t want any camera movement so I wasn’t bothering to attach the camera to the canvas at all, but I can just clear the inputs I guess.

Anyway, this seems to resolve my issue. I suppose this is a edge case but its possible others could run into this scenario in the future.

2 Likes