我正在尝试使用LWJGL来获取是否按下某个键。如果按下退出键,则应用程序退出。但是,我无法读取任何键盘输入,尽管Display.isCloseRequested()
工作正常。
我在RHEL上使用LWJGL 2.6和Java 1.6。
for(;;) {
// check if we want to quit
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
System.exit(0); // can't get this to happen!
}
if(Display.isCloseRequested()) {
System.exit(0);
}
/* timer code omitted */
render();
Display.update();
}
编辑:完全相同的代码在我的Windows机器上完全正常,使用相同版本的lwjgl和JRE。
答案 0 :(得分:0)
也许您可以使用isCreated
功能检查键盘是否已创建?
除此之外,我在编程方面并不是那么好,所以我无法为你提供任何其他输入。
试试这个
Keyboard.isCreated()
答案 1 :(得分:0)
我可能会或可能没有帮助/恢复这里的死主题,但对于任何流氓Google员工,我都会给你:
It's my Input class from my Zdeva Engine
在这里,您无需下载整个'引擎'..
package LWJGL;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
public class Input
{
public static boolean[] mouseButtons = {false, false, false};
public static int[] mousePos = new int[Mouse.getButtonCount()];
public static int[] keysBound = {Keyboard.KEY_A, Keyboard.KEY_B};
/**
* Initializes the input system. Loads keyconfig.
*
*/
public static void init()
{
System.out.println("Initializing input system...");
//Eventually will check for OS, and adjust keys accordingly.
System.out.println("Input system initialized!");
}
/**
* Updates all mouse info, keys bound, and performs actions.
*/
public static void tick()
{
mouseButtons[0] = Mouse.isButtonDown(0);
mouseButtons[1] = Mouse.isButtonDown(1);
mousePos[0] = Mouse.getX();
mousePos[1] = Mouse.getY();
while(Keyboard.next())
{
if(Keyboard.getEventKeyState())
{
doAction(Keyboard.getEventKey(), false);
}
}
for(int key : keysBound)
{
if(Keyboard.isKeyDown(key))
{
doAction(key, true);
}
}
while(Mouse.next())
{
doAction(-1, false);
}
doAction(0, true);
}
/**
* Does the associated action for each key. Called automatically from tick.
* @param key The key to check & perform associated action
*/
public static void doAction(int key, boolean ifRepeat)
{
if(mouseButtons[0])
{
}
if(mouseButtons[1])
{
}
if(key == keysBound[0] & ifRepeat)
{
System.out.println("a");
}
if(key == keysBound[1] & !ifRepeat)
{
System.out.println("b");
}
}
}