Disable cursor unlocking when pressing ESC

Currently if I press ESC the cursor unlocks until I click on the game window again. This is absolutely disruptive for the game testing. How do I avoid that behavior??

I’m pretty sure there’s nothing you can really do. Hitting escape in the editor while in Play Mode will make the mouse cursor visible again. If this is a deal breaker:

  • This will only be an issue in the editor, so you can try testing in a build, where this behavior won’t occur.
  • You can pick a different key other than Escape which you use while in the editor, or map an additional key besides Escape to that behavior.

I’m awaiting the day when we can simply use the Escape key (or Alt keys, for that matter) in Unity without side effects.

3 Likes

You should be testing your game in a build instead of in the editor primarily. But for testing this in the editor you check if you’re running in the editor, and if so you use an alternate key instead of ESC. In a build you use ESC as normal.

2 Likes

Yes, I’m using [ ~ ] key now. But I think the ability to disable this behavior should exist in Unity.

2 Likes

Yes, I’m saying for years that we need to be able to redefine all of the shortcuts, not just the ones that have menu item. This includes the ESC when escaping the game view in the editor.

3 Likes

I am honestly baffled reading this. The controls in my game are bindable and not hardcoded (as in any game, I hope), with a default key of Esc set for certain actions like pause or exiting menus. I can’t just hard code a different key for it in case we’re running in the editor. So I show the mouse cursor when entering a menu, and then lock it again when exiting, only for it to reappear because Unity is like “Oh, Escape has been pressed, happy days”.

The fact that Unity ties a completely unnecessary functionality to one of the most commonly used keys, that will be only used in the editor, with no way to disable it or bind it to a different key is simply insane… Who thought that would be a good idea?

4 Likes

Why not? We have the #if UNITY_EDITOR for stuff like this.
If you use the new InputSystem, it’s even more easy.

#if UNITY_EDITOR
        private const string INPUT_ORIG_ESC_KEY = "<Keyboard>/escape";
        private const string INPUT_TEMP_TAB_KEY = "<Keyboard>/tab";
#endif
// [...]
#if UNITY_EDITOR
    private void Awake()
    {
        Input.MainMenu.Back.ApplyBindingOverride(INPUT_TEMP_TAB_KEY, path: INPUT_ORIG_ESC_KEY);
    }
#endif

Obviously doesn’t matter how you get the Back InputAction, you can override its binding temporarily.