找到了解决方案! 我创建了一个List,它接收所有键的键入,并在需要时输出它们! 问题是我的主要游戏状态不够快速更新。我希望我帮助至少有人在那里:)
package base.main.keyhandler;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class KeyHandler {
public static final int NUM_KEYS = 25;
public static int keyCode;
public static boolean anyKey;
public static boolean prevAnyKey;
public static boolean keyState[] = new boolean[NUM_KEYS];
public static boolean prevKeyState[] = new boolean[NUM_KEYS];
public static int[] registeredKeys = new int[]{
KeyEvent.VK_Z,
KeyEvent.VK_Q,
KeyEvent.VK_S,
KeyEvent.VK_D,
KeyEvent.VK_SPACE, //Attack
KeyEvent.VK_ENTER,//Accept
KeyEvent.VK_E,//inventory
KeyEvent.VK_F, //interact
KeyEvent.VK_T,//for console shouldn't be changeable. maybe for talking later ?
KeyEvent.VK_B,//for bounding boxes. shouldn't be changeable
KeyEvent.VK_1,
KeyEvent.VK_2,
KeyEvent.VK_3,
KeyEvent.VK_4,
KeyEvent.VK_5,
KeyEvent.VK_6,
KeyEvent.VK_7,
KeyEvent.VK_8,
KeyEvent.VK_9,
KeyEvent.VK_M,//Place button for xbox controllers
KeyEvent.VK_ESCAPE,
KeyEvent.VK_N, //escape button 2, for controllers only
};
public static int UP = 0;
public static int LEFT = 1;
public static int DOWN = 2;
public static int RIGHT = 3;
public static int SPACE = 4;
public static int ENTER = 5;
public static int INVENTORY = 6;
public static int INTERACT = 7;
public static int T = 8;
public static int B = 9;
public static int ONE = 10;
public static int TWO = 11;
public static int THREE = 12;
public static int FOUR = 13;
public static int FIVE = 14;
public static int SIX = 15;
public static int SEVEN = 16;
public static int EIGHT = 17;
public static int NINE = 18;
public static int PLACE = 19;
public static int ESCAPE = 20;
public static int ESCAPE2 = 21;
public static int ANYKEY = 22;
public static boolean anyKeyPress() {
for (int i = 0; i < NUM_KEYS; i++)
if (keyState[i])
return true;
return false;
}
public static String keyPressed(int i, String string){
// System.out.println(string);
if(typed.isEmpty())
return string;
System.out.println(typed.size() + " " + typed.get(0)+ " " + string);
if(keyState[ANYKEY] && !prevKeyState[ANYKEY] || typed.size() > 0){
String s = typed.get(0);
System.out.println("key passing " + s);
// String s = KeyEvent.getKeyText(keyCode);
if(s.length() == 1)
string +=s;
if(keyCode == KeyEvent.VK_BACK_SPACE && string.length() > 0){
String s2 = string.substring(0, string.length()-1);
string = s2;
}
if(keyCode == KeyEvent.VK_SPACE)
string+=" ";
if(keyCode == KeyEvent.VK_UNDERSCORE)
string+="_";
typed.remove(0);
typed.trimToSize();
// System.out.println("key typed "+s);
// System.out.println("string returned " + string);
}
return string.toLowerCase();
}
public static boolean isPressed(int i) {
return keyState[i] && !prevKeyState[i] ;
}
public static boolean isReleased(int i) {
return !keyState[i] && prevKeyState[i];
}
public static boolean isValidationKeyPressed(){
return (keyState[SPACE] && !prevKeyState[SPACE]) || (keyState[ENTER] && !prevKeyState[ENTER]);
}
private static ArrayList<String> typed = new ArrayList<>();
public static void keySet(int i, boolean b){
keyCode = i;
if(b)
typed.add(KeyEvent.getKeyText(i));
if (i == registeredKeys[UP])
keyState[UP] = b;
else if (i == registeredKeys[LEFT])
keyState[LEFT] = b;
else if (i == registeredKeys[RIGHT])
keyState[RIGHT] = b;
else if (i == registeredKeys[DOWN])
keyState[DOWN] = b;
else if (i == registeredKeys[SPACE])
keyState[SPACE] = b;
else if (i == registeredKeys[ENTER])
keyState[ENTER] = b;
else if (i == registeredKeys[ESCAPE])
keyState[ESCAPE] = b;
else if (i == registeredKeys[T])
keyState[T] = b;
else if (i == registeredKeys[INVENTORY])
keyState[INVENTORY] = b;
else if (i == registeredKeys[B])
keyState[B] = b;
else if (i == KeyEvent.VK_1){
keyState[ONE] = b;
}
else if (i == KeyEvent.VK_2){
keyState[TWO] = b;
}
else if (i == KeyEvent.VK_3){
keyState[THREE] = b;
}
else if (i == KeyEvent.VK_4){
keyState[FOUR] = b;
}
else if (i == KeyEvent.VK_5){
keyState[FIVE] = b;
}
else if (i == KeyEvent.VK_6){
keyState[SIX] = b;
}
else if (i == KeyEvent.VK_7){
keyState[SEVEN] = b;
}
else if (i == KeyEvent.VK_8){
keyState[EIGHT] = b;
}
else if (i == KeyEvent.VK_9){
keyState[NINE] = b;
}
else if(i == registeredKeys[INTERACT]){
keyState[INTERACT] = b;
}
//xbox only.
else if(i == registeredKeys[PLACE]){
keyState[PLACE] = b;
}
else if(i == registeredKeys[ESCAPE2]){
keyState[ESCAPE2] = b;
}
keyState[ANYKEY] = b;
}
public static void update() {
for (int i = 0; i < NUM_KEYS; i++)
prevKeyState[i] = keyState[i];
}
}
所以,我已经在这个小游戏上工作了一段时间。 游戏开始滞后,所以我写了另一个游戏循环来处理更新每个刻度,并且可以在需要时跳过帧。
我最近在游戏中添加了一个控制台,我可以输入一些命令来调试游戏的几个方面而无需等待。
然后我注意到我输入的键只有一半在游戏中打印出来....
调试后:
Key typed H
key passing H
Key typed E
key passing E
Key typed L
key passing L
Key typed L
key passing L
Key typed O
key passing O
Key typed Space
Key typed W
key passing W
Key typed O
Key typed R
key passing R
Key typed L
key passing L
Key typed D
每个键都应该有一个键入,传递给它,但情况并非如此...... (它应该印有hello world,但它打印了hellowrl)
仅供参考:键入的键直接打印在&#39; keyPressed&#39;方法
@Override
public void keyPressed(KeyEvent key) {
KeyHandler.keySet(key.getKeyCode(), true);
System.out.println("Key typed "+KeyEvent.getKeyText(key.getKeyCode()));
}
我查找了教程和其他人的关键问题,但似乎没有任何关联。我做错了什么?
这里是来自keyhandler的代码。
(如果我需要发布任何其他内容,请询问,我将编辑此帖子!)
package base.main.keyhandler;
import java.awt.event.KeyEvent;
public class KeyHandler {
public static final int NUM_KEYS = 25;
public static int keyCode;
public static boolean anyKey;
public static boolean prevAnyKey;
public static boolean keyState[] = new boolean[NUM_KEYS];
public static boolean prevKeyState[] = new boolean[NUM_KEYS];
public static int[] registeredKeys = new int[]{
KeyEvent.VK_Z,
KeyEvent.VK_Q,
KeyEvent.VK_S,
KeyEvent.VK_D,
KeyEvent.VK_SPACE, //Attack
KeyEvent.VK_ENTER,//Accept
KeyEvent.VK_E,//inventory
KeyEvent.VK_F, //interact
KeyEvent.VK_T,//for console shouldn't be changeable. maybe for talking later ?
KeyEvent.VK_B,//for bounding boxes. shouldn't be changeable
KeyEvent.VK_1,
KeyEvent.VK_2,
KeyEvent.VK_3,
KeyEvent.VK_4,
KeyEvent.VK_5,
KeyEvent.VK_6,
KeyEvent.VK_7,
KeyEvent.VK_8,
KeyEvent.VK_9,
KeyEvent.VK_M,//Place button for xbox controllers
KeyEvent.VK_ESCAPE,
KeyEvent.VK_N, //escape button 2, for controllers only
};
public static int UP = 0;
public static int LEFT = 1;
public static int DOWN = 2;
public static int RIGHT = 3;
public static int SPACE = 4;
public static int ENTER = 5;
public static int INVENTORY = 6;
public static int INTERACT = 7;
public static int T = 8;
public static int B = 9;
public static int ONE = 10;
public static int TWO = 11;
public static int THREE = 12;
public static int FOUR = 13;
public static int FIVE = 14;
public static int SIX = 15;
public static int SEVEN = 16;
public static int EIGHT = 17;
public static int NINE = 18;
public static int PLACE = 19;
public static int ESCAPE = 20;
public static int ESCAPE2 = 21;
public static int ANYKEY = 22;
public static boolean anyKeyPress() {
for (int i = 0; i < NUM_KEYS; i++)
if (keyState[i])
return true;
return false;
}
public static String keyPressed(int i, String string){
if(keyState[ANYKEY] && !prevKeyState[ANYKEY]){
System.out.println("key passing " + KeyEvent.getKeyText(keyCode));
String s = KeyEvent.getKeyText(keyCode);
if(s.length() == 1)
string +=s;
if(keyCode == KeyEvent.VK_BACK_SPACE && string.length() > 0){
String s2 = string.substring(0, string.length()-1);
string = s2;
}
if(keyCode == KeyEvent.VK_SPACE)
string+=" ";
if(keyCode == KeyEvent.VK_UNDERSCORE)
string+="_";
// System.out.println("key typed "+s);
// System.out.println("string returned " + string);
}
return string.toLowerCase();
}
public static boolean isPressed(int i) {
return keyState[i] && !prevKeyState[i] ;
}
public static boolean isReleased(int i) {
return !keyState[i] && prevKeyState[i];
}
public static boolean isValidationKeyPressed(){
return (keyState[SPACE] && !prevKeyState[SPACE]) || (keyState[ENTER] && !prevKeyState[ENTER]);
}
public static void keySet(int i, boolean b){
keyCode = i;
if (i == registeredKeys[UP])
keyState[UP] = b;
else if (i == registeredKeys[LEFT])
keyState[LEFT] = b;
else if (i == registeredKeys[RIGHT])
keyState[RIGHT] = b;
else if (i == registeredKeys[DOWN])
keyState[DOWN] = b;
else if (i == registeredKeys[SPACE])
keyState[SPACE] = b;
else if (i == registeredKeys[ENTER])
keyState[ENTER] = b;
else if (i == registeredKeys[ESCAPE])
keyState[ESCAPE] = b;
else if (i == registeredKeys[T])
keyState[T] = b;
else if (i == registeredKeys[INVENTORY])
keyState[INVENTORY] = b;
else if (i == registeredKeys[B])
keyState[B] = b;
else if (i == KeyEvent.VK_1){
keyState[ONE] = b;
}
else if (i == KeyEvent.VK_2){
keyState[TWO] = b;
}
else if (i == KeyEvent.VK_3){
keyState[THREE] = b;
}
else if (i == KeyEvent.VK_4){
keyState[FOUR] = b;
}
else if (i == KeyEvent.VK_5){
keyState[FIVE] = b;
}
else if (i == KeyEvent.VK_6){
keyState[SIX] = b;
}
else if (i == KeyEvent.VK_7){
keyState[SEVEN] = b;
}
else if (i == KeyEvent.VK_8){
keyState[EIGHT] = b;
}
else if (i == KeyEvent.VK_9){
keyState[NINE] = b;
}
else if(i == registeredKeys[INTERACT]){
keyState[INTERACT] = b;
}
//xbox only.
else if(i == registeredKeys[PLACE]){
keyState[PLACE] = b;
}
else if(i == registeredKeys[ESCAPE2]){
keyState[ESCAPE2] = b;
}
keyState[ANYKEY] = b;
}
public static void update() {
for (int i = 0; i < NUM_KEYS; i++)
prevKeyState[i] = keyState[i];
}
}
感谢。