Typing W, A, S, D in InputField moves character in the game

Hi! I have some screen space UI elements that have InputField’s for my HUD (specifically chat), but whenever a user presses W, S, A, or D, their FirstPersonController-driven character moves around in the game. Our users are complaining to us that they find this behavior very annoying.

Previously, I tried “locking” the keyboard from affecting the m_Input vector inside FirstPersonController as such, but the keyboard sometimes didn’t UNLOCK even after the InputField lost focus causing an extremely frustrating input lock where the user could no longer move at all using WASD, even though they were no longer typing:

    private static void PatchInputFields(Transform t)
    {
        InputField[] inputFields = t.GetComponentsInChildren<InputField>();
        foreach (InputField ipf in inputFields) {
            GameObject ipfObject = ipf.gameObject;
            EventTrigger eventTrigger = ipfObject.GetComponent<EventTrigger>();
            if (eventTrigger == null) {
                eventTrigger = ipfObject.AddComponent<EventTrigger>();
                GUIUtil.AddEventTrigger(eventTrigger, () =>
                {
                    Logging.Debug("Keyboard Locked: " + ipf.gameObject.name);
                    KeyboardLocked = true;
                }, EventTriggerType.PointerClick);
                ipf.onEndEdit.AddListener((string unused) =>
                {
                    Logging.Debug("Keyboard Unlocked");
                    KeyboardLocked = false;
                });
            } else {
                Logging.Debug("PatchInputFields: Skipping InputField GO: " + ipfObject.name);
            }
        }
    }

Is there a more robust way to programmatically check if an InputField has focus? I couldn’t find anything mentioned in the Unity documentation. It seems like checking for focus is an obvious core feature for any GUI framework to be functional – something that Unity would definitely have.

Any programmer would tell you that relying on global state (i.e. my KeyboardLocked flag) for this kind of bookkeeping is a disaster waiting to happen, so I’m curious to hear if anyone has a better approach in mind? The robust thing to do would be to somehow check for focus every frame over all InputField components instead.

We’re using Unity 5.2.0f3 for our game project.

i’ve has the same problem, the solution was to check the eventsystem’s selected gameobject (i’m using my own controller):

        if (EventSystem.current
            && EventSystem.current.currentSelectedGameObject
            && EventSystem.current.currentSelectedGameObject.GetComponentInChildren<InputField> ())   
            return; 

       // respond to input, move the controller etc

Sweet! Thanks! :slight_smile: