Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
2

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 · Show 1 · 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 duvaork1 · Apr 05, 2021 at 04:43 PM 0
Share

I like this answer the most because it allows me to know if an input is focused without referencing the inputs or without needing linking everything with events, in the end this allows to reduce the coupling. Thanks.

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

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.

Follow this Question

Answers Answers and Comments

228 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 avatar image

Related Questions

Detect if mouse has clicked on a UI element? 0 Answers

,Key immediately registered 2 Answers

What is the difference between Getkey and Getkeydown (C#) 1 Answer

How do i convert inputfield input to float ? 0 Answers

Method Skipping Inputs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges