所以我在过去的一天半里一直坚持这个问题。 我正试图在我的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;
import org.lwjgl.glfw.GLFWKeyCallback; import static org.lwjgl.glfw.GLFW。*;
public class Keyboard扩展了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方法由我制作。
谢谢!
答案 0 :(得分:0)
string key2 = null;
public static char checkAllKeys() {
char key = '*';
if(isKeyDown(GLFW_KEY_A)) {
key2 = 'a';
}
if(isKeyDown(GLFW_KEY_Z)) {
key2 = 'z';
}
if(isKeyDown(GLFW_KEY_SPACE)) {
key2 = ' ';
}
if(isKeyDown(GLFW_KEY_BACKSPACE)) {
key2 = '/';
}
if(isKeyDown(GLFW_KEY_ENTER)) {
key2 = '{';
}
if(!isKeyDown(GLFW_KEY_A)&&key2=='a') {
key = 'a';
key2 = null;
}
if(!isKeyDown(GLFW_KEY_Z)&&key2=='z') {
key = 'z';
key2 = null;
}
if(!isKeyDown(GLFW_KEY_SPACE)&&key2==' ') {
key = ' ';
key2 = null;
}
if(!isKeyDown(GLFW_KEY_BACKSPACE)&&key2=='/') {
key = '/';
key2 = null;
}
if(!isKeyDown(GLFW_KEY_ENTER)&&key2=='{') {
key = '{';
key2 = null;
}
}