libgdx 的文本字段中的特殊字符不起作用
我可以使用 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 字符的代码
请参阅:2.9.1-changelog.txt
原答案
如何从 Libgdx 中的特殊字符中获取输入?
How do you get input from special characters in Libgdx?
可以通过调用Gdx.input.setInputProcessor(inputProcessor);实现com.badlogic.gdx.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 style)),看看是否能解决问题.
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.
相关文章