Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting key input to ignore when a player is typing in an input box.

Discussion in 'Editor & General Support' started by Tnagele, Mar 30, 2015.

  1. Tnagele

    Tnagele

    Joined:
    Jan 3, 2015
    Posts:
    17
    The title says it all, basically I have a developer console and if I type in it, I can still walk for example if I press W or any other keys that control my menu UI.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    If the menu is enabled set a flag. In your player code, skip the key checks if the menu flag is true.
     
  3. Tnagele

    Tnagele

    Joined:
    Jan 3, 2015
    Posts:
    17
    Yeah that's what I figured, thanks.

    Edit: Just did it based on whether the cursor was visible or not as I have a toggle for that and it toggles automatically when a menu opens ;)
     
    Last edited: Apr 1, 2015
  4. Kushulain

    Kushulain

    Joined:
    Sep 20, 2012
    Posts:
    19
    Last edited: Sep 15, 2015
  5. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    30
    You can check like this (Not very performant if you have a lot of selectables in the scene):

    Code (CSharp):
    1.     private static bool IsPointerFocusedInputField()
    2.     {
    3.  
    4.         var selectables = Selectable.allSelectables;
    5.         foreach (var selectable in selectables)
    6.         {
    7.             var inputField = selectable as InputField;
    8.             if (inputField && inputField.isFocused)
    9.             {
    10.                 return true;
    11.             }
    12.         }
    13.         return false;
    14.     }
     
    mgstauff likes this.
  6. mgstauff

    mgstauff

    Joined:
    Sep 6, 2017
    Posts:
    59
    @ibrahimpenekli's method works, thanks for that.

    I came here however looking to know when *any* UI element has foucs, i.e. is selected. This is because I need to prevent mouse click-and-drag from rotating my scene view when the user is actually adjusting a slide. My UI is always up and I don't want to add handlers to every control to track when the mouse enters and exits.

    So this simple check works well for anyone else coming here for the same reasons as I

    Code (CSharp):
    1.     private bool IsUIactive()
    2.     {
    3.         return EventSystem.current.currentSelectedGameObject != null;
    4.     }