Detecting ESC in WebGL After Locking the Cursor

Hello. In a WebGL build, I have an update loop checking for when the player hits the ESC key. If the unity player has locked and hidden the cursor, hitting escape will NOT trigger the code to execute in the update loop, even though it does in the editor and desktop.

What is the best way to handle this? I simply want to achieve a situation where if the player is in first-person-mode with a locked and hidden cursor and they hit ESC, they will get their cursor back and stop causing player rotation. This seems like it should be easy, but I think some under-the-hood stuff with webgl is making it more difficult.

ALSO, i try detecting ESC with javascript, and if the cursor is locked, it doesnt detect it either…

What is the best, cleanest way to accomplish this?

Thank you!

I am facing the same issue in the WebGL build. I just wanted to hide the reticle when user press escape. But seems like in the update input for KeyCode.Escape is not called. Did you find any solution about it, or any alternate way to handle it? @theVirtunaut

Yes, don’t use the escape key. The escape key is part of the security sandbox of the browser. Pressing this key MUST take away the mouse control from the application / website. It also cancels fullscreen mode. Simply use any other key.

Using javascript to detect if the pointer is no longer locked

document.addEventListener(‘pointerlockchange’, function() {
if (document.pointerLockElement === null) {
// Cursor is no longer locked
PauseGame();
}
});
document.addEventListener(‘keydown’, function(event) {
if (event.key === ‘Escape’) {
// Check if the cursor is currently locked
if (document.pointerLockElement !== null) {
// Cursor is locked, so unlock it
document.exitPointerLock();
}
}
});