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

[Bug] TextField uses KeyUp event (once) -> Input.GetKey to return incorrect value

Discussion in 'Immediate Mode GUI (IMGUI)' started by greg-h, Nov 17, 2013.

  1. greg-h

    greg-h

    Joined:
    Jul 29, 2012
    Posts:
    17
    EDIT: Unity version 4.2.1f4

    Context (skip down for explicit instructions to recreate):

    I'm using a CharacterController to move the player avatar with input through Input.GetKey(KeyCode) in the Update loop.

    In game chat is engaged by detecting the Return key being pressed* in the OnGUI loop, which begins calling GUI.TextField(..).

    *Condition for detecting return key: (Event.current.type == EventType.KeyDown Event.current.keyCode == KeyCode.Return); Event.current.Used() is not being called.

    If the user is pressing a KeyCode _key to move the character and simultaneously presses and releases the return button, Input.GetKey(_key) will return true until GUI.TextField is no longer called AND the user presses and releases _key.

    To recreate:

    Code (csharp):
    1. public class UpdateInputUser : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         if (Input.GetKey(KeyCode.W))
    6.         {
    7.             Debug.Log("W key down");
    8.         }
    9.     }
    10. }
    Code (csharp):
    1. public class TextFieldDrawer : MonoBehaviour
    2. {
    3.     private bool _drawingTextField = false;
    4.  
    5.     void OnGUI()
    6.     {
    7.         if (Event.current.type == EventType.KeyDown  Event.current.keyCode == KeyCode.Return)
    8.         {
    9.             _drawingTextField = !_drawingTextField;
    10.         }
    11.  
    12.  
    13.         if (_drawingTextField)
    14.         {
    15.             GUI.SetNextControlName("TextField");
    16.             GUI.TextField(new Rect(0, 0, 50, 50), "");
    17.             GUI.FocusControl("TextField");
    18.         }
    19.     }
    20. }
    1. Assign the two MonoBehaviours to an object.
    2. Start the game in the editor.
    3. Press (and hold) the W key.
    4. While pressing the W key, press and release return.
    5. Release the W key.
    6. Observe that "W key down" is still being printed every frame.
    7. Observe that no key can be entered into the TextField to cause Input.GetKey(KeyCode.W) to return false.
    8. Press and release return.
    9. Observe that "W key down" is still being printed every frame.
    10. Press and release the W key.
    11. Observe that "W key down" is no longer being printed every frame.
    12. Repeat steps 1-5 and observe that pressing and releasing the return key while pressing the W key causes "W key down" to cease printing.

    When I first discovered the bug, the problem occurred consistently (it did not fix itself after the first time I pressed return). After starting a new project to isolate the code causing the issue, I could only recreate it as described above.

    EDIT:

    This code will cause the issue to appear consistently:

    Code (csharp):
    1.  
    2. public class TextFieldDrawer : MonoBehaviour
    3. {
    4.     private bool _drawingTextField = false;
    5.  
    6.     void OnGUI()
    7.     {
    8.         if (Event.current.type == EventType.KeyDown  Event.current.keyCode == KeyCode.Return)
    9.         {
    10.             _drawingTextField = !_drawingTextField;
    11.         }
    12.  
    13.  
    14.         if (_drawingTextField)
    15.         {
    16.             GUI.SetNextControlName("TextField");
    17.             GUI.TextField(new Rect(0, 0, 50, 50), "");
    18.             GUI.FocusControl("TextField");
    19.         }
    20.         else
    21.         {
    22.             GUI.FocusControl(null);
    23.         }
    24.     }
    25. }
    26.  
     
    Last edited: Nov 17, 2013