The TextArea control (along with the TextField control) does not fire an EventType.KeyDown event when a keyboard key is pressed. Why is this, and what is a good workaround?
Here is a script that demonstrates this:
private string textAreaString = "Type here: !";
void Update () {
// Only happens on mouse click
if(Input.anyKeyDown)
Debug.Log("SUCCESS! Input.anyKeyDown is true!");
// Only happens on mouse click
if(Input.anyKey)
Debug.Log("SUCCESS! Input.anyKey is true!");
}
void OnGUI ()
{
textAreaString = GUILayout.TextArea(textAreaString);
// Only happens on mouse click
if(Event.current.type == EventType.KeyDown)
Debug.Log("SUCCESS! EventType.KeyDown fired!");
// Only KeyUp and used events happen
if(Event.current.type != EventType.Repaint && Event.current.type != EventType.Layout )
Debug.Log("Current event " + Event.current.type + " at " + Time.timeSinceLevelLoad);
}
I’m currently running Unity 4.0.1f2 on PC.
Hi, It happens when the event gets used by any other control. In this case, the KeyDown event is getting used by the TextArea control and so it doesnt fire.
– jiteshvm