java - LWJGL 单击和按住的问题

标签 java keyboard lwjgl

我一直在研究一种在我的输入系统中使用 LWJGL 的方法。我在检测是单击还是按住时遇到问题。当我点击时,该事件会触发两次,而不是一次。

    while(Keyboard.next())
    {
        if(Keyboard.getEventKeyState())
        {
            if(Keyboard.isRepeatEvent())
            {
                //Key held.
                doAction(Keyboard.getEventKey(), true, false);
            }
            else
            {
                //Key pressed
                doAction(Keyboard.getEventKey(), false, false);
            }
        }
        else
        {
            //Fired when key is released.
            doAction(Keyboard.getEventKey(), false, true);
        }
    }

编辑:我已经解决了该问题并对此进行了修改。给你,修改版。 (该死,Teamviewer..)

/**
 * 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())
    {
        doAction(0, false);
        if(Keyboard.getEventKeyState())
        {
            if(!Keyboard.isRepeatEvent())
            {
                doAction(Keyboard.getEventKey(), false);
            }
        }
        else
        {
            doAction(Keyboard.getEventKey(), true);
        }
    }

    while(Mouse.next())
    {
    }
}

/**
 * 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 ifReleased)
{
    if(mouseButtons[0])
    {

    }
    if(mouseButtons[1])
    {

    }
    if(key == 2 & !ifReleased)
    {
        System.out.println("a");
    }
    if(Keyboard.isKeyDown(3))
    {
        System.out.println("b");            
    }
}

最佳答案

我知道这个问题被提出已经有一段时间了,但我自己想出了一个解决方案。我的 InputHelper 允许您确定是否按下、释放或按住某个键或鼠标按钮,并且可以从任何其他类访问它,而无需初始化和共享它的同一实例。

它的工作原理是有 2 个数组,1 个用于鼠标事件,1 个用于键盘事件,每个数组为每个键存储一个枚举值。如果存在按钮或按键事件,则更新时,更新函数会将该按钮/按键的相应数组中的值设置为某个枚举。然后,下次更新时,它将所有按键和按钮事件设置为无事件,并重复该过程,处理任何新事件。

/*
 * Handles mouse and keyboard input and stores values for keys
 * down, released, or pressed, that can be accessed from anywhere.
 * 
 * To update the input helper, add this line into the main draw loop:
 *  InputHelper.update();
 * 
 * Use as so (can be used from anywhere):
 *  InputHelper.isKeyDown(Keyboard.KEY_SPACE);
 */

import java.util.ArrayList;
import org.lwjgl.input.*;

/**
 *
 * @author Jocopa3
 */
public class InputHelper {
    private static InputHelper input = new InputHelper(); //Singleton class instance

    private enum EventState {
        NONE,PRESSED,DOWN,RELEASED; 
    }

    private ArrayList<EventState> mouseEvents;
    private ArrayList<EventState> keyboardEvents;

    public InputHelper(){
        //Mouse initialization
        mouseEvents = new ArrayList<EventState>();
        //Add mouse events to Array list
        for(int i = 0; i < Mouse.getButtonCount(); i++) {
            mouseEvents.add(EventState.NONE);
        }

        //Keyboard initialization
        keyboardEvents = new ArrayList<EventState>();
        //Add keyboard events to Array list
        for(int i = 0; i < Keyboard.KEYBOARD_SIZE; i++) {
            keyboardEvents.add(EventState.NONE);
        }
    }

    private void Update(){
        resetKeys(); //clear Keyboard events
        //Set Key down events (more accurate than using repeat-event method)
        for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++){
            if(Keyboard.isKeyDown(i))
                keyboardEvents.set(i, EventState.DOWN);
        }
        while(Keyboard.next()){ //Handle all Keyboard events
            int key = Keyboard.getEventKey();
            if(key<0) continue; //Ignore no events

            if(Keyboard.getEventKeyState()){
                if(!Keyboard.isRepeatEvent()){
                    keyboardEvents.set(key, EventState.PRESSED);
                }
            }else{
                keyboardEvents.set(key, EventState.RELEASED);
            }
        }


        resetMouse(); //clear Mouse events
        //Set Mouse down events
        for(int i = 0; i < Mouse.getButtonCount(); i++){
            if(Mouse.isButtonDown(i))
                mouseEvents.set(i, EventState.DOWN);
        }
        while (Mouse.next()){ //Handle all Mouse events
            int button = Mouse.getEventButton();
            if(button<0) continue; //Ignore no events
            if (Mouse.getEventButtonState()) {
                mouseEvents.set(button, EventState.PRESSED);
            }else {
                mouseEvents.set(button, EventState.RELEASED);
            }
        }
    }

    //Set all Keyboard events to false
    private void resetKeys(){
        for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++) {
            keyboardEvents.set(i, EventState.NONE);
        }
    }

    //Set all Mouse events to false
    private void resetMouse(){
        for(int i = 0; i < Mouse.getButtonCount(); i++) {
            mouseEvents.set(i, EventState.NONE);
        }
    }

    //Non-static version of methods (Only used in the singleton instance)
    private boolean KeyDown(int key){
        return keyboardEvents.get(key)==EventState.DOWN;
    }
    private boolean KeyPressed(int key){
        return keyboardEvents.get(key)==EventState.PRESSED;
    }
    private boolean KeyReleased(int key){
        return keyboardEvents.get(key)==EventState.RELEASED;
    }
    private boolean MouseButtonDown(int key){
        return mouseEvents.get(key)==EventState.DOWN;
    }
    private boolean MouseButtonPressed(int key){
        return mouseEvents.get(key)==EventState.PRESSED;
    }
    private boolean MouseButtonReleased(int key){
        return mouseEvents.get(key)==EventState.RELEASED;
    }

    //Static version of methods (called from anywhere, return singleton instance value)
    public static boolean isKeyDown(int key){
        return input.KeyDown(key);
    }
    public static boolean isKeyPressed(int key){
        return input.KeyPressed(key);
    }
    public static boolean isKeyReleased(int key){
        return input.KeyReleased(key);
    }
    public static boolean isButtonDown(int key){
        return input.MouseButtonDown(key);
    }
    public static boolean isButtonPressed(int key){
        return input.MouseButtonPressed(key);
    }
    public static boolean isButtonReleased(int key){
        return input.MouseButtonReleased(key);
    }
    public static void update(){
        input.Update();
    }
}

它必须手动更新每一帧,因此您的主绘制循环应该添加行 InputHelper.update(); ,如下所示:

while(!Display.isCloseRequested()) {
    InputHelper.update(); //Should go before other code that uses the inputs

    //Rest of code here
}

一旦将其设置为更新每一帧,您就可以在任何需要确定鼠标或按键按钮的输入状态的地方使用它,如下所示:

//Mouse test    
if(InputHelper.isButtonPressed(0))
    System.out.println("Left Mouse button pressed");
if(InputHelper.isButtonDown(0))
    System.out.println("Left Mouse button down");
if(InputHelper.isButtonReleased(0))
    System.out.println("Left Mouse button released");

//Keyboard Test
if(InputHelper.isKeyPressed(Keyboard.KEY_SPACE))
    System.out.println("Space key pressed");
if(InputHelper.isKeyDown(Keyboard.KEY_SPACE))
    System.out.println("Space key down");
if(InputHelper.isKeyReleased(Keyboard.KEY_SPACE))
    System.out.println("Space key released");

关于java - LWJGL 单击和按住的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053847/

相关文章:

c++ - 自定义小部件中的 Qt 和死键

android - 当显示键盘时,我的 Android AlertDialog 没有向上移动

java - Applet 应用程序因安全原因被阻止

java - glOrtho 参数对应什么?

java - 错误 : Could not find or load main class

创建依赖类时,JavaCompiler 给出错误

java - 当目录名称中存在句点时使用 Java.exe 运行

wpf - 在 WPF 中,如何复制旧的 WinForms OnKeyPressed 功能?

java - LWJGL使用 "java <class file>"命令时找不到类错误

java - 使用 GSON 将 JSON 文件中的数据解析为 Java 对象