LWJGL不会读取键盘输入

发布于 2024-09-29 04:21:38 字数 549 浏览 5 评论 0原文

我正在尝试使用 LWJGL 来获取是否按下了某个键。如果按下退出键,则应用程序退出。但是,尽管 Display.isCloseRequested() 工作正常,但我无法让它读取任何键盘输入。

我在 RHEL 上使用 LWJGL 2.6 和 Java 1.6。

for(;;) {
    // check if we want to quit

    if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
        System.exit(0);  // can't get this to happen!
    }
    if(Display.isCloseRequested()) {
        System.exit(0);
    }

/* timer code omitted */

    render();
    Display.update();
}

编辑:完全相同的代码在我的 Windows 机器上运行得非常好,并且使用相同版本的 lwjgl 和 JRE。

I'm trying to use LWJGL to get whether a key is pressed. If the escape key is pressed, then the application quits. However, I can't get it to read any keyboard input, although Display.isCloseRequested() works fine.

I'm on RHEL using LWJGL 2.6 and Java 1.6.

for(;;) {
    // check if we want to quit

    if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
        System.exit(0);  // can't get this to happen!
    }
    if(Display.isCloseRequested()) {
        System.exit(0);
    }

/* timer code omitted */

    render();
    Display.update();
}

Edit: The exact same code works perfectly fine on my Windows box, with the same versions of lwjgl and JRE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-10-06 04:21:38

也许您可以使用 isCreated 函数检查键盘是否已创建?

除此之外,我不太擅长编程,所以我无法为您提供任何其他输入。

试试这个

Keyboard.isCreated()

Maybe you can check if the Keyboard is Created with the isCreated function?

Other then that I'm not all that good in programming so I can't provide you with any other input.

try this

Keyboard.isCreated()
旧伤还要旧人安 2024-10-06 04:21:38

我可能会或可能不会帮助/恢复一个死掉的话题,但对于任何流氓谷歌人,我给你这个:

这是我的 Zdeva 引擎的输入类

给你,无需下载整个“引擎”..

package LWJGL;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

public class Input 
{
    public static boolean[] mouseButtons = {false, false, false};
    public static int[] mousePos = new int[Mouse.getButtonCount()];
    public static int[] keysBound = {Keyboard.KEY_A, Keyboard.KEY_B};

    /**
     * Initializes the input system. Loads keyconfig.
     * 
     */
    public static void init()
    {
        System.out.println("Initializing input system...");
        //Eventually will check for OS, and adjust keys accordingly.
        System.out.println("Input system initialized!");
    }

    /**
     * Updates all mouse info, keys bound, and performs actions.
     */
    public static void tick()
    {
        mouseButtons[0] = Mouse.isButtonDown(0);
        mouseButtons[1] = Mouse.isButtonDown(1);

        mousePos[0] = Mouse.getX();
        mousePos[1] = Mouse.getY();

        while(Keyboard.next())
        {
            if(Keyboard.getEventKeyState())
            {
                doAction(Keyboard.getEventKey(), false);
            }
        }

        for(int key : keysBound)
        {
            if(Keyboard.isKeyDown(key))
            {
                doAction(key, true);
            }
        }

        while(Mouse.next())
        {
            doAction(-1, false);
        }
        doAction(0, true);  
    }

    /**
     * Does the associated action for each key. Called automatically from tick.
     * @param key The key to check & perform associated action
     */
    public static void doAction(int key, boolean ifRepeat)
    {
        if(mouseButtons[0])
        {

        }
        if(mouseButtons[1])
        {

        }
        if(key == keysBound[0] & ifRepeat)
        {
            System.out.println("a");
        }
        if(key == keysBound[1] & !ifRepeat)
        {
            System.out.println("b");            
        }
    }
}

I may or may not be helpful/reviving a dead topic here, but for any rogue Googlers I give you this:

It's my Input class from my Zdeva Engine

Here you go, without having to download the entire 'engine'..

package LWJGL;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

public class Input 
{
    public static boolean[] mouseButtons = {false, false, false};
    public static int[] mousePos = new int[Mouse.getButtonCount()];
    public static int[] keysBound = {Keyboard.KEY_A, Keyboard.KEY_B};

    /**
     * Initializes the input system. Loads keyconfig.
     * 
     */
    public static void init()
    {
        System.out.println("Initializing input system...");
        //Eventually will check for OS, and adjust keys accordingly.
        System.out.println("Input system initialized!");
    }

    /**
     * Updates all mouse info, keys bound, and performs actions.
     */
    public static void tick()
    {
        mouseButtons[0] = Mouse.isButtonDown(0);
        mouseButtons[1] = Mouse.isButtonDown(1);

        mousePos[0] = Mouse.getX();
        mousePos[1] = Mouse.getY();

        while(Keyboard.next())
        {
            if(Keyboard.getEventKeyState())
            {
                doAction(Keyboard.getEventKey(), false);
            }
        }

        for(int key : keysBound)
        {
            if(Keyboard.isKeyDown(key))
            {
                doAction(key, true);
            }
        }

        while(Mouse.next())
        {
            doAction(-1, false);
        }
        doAction(0, true);  
    }

    /**
     * Does the associated action for each key. Called automatically from tick.
     * @param key The key to check & perform associated action
     */
    public static void doAction(int key, boolean ifRepeat)
    {
        if(mouseButtons[0])
        {

        }
        if(mouseButtons[1])
        {

        }
        if(key == keysBound[0] & ifRepeat)
        {
            System.out.println("a");
        }
        if(key == keysBound[1] & !ifRepeat)
        {
            System.out.println("b");            
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文