简体   繁体   中英

Using Key/MouseListeners on LWJGL?

I'm making a java application (not a game) that uses LWJGL and I was wondering if there was a way to add KeyListeners and MouseListeners to an application?

The setup I have is I've got a JFrame and a Canvas . The JFrame has some JPanel sidebars. I've tried adding them to the JFrame only to have them work on my sidebars. I've tried adding them to my Canvas only to see them work once.

I know LWJGL has it's own input classes, but I'm looking to use Listeners because that will give me the inputs when they happen.

My question boils down to this, is it possible to add KeyListeners and such to LWJGL applications or is there another way to get events when they happen? Or am I forced to create a thread and listen for events myself?

I know this is an old question, but for those who read this and look for the answer, here you have it (I think):

You can create a class which implements KeyListener and set it as the KeyListener for the JFrame .

If I'm not completely wrong, that will listen for key input as long as the JFrame is active. Hope this helps!

Example:

The key listener class:

public class MyCustomKeyListener implements KeyListener {
    // Implement your key listening
}

The main class:

public class JFrameWithLWJGL extends JFrame {

    // Our key listener
    private MyCustomKeyListener keyListener;

    public JFrameWithLWJGL() {
        // Create the key listener
        keyListener = new MyCustomKeyListener();
        // Set all JFrame properties here
        // Add the key listener to the frame
        add(keyListener);
    }

    public static void main(String[] args) {
        // Create an instance of the application
        new JFrameWithLWJGL();
    }
}

This is how I create KeyListeners: gist.github.com !

This is not possible with the current lwjgl 2.8.2 on Windows only.

The reason is that the Windows implementation of lwjgl clobbers a key data structure that AWT requires for event handling.

http://www.java-gaming.org/topics/cannot-add-mouselistener-to-java-awt-canvas-with-lwjgl-on-windows/24650/msg/208505/view.html#msg208505

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM