How to detect if a input field is selected

Can’t figured out how to detect if a InputField is currently selected.
I don’t see any options for it in the list.
I see IsFocused but no IsSelected?

So that puzzles me.
I do see Select where I guess I select the selectable then maybe I use that to find if its selected?

on the input field there is an ‘on value changed’
you could use this to keep up with which one is being selected…though, now that you mention, could have swore there was another way I’ve used semi recently, will check some scripts when I wake up, read this one right before crashing, provided it hasn’t been answered by then :wink:

but at the very least, you could have it send a value to a script and go by which one sent the value last =p

1 Like

Thanks for the response Raven. That’s actually not how I will be using it :stuck_out_tongue:

I have two InputFields
When Return is hit on the keyboard it will do some stuff but only if the player still has one of them selected.
This way I can use the enter key for other inputs in my game as well.

that’s why I can’t use on value changed. Since I don’t want it to trigger when just one of the two inputs value has been changed.

Maybe I can detect if a carrot exists. Going to check into that.

is confused
keep in mind, onvaluechanged and EndEdit doesn’t have to trigger the event, you can use it just to keep up with what they’ve done. ie, as you want to make sure they have changed both of them, first would trigger a bool value input1used to true etc, then just use an if statement to make sure both have been filled out. Using that way, you can even verify what they put in is valid.

granted, none of that will tell you if they’re still selected >_< cause end edit, if I remember right, will leave the cursor in the last input field…hmm.

Tell me this, are the input fields visible throughout the game, or can you separate the UI’s .so that you can switch the Enter’s behavior based on which UI element you have active?

Check out the "InputFocus" control in the UI Extensions project (link in sig), should be able to help you out or at the very least set you on the right path.

The inputs are always active. I’ll show you guys a picture.
I have a map system with x and y cords.
player types in the x and y and hits enter
but enter will only effect that input if x or y is still selected.

This way when someone is typing in the chat box they can hit enter and it doesn’t trip the x and y function.

Anyways I figured out a pretty simple way to do it. I forgot I would be using the place holder on the inputs.
So when they hit enter the input amount gets removed and replaced with the placeholder.
This way when they hit enter if there is any thing in those boxes then they must be editing it. or wanting to change sector.

I’m not totally happy with this idea though.
what if they type in some numbers then someone sends them a chat message and they go and type a reply and hit enter and it changes the sector.
I could of course just clear the sector input if they click the chat input to but I was trying to find a more cleaner way of doing this instead of just creating Boolean switches etc…

Hello, I know this is an old thread, but I got this problem and I fix it with an “OK” solution.
I hope it can help some people.

So what I did was make a subclass of inputfield on overrided the “OnSelect” method. I added a static variable for the Currently Selected inputfield.

So first of all create a new class called MyInputField.cs that extends InputField

using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MyInputField : InputField {

    public static InputField Selected;

    public override void OnSelect(BaseEventData eventData) {
        base.OnSelect(eventData);
        Selected = this;
    }

}

then, when you want to check if your inputfield is selected just check

if (MyInputField.Selected == PasswordInput as MyInputField) Submit();

I hope it was useful, if you wanna learn more, you can visit my Youtube channel “Creagines”

Bye bye

4 Likes

Thanks to Rider’s decompile functionality I was able to dig into the Selectable source and figure out where this information goes. Turns out it’s EventSystem. You can be sure that no Selectable is selected (which includes InputFields) by simply checking this bool:

EventSystem.current.currentSelectedGameObject == null

It technically just returns a GameObject but all Selectable objects set this value when they are selected.

18 Likes