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
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
}
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();}
}