Why won't my if result to true?

Please help me understand what I am doing wrong.


Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.keyDown

results to true when I hit enter both outside and inside my TextArea

And this GUI.GetNameOfFocusedControl() == "textField"
results to true when I focus on my textArea Set to “textField”

but when I do this

(Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.keyDown && GUI.GetNameOfFocusedControl() == "textField")

Nothing… Never gets set to true when I hit enter in my textField. I don’t understand.

When you hit enter on a textfield, the event has already been “used” by the control by the time it gets to OnGUI, so instead of getting a keyDown event, you’ll get a used event instead.

Try changing your code to:

if (Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.used && GUI.GetNameOfFocusedControl() == "textField")