gpt4 book ai didi

java - 2D 文本框的键盘输入

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:05 25 4
gpt4 key购买 nike

所以我在过去的一天半里一直被这个问题困扰。我正在尝试在我的 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;导入静态org.lwjgl.glfw.GLFW.*;

公共(public)类键盘扩展 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方法是我做的。

谢谢!

最佳答案

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;
}
}

关于java - 2D 文本框的键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39687614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com