gpt4 book ai didi

java - LWJGL 键盘输入不起作用

转载 作者:行者123 更新时间:2023-11-30 04:18:51 24 4
gpt4 key购买 nike

我正在编写 LWJGL 应用程序,但在使键盘和鼠标类正常工作时遇到一些问题。 mouseWheelMoved、keyPressed 和 keyReleased 方法永远不会被调用。我可以确认调用了listener.update方法。

在此应用程序中,我将 Display 嵌入到 JFrame 中,但即使我不这样做,它仍然无法工作,因此这似乎不是问题。

控制台输出:

Drew Map
Keyboard & Mouse listener initialized
(Few minutes of key pressing here)
Application Close Requested
Application Closing
Renderer Stopping
Renderer End


渲染器初始化:

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

最佳答案

我想我需要调用“Display.update()”来刷新键盘输入。

您可以忽略此行为并通过调用 Display.processMessages()、Mouse.poll() 和 Keyboard.poll() 手动更新输入。

关于java - LWJGL 键盘输入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641222/

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