Search Unity

[Solved] How to Focus and Un Focus Input Field on KeyDown

Discussion in 'UGUI & TextMesh Pro' started by 420BlazeIt, Aug 11, 2015.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    This is basically my script:
    Code (CSharp):
    1. void Update ()
    2. {
    3.     if(Input.GetKeyDown(KeyCode.Return) && !isChatFocused)
    4.     {
    5.         isChatFocused = true;
    6.  
    7.         inputField.ActivateInputField();
    8.     }
    9.     if(Input.GetKeyDown(KeyCode.Return) && isChatFocused)
    10.     {
    11.         isChatFocused = false;
    12.  
    13.         inputField.DeactivateInputField();
    14.     }
    15. }
    The first part works fine and I am able to focus on the Input Field but for some reason I am not able to Un Focus from it when I press Return again. Please help.
     
  2. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I solved this problem by using else if statements.

    For example:
    Code (CSharp):
    1. void Update ()
    2. {
    3.     if(Input.GetKeyDown(KeyCode.Return) && !isChatFocused)
    4.     {
    5.         isChatFocused = true;
    6.    
    7.         inputField.ActivateInputField();
    8.     }
    9.     else if(Input.GetKeyDown(KeyCode.Return) && isChatFocused)
    10.     {
    11.         isChatFocused = false;
    12.    
    13.         inputField.DeactivateInputField();
    14.     }
    15. }