I want to mark the start on the screen and press the letter to lock the pointer.
I wrote the code to hide the elements I clicked through the “visibility” option and lock the pointer through eventListener “mouseover”.
When you click the start character, the mouse lock function is currently completed, but the part where the menu is displayed again after detecting the “esc” key does not work.
Is there an example of a code that detects pointer lock when unlocking to “esc”?
The actions I want to do are as follows.
When you click the start button, the start button disappears and the mouse locks.
When you press esc, the restart button appears and the mouse is unlocked.
Do you have an example Playground (PG) of your code? Will be good for people to see what’s going on. I’m also curious, are you using Babylon GUI for this? Sounds like it, but if not I would really recommend it for something like this.
Sounds like you just need a bunch of boolean flags to guard against any clicks/updating pointer positions, but let’s see what you have.
Because the pointerlock consumes the first Escape press, you won’t be able to just listen and react to it directly but you can add a listener for pointerlockchange. If document.pointerLockElement isn’t equal to your canvas, you can then excute the same could that you would for your PauseMenu. Here’s an example PG that demonstrates how to do this: PointerLock Event Example | Babylon.js Playground (babylonjs.com).
As for you other question, it might be related to focus. When you first click on the screen, you set your focus to that element and any input event will be dispatched to it. If it doesn’t have focus, it won’t react to any keyboard input. If you call focus() on your canvas, at the beginning, it should address this.:
...
createScene();
canvas.focus();
let renderLoop ...