TextField, Event.current, Input.GetKey, and GUI.FocusControl locking

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("");

//Can’t you add a bool variable chatEnter in your movement code and OnGUI?
//something like

// make the same bool variable available in both the movement code and OnGUI code
bool chatEnter;

// Wherever your movement code is located
if(chatEnter == false)
  {
    // movement code goes here
  }

// OnGUI code
// You need code in OnGUI to make chatEnter true

If(whatever code you have for here)
{
  chatEnter = true;
}

if(chatEnter == true)
  {

  }

// And you will need to set chatEnter back to false when chat closes