If I hold the RightArrow key to run right, then press Enter to start chatting, the running state:
if (Input.GetKey(KeyCode.RightArrow) { ... }
// in Update()
gets locked until I press the RightArrow key again after ending the chat. Instead, I want to clear all Input states as soon as the chat TextField is focused, so that my character does not continue running later on. I’m handling the chat in OnGUI():
if (Event.current.Equals(Event.KeyboardEvent("return"))) {
chatEnter = !chatEnter;
}
if (chatEnter) {
GUI.SetNextControlName("ChatField");
chatString = GUI.TextField(..., chatString);
GUI.FocusControl("ChatField");
} else
GUI.FocusControl("");
- I need a way to clear the Input.GetKey state, or a workaround. I can’t simply stop my character moving
if (chatEnter)
because the character then undesirably continues moving later (even after chatEnter = false;). I searched this problem, and while there are similar issues, most simply refer to Event.current which is what i’m using.
Edit: I solved this after debugging. You need to set the focus in the OnGUI Repaint pass; otherwise it steals the Update Input for some reason. If anyone could comment and expand the reasoning, it would be useful.
if (chatEnter) {
if (Event.current.type == EventType.Repaint)
GUI.FocusControl("ChatField");
} else
GUI.FocusControl("");