libgdx的文本字段中的特殊字符不起作用 [英] Special characters in libgdx's textfield do not work
问题描述
我可以使用setText(åäö"),但是如果我没有在键盘上键入内容,这也不起作用
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);
}
奇怪的是,它不能在Mac上运行,但可以在Windows上运行,有人对此有答案吗? 谢谢!
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!
如何从Libgdx中的特殊字符获取输入?
我试图获取ascii值并将其通过
I have tried to get the ascii value and puting it through
Gdx.input.isKeyPressed(ascii value);
但是它不起作用.我已将项目编码设置为UTF-8,并且可以打印åäö等特殊字符.
but it doesn't work. I have set my project encoding to UTF-8 and I can print special characters like åäö.
我尝试过
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;
}
});
未打印åäö
推荐答案
更新
这似乎与OSX上lwjgl中的Unicode字符错误有关: [已修复] Unicode输入
It seems, that this is related to a bug with unicode characters in lwjgl on OSX: [FIXED] Unicode input
如果您更新到最新的lwjgl库2.9.1,则应解决此问题.从变更日志中:
If you update to the latest lwjgl library 2.9.1 this should be fixed. From the changelog:
2013-10-13 kappaOne
2013-10-13 kappaOne
- .../org/lwjgl/opengl/MacOSXNativeKeyboard.java, src/native/macosx/org_lwjgl_opengl_Display.m:修复键盘键 代码以返回Unicode字符而不是ASCII字符
- .../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
请参阅: 2.9.1-changelog.txt
原始答案
如何从Libgdx中的特殊字符获取输入?
How do you get input from special characters in Libgdx?
您可以实现接口com.badlogic.gdx.InputProcessor并通过调用Gdx.input.setInputProcessor(inputProcessor );
You can implement the interface com.badlogic.gdx.InputProcessor and set the current input processor by calling Gdx.input.setInputProcessor(inputProcessor );
然后,您应该能够在InputProcessor#keyTyped(char c)方法中接收有关所有键入字符(无论是否特殊)的通知.
Then you should be able to receive notifications about all typed characters (special or not) in the method InputProcessor#keyTyped(char c).
实际上,这或多或少是TextField类所做的(不完全是,它通过扩展com.badlogic.gdx.scenes.scene2d.InputListener来监听输入).
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).
我快速浏览了TextFields keyTyped方法的相关源代码:
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
}
[...]
因此,似乎文本字段的字体必须支持该字符,否则它将不会显示.我没有Mac可以对此进行测试,但是我想,您Mac上默认使用的BitmapFont不支持特殊字符.尝试为TextField设置其他BitmapFont(请参见TextField.setStyle(TextFieldStyle样式)),看看是否可以解决问题.
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.
这篇关于libgdx的文本字段中的特殊字符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!