Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - Special characters in libgdx's textfield do not work

I can use setText("???") but if I type on my keyboard it doesn't show up, this doesn't work either

            public void keyTyped(TextField textField, char key) {
                JOptionPane.showMessageDialog(new JFrame(), key);
            }

The strange thing is that it doesn't work on mac but it does work on Windows, does anyone have an answer for that? Thank You!

Here's another question with a similarly topic!

How do you get input from special characters in Libgdx?

I have tried to get the ascii value and puting it through

Gdx.input.isKeyPressed(ascii value);

but it doesn't work. I have set my project encoding to UTF-8 and I can print special characters like ???.

Edit: I tried this

Gdx.input.setInputProcessor(new InputProcessor() {

        @Override
        public boolean keyDown(int keycode) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyUp(int keycode) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            System.out.println(character);
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            return false;
        }

    });

Didn't print ???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE

It seems, that this is related to a bug with unicode characters in lwjgl on OSX: [FIXED] Unicode input

If you update to the latest lwjgl library 2.9.1 this should be fixed. From the changelog:

2013-10-13 kappaOne

  • .../org/lwjgl/opengl/MacOSXNativeKeyboard.java, src/native/macosx/org_lwjgl_opengl_Display.m: Fix keyboard key codes to return Unicode characters instead of ASCII characters

see: 2.9.1-changelog.txt


Original Answer

How do you get input from special characters in Libgdx?

You can implement the interface com.badlogic.gdx.InputProcessor and set the current input processor by calling Gdx.input.setInputProcessor(inputProcessor );

Then you should be able to receive notifications about all typed characters (special or not) in the method InputProcessor#keyTyped(char c).

Actually this is more or less, what the class TextField does (not exactly, it listens for input by extending com.badlogic.gdx.scenes.scene2d.InputListener).

I had a quick look at the relevant source code of TextFields keyTyped method:

[...]
if (font.containsCharacter(character)) {
   // here the character gets added to the text field
}
[...]

So it seems, that the font of the text field must support the character, otherwise it will not be displayed. I have no mac to test this, but I would guess, that the BitmapFont used by default on your mac does not support the special characters. Try to set a different BitmapFont for the TextField (see TextField.setStyle(TextFieldStyle style)) and see if this solves the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...