我正在编写一个LWJGL应用程序,我在使键盘和鼠标类工作时遇到了一些麻烦。永远不会调用mouseWheelMoved,keyPressed和keyReleased方法。我可以确认调用了listener.update方法。
在这个应用程序中,我将Display嵌入到JFrame中,但即使我不这样做,它仍然无法工作,所以这似乎不是问题。
控制台输出:
德鲁地图
键盘&鼠标监听器初始化为 (按键按下几分钟)
申请截止申请
申请结束
渲染器停止
渲染器结束
渲染器初始化:
@Override
public void run() {
try {
Display.setParent(canvas);
Display.create();
Display.setResizable(false);
Display.setTitle("Display");
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glOrtho(0, 1, 0, 1, 0, 1);
draw();
listener.initialize();
} catch (LWJGLException ex) {
System.out.println("Could not initialize renderer: " + ex.getMessage());
Thread.currentThread().interrupt();
}
do {
listener.update();
if(shouldUpdate)
draw();
Thread.yield();
} while(!closing);
System.out.println("Renderer Stopping");
try {
Display.destroy();
} catch(Exception ex) {
System.out.println("Error while stopping renderer: " + ex.getMessage());
}
listener.destroy();
System.out.println("Renderer End");
}
听众类:
public void initialize() {
try {
Mouse.create();
Keyboard.create();
Keyboard.enableRepeatEvents(true);
System.out.println("Keyboard & Mouse listener initialized");
} catch (LWJGLException ex) {
System.out.println("Could not initialize listener");
Thread.currentThread().interrupt();
}
}
public void update() {
while(Keyboard.next()) { // Never True
int key = Keyboard.getEventKey();
if(Keyboard.getEventKeyState())
keyPressed(key); // Never Called
else
keyReleased(key); // Never Called
System.out.println(Keyboard.getKeyName(key)); // Never Printed
}
int amount = Mouse.getDWheel(); // Always 0
if(amount != 0)
mouseWheelMoved(amount); // Never Called
}
答案 0 :(得分:2)
我想我需要调用“Display.update()”来刷新键盘输入。
您可以省略此行为并通过调用Display.processMessages(),Mouse.poll()和Keyboard.poll()手动更新输入。