• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Alex-P822 · Apr 28, 2020 at 08:02 PM · inputuser interfaceinputfieldgetkeygetkeyup

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

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, 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cuttlas-U · Apr 28, 2020 at 08:34 PM

hey ; what u did so far is the right way ; and i cant think of any other way rather then checking a bool variable ;

but what you can do to optimize your code is making a seprate Function for your input codes and check the bool the line before they are called ; some thing like this :

  void Update () 
 {
      AllMyInputFunctions();
 }
 
 
 void AllMyInputFunctions()
 {
 if (isEditingInputField) return;
 
 if (Input.GetKeyDown (KeyCode.L)) {doSomething();}
 
 if (Input.GetKeyDown (KeyCode.M)) {doSomething2();}
 
 if (Input.GetKeyDown (KeyCode.N)) {doSomething3();}
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by EmiyaSyahriel · Dec 17, 2020 at 11:34 AM

I used this method to determine whether is currently focused game object is an Input Field or not:

 public bool IsEditingInputField(){
     GameObject currentFocus = EventSystem.current.currentSelectedGameObject;
     if(currentFocus != null){ 
          // returns true if current gameObject has input field in it
         return currentFocus.TryGetComponent(out InputField _);
     } else {
         return false;
     }
 }

In newer C# version, it can be simplified to:

 public bool IsEditingInputField => 
     EventSystem.current.currentSelectedGameObject?.TryGetComponent(out InputField _) ?? false;

It's possible to make it a static method / property so it can be called anywhere as long as there is an EventSystem in current scene (which will be automatically spawned by Editor when adding a canvas to scene if none is available).

This is the entire helper class I use:

 public static class NonUIInput{
     // Check if is currently focused on input field
     public static bool IsEditingInputField => EventSystem.current.currentSelectedGameObject?.TryGetComponent(out InputField _) ?? false;

     // conditional layers over UnityEngine.Input.GetKey methods
     public static bool GetKeyDown(KeyCode key) => IsEditingInputField ? false : Input.GetKeyDown(key);

     public static bool GetKeyUp(KeyCode key) => IsEditingInputField ? false : Input.GetKeyUp(key);

     public static bool GetKey(KeyCode key) => IsEditingInputField ? false : Input.GetKey(key);
 }

Can be used like:

 if(NonUIInput.GetKeyDown(KeyCode.I)){
      // Your code here
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

227 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input Field between scenes 0 Answers

Input Field Problem 0 Answers

Link UI Input field to Google Map Script 1 Answer

Setting buttons used by Input.GetAxis() pragmatically. 0 Answers

I cant Multiply integer from Input Field 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges