2

I cannot get the android "delete" key to register in my TextField (scene2d ui element in libgdx) listener. Here is my code to define the text field:

    nameTextfield = new TextField("", skin);
    nameTextfield.setMessageText("Some Text");

    uiStage.addActor(nameTextfield);

I tried this listener just to decode the keycode for the DELETE key:

    nameTextfield.setTextFieldListener(new TextFieldListener() {
        public void keyTyped (TextField textField, char key) {
            textField.setText(String.valueOf(Integer.valueOf(key)));

        }
    });

Although it gives code for almost for all buttons, it doesn't even react on DELETE button.

I tested this on a Nexus 7.

P.T.
  • 24,557
  • 7
  • 64
  • 95
sergant88
  • 109
  • 3
  • 8
  • i`ve found same problem here http://badlogicgames.com/forum/viewtopic.php?f=11&t=6324&p=29646&hilit=textfield#p29646 though there is no solve for it, and in my case SWYPE keyboard worked even worse - most keys touching had no reaction on TextField – sergant88 Jan 21 '13 at 00:32

4 Answers4

2

From the TextField.java source it looks like the "DELETE" (and "BACKSPACE", and "TAB" and a couple other keys ) are handled specially by the TextField. These keys are never forwarded to any listener.

The built-in handler should do "the right thing" (trimming characters off the string contents).

Is delete not behaving correctly for your case in some way that led you to try to decode it?

P.T.
  • 24,557
  • 7
  • 64
  • 95
0

Well the DELETE button should be implemented differently.

I suggest trying to verify if the pressed key is the DELETE button. If it is, you just do textField.getText(), trim the last letter off it, and set the new text with setText.

I'm sure there's a much more elegant way to do this, but it's the only workaround I can think of. After all, DELETE isn't really a char which you can throw inside setText. Is it? :/

LATER EDIT:

Print the key variable inside your listener, put a breakpoint there, and see what value is assigned to it.

Then also print (or check the javadoc) KeyEvent.KEYCODE_DEL (documentation here) to see what value this one takes.

Georgian
  • 8,795
  • 8
  • 46
  • 87
  • Verify if DELETE button was pressed is really a problem for me. I tried code like this: if(key == Keys.DEL){ textField.setText(textField.getText().substring(0, textField.getText().length() - 2)); } – sergant88 Jan 20 '13 at 23:56
  • Just put a simple `if( key == Keys.DEL ) {}` and put a breakpoint inside of it. See if it stops there. – Georgian Jan 21 '13 at 00:02
  • `Keys.DEL` is most certainly an `int`, whereas `key` is a char. Take that into consideration too. – Georgian Jan 21 '13 at 00:06
  • But it didnt work, this if statement was never true even if i touch DELETE key . And you are right DELETE is not a char that i can throw inside setText, i used that code to know key code of DELETE key - touching delete key gave no reaction. It seems like there is some bag in libgdx or some trick i dont know. – sergant88 Jan 21 '13 at 00:09
0

the best way to solve this is the following:

You will need to listen to inputs from another listner, for example the same screen.

MainMenuScreen implements Screen, InputProcessor

then you will need to create a multiplexer to let inputs been listen from both the stage and the listner.

multiplexer = new InputMultiplexer();

then add the two listners:

        multiplexer.addProcessor(this);
        multiplexer.addProcessor(stage);

Now you will have to simply delete the field from here:

    @Override
    public boolean keyDown(int keycode) {
             Gdx.app.log("Debug:", "keydown : "+keycode);
            //DO SMOTHING LIKE
            // if(keycode==...) deleteTextField();
            return true;
}

Let me know if you have questions about this solution. Worked great for me.

chelo_c
  • 1,739
  • 3
  • 21
  • 34
0

This issue has been solved by the latest nightly version of libgdx, the issue is known and discussed in the following link: nexus button

Community
  • 1
  • 1
Juri Adam
  • 569
  • 6
  • 21