- Home /
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("");
Answer by Aeron0 · May 16, 2013 at 12:03 AM
//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
I mentioned that I tried this - the problem is not of stopping the character moving - its of the Input.Get$$anonymous$$ey states locking as focus is shifted to the chat. If I do this approach, the character stops (when chatting) then undesirably continues moving after the chat closes.
$$anonymous$$aybe this will help.
If you are using input for any kind of movement behaviour use Input.GetAxis. It gives you smoothed and configurable input that can be mapped to keyboard, joystick or mouse. Use Input.GetButton for action like events only. Don't use it for movement, Input.GetAxis will make the script code smaller and simpler.
Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop.
Class Funtion:ResetInputAxes Resets all input. After ResetInputAxes all axes return to 0 and all buttons return to 0 for one frame.
Your answer
Follow this Question
Related Questions
Emulate RightArrow GUI Event 0 Answers
Using GUI.FocusControl on TextField selects all text 4 Answers
Get key state while textfield has focus? 0 Answers
TextField not getting focus on iOS 1 Answer