简体   繁体   English

在LWJGL上使用Key / MouseListeners?

[英]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? 我正在制作一个使用LWJGL的Java应用程序(不是游戏),我想知道是否可以将KeyListeners和MouseListeners添加到应用程序中?

The setup I have is I've got a JFrame and a Canvas . 我的设置是我有一个JFrame和一个Canvas The JFrame has some JPanel sidebars. JFrame有一些JPanel栏。 I've tried adding them to the JFrame only to have them work on my sidebars. 我试图将它们添加到JFrame只是为了让它们在我的侧边栏上工作。 I've tried adding them to my Canvas only to see them work once. 我尝试将它们添加到“ Canvas只是为了看到它们工作一次。

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. 我知道LWJGL有它自己的输入类,但是我希望使用侦听器,因为这会在发生输入时为我提供输入。

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? 我的问题可以归结为这一点,是否可以向LWJGL应用程序中添加KeyListeners之类,还是有发生事件时获取事件的另一种方法? 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 . 您可以创建一个实现KeyListener的类,并将其设置为JFrameKeyListener

If I'm not completely wrong, that will listen for key input as long as the JFrame is active. 如果我没有完全错,那么只要JFrame处于活动状态,它就会侦听键输入。 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 ! 这就是我创建KeyListeners的方式: gist.github.com

This is not possible with the current lwjgl 2.8.2 on Windows only. 仅在Windows上,当前的lwjgl 2.8.2无法做到这一点。

The reason is that the Windows implementation of lwjgl clobbers a key data structure that AWT requires for event handling. 原因是Windows的lwjgl实现掩盖了AWT事件处理所需的关键数据结构。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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