1
所以我一直在過去的一天和一半卡住這個問題。 我想在我的LWJGL遊戲中實現2D可寫文本框。文本框的渲染不是問題,並且工作完美無瑕。二維文本框的鍵盤輸入
但是我的輸入並不是很好。問題是,我無法弄清楚如何檢測單個按鍵,所以不是在我的輸入字符串中添加「a」,而是添加:「aaaaaaaaaaaaaaaaaa」,因爲遊戲時鐘非常快。
這是我的代碼:
private boolean canType = false;
private static long curTime= System.currentTimeMillis();
private static long keyTypeTime = System.currentTimeMillis();
String input = "";
// Game loop
// I'm using a timer to limit typing, which doesn't work that well.
if (curTime - keyTypeTime >= 100) {
keyTypeTime = System.currentTimeMillis();
canType = true;
}
if (canType) {
char c = Keyboard.checkAllKeys();
canType = false;
if (c != '*') {
if (c == '/') {
System.out.println("backspace");
if (input != null && input.length() > 0) {
input = input.substring(0, input.length() - 1);
}
} else if (c == '{') {
storyLogic();
} else {
input += c;
}
}
}
這是我的實際鍵盤類,而 「checkAllKeys」 的方法:
package me.mateo226.main;
進口org.lwjgl.glfw.GLFWKeyCallback; import static org.lwjgl.glfw.GLFW。*;
公共類鍵盤擴展GLFWKeyCallback {
public static boolean[] keys = new boolean[65536];
// The GLFWKeyCallback class is an abstract method that
// can't be instantiated by itself and must instead be extended
//
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
// TODO Auto-generated method stub
keys[key] = action != GLFW_RELEASE;
}
// boolean method that returns true if a given key
// is pressed.
public static boolean isKeyDown(int keycode) {
return keys[keycode];
}
public static char checkAllKeys() {
char key = '*';
if(isKeyDown(GLFW_KEY_A)) {
key = 'a';
}
if(isKeyDown(GLFW_KEY_Z)) {
key = 'z';
}
if(isKeyDown(GLFW_KEY_SPACE)) {
key = ' ';
}
if(isKeyDown(GLFW_KEY_BACKSPACE)) {
key = '/';
}
if(isKeyDown(GLFW_KEY_ENTER)) {
key = '{';
}
return key;
}
}
我還在學習LWJGL 3所以鍵盤類是不是我的,只有checkAllKeys方法是我做的。
謝謝!