Tackwin
Tackwin

Reputation: 69

libgdx, textfield test focus

I would like to know how to check if a textfield is focus. I have already add a focusListener like this : field.addListener( new FocusListener() {});. But what's next in the if() ?

Upvotes: 1

Views: 943

Answers (1)

Porlune
Porlune

Reputation: 749

I wrote this to detect when a libgdx textfield has been focused using either the mouse or the tab key.

<textfield>.addListener(new FocusListener(){
        @Override
        public boolean handle(Event event){
        if (event.toString().equals("mouseMoved") 
                || event.toString().equals("exit") 
                || event.toString().equals("enter") 
                || event.toString().equals("keyDown") 
                || event.toString().equals("touchUp")){

            return false;
        }
            //add your focus handling code here.
            System.out.println("focused");

            return true;
        }

    });

Simply call this method and whenever the textfield is focused, using either the tab key or a click, it will print "focused" to your console.

Feel free to ask for more if needed!

I hope this helps!

Upvotes: 2

Related Questions