1

I'm writing a LWJGL application and I have some trouble getting the Keyboard and Mouse classes to work. The mouseWheelMoved, keyPressed and keyReleased methods are never called. I can confirm the listener.update method is called.

In this application I'm embedding the Display inside a JFrame, but even if I don't it still isn't working so that doesn't seem to be the problem.

Console Output:

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


Renderer Initialization:

@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");
}


Listener Class:

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

1 回答 1

2

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

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

于 2013-07-14T17:30:40.573 回答