Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Disable cursor unlocking when pressing ESC

Discussion in 'Editor & General Support' started by illinar, Jun 22, 2020.

  1. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    857
    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??
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    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.
     
    SPeeD0fShAd0W and illinar like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    illinar and PraetorBlue like this.
  4. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    857
    Yes, I'm using [ ~ ] key now. But I think the ability to disable this behavior should exist in Unity.
     
    Denis-535 likes this.
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    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.
     
    guilhermedaniluski and cloverme like this.
  6. WildcardMoo

    WildcardMoo

    Joined:
    Aug 28, 2020
    Posts:
    18
    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?
     
    Neiist and MUGIK like this.
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Why not? We have the
    #if UNITY_EDITOR
    for stuff like this.
    If you use the new InputSystem, it's even more easy.
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         private const string INPUT_ORIG_ESC_KEY = "<Keyboard>/escape";
    3.         private const string INPUT_TEMP_TAB_KEY = "<Keyboard>/tab";
    4. #endif
    5. // [...]
    6. #if UNITY_EDITOR
    7.     private void Awake()
    8.     {
    9.         Input.MainMenu.Back.ApplyBindingOverride(INPUT_TEMP_TAB_KEY, path: INPUT_ORIG_ESC_KEY);
    10.     }
    11. #endif
    Obviously doesn't matter how you get the
    Back
    InputAction
    , you can override its binding temporarily.