Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity UI How to disable Input Events (OnKeyDown etc.) while editing InputField?

Discussion in 'UGUI & TextMesh Pro' started by Alex-P822, Apr 28, 2020.

  1. Alex-P822

    Alex-P822

    Joined:
    Nov 16, 2013
    Posts:
    2
    Hello everyone,

    i started developing several on-key actions like this:

    void Update () {if (Input.GetKeyDown (KeyCode.L)) {doSomething();}}


    I later added a UI Canvas with an Event System and a Standalone Input Module.
    After this, i also added an InputField GameObject to my UI Canvas.

    Now i have the problem, that when i enter a letter into the text field all Input.GetKeyDown actions are fired, even though i intend to not fire any key events other than needed for editing the UI InputField.

    Is there an easy way to disable input key events while focus is in UI fields?

    I already thought about adding a global variable like isEditingInputField which i can set on focus and on leave of the inputfield (as described here and here) and perform all keydown-checks only when it is set to false, but this would require a lot of refactoring and dependencies in the already implemented scripts.

    void Update () {if (!isEditingInputField && Input.GetKeyDown (KeyCode.L)) {doSomething();}}


    Is there a better way to do it?

    Kind Regards




    Tags: UI Input Event KeyCode GetKeyDown GetKeyUp GetKey InputField
     
    Last edited: Apr 28, 2020
  2. Alex-P822

    Alex-P822

    Joined:
    Nov 16, 2013
    Posts:
    2