当前位置: 首页 > 知识库问答 >
问题:

LWJGL的单点击和按住问题

莘钧
2023-03-14
    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");            
    }
}

共有1个答案

谢学名
2023-03-14

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

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

/*
 * 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");
 类似资料:
  • 所以最基本的,我想做的是使用LWJGL让我的球员在比赛中移动。玩家当前正在移动,但当你按住按钮时,他没有继续移动。 更新的代码: 我的代码仍然有同样的问题,我开始认为是Keyboard.next()阻止了我按住按钮,而播放器仍然在移动。

  • 一直试图点击网站上的单选按钮,但无济于事。 一直试图点击单选按钮和标签,但硒一直抛出没有这样的元素错误,我在这个阶段有点沮丧。 在实际网站上可能更容易看到: https://www.theaa.ie/car-insurance/journey/getting-started 它在输入电子邮件后的页面上。试图让一些测试用例运行,但这些单选按钮不想被点击。

  • 在我的应用程序中,我有一个按钮。单击和双击按钮后,将执行单独的操作。我该怎么做?谢谢

  • 单击并按住向上/向下箭头按钮时,微调器更新的速度很慢。有没有办法提高更换速度? 当我用鼠标单击、单击、单击时,旋转器值的变化和我单击一样快。如果我每次按下键盘上的上/下箭头,或者如果我按住上/下箭头键,它也会快速变化。我希望当我单击并按住箭头按钮时,值能快速更改。 有人知道怎么做吗?

  • 所以我一直试图让与谷歌Signin作为我的应用程序的Signin选项。当我点击sigin按钮时,应用程序会显示availabe电子邮件列表,但只要我点击任何电子邮件,应用程序就不会让我登录。我尝试了几乎所有的方法,包括在firebase中添加SHA-1键,替换新的google-services.json文件。我已经重新检查了我的代码,但我一定漏掉了什么。请帮我找出我的错误

  • 这是关于Selenium和Click的另一个问题。我已经挣扎了大约两天,无法让它工作-我已经在互联网上尝试了答案,现在我需要一个共同的努力。提前谢谢!! 我在以下网站上工作http://144.76.109.38/peTEST-如果您想回顾我的步骤,这可能会有所帮助。 我正在尝试填写登录表单,然后点击登录,看到答案页面。 这是我的代码: 所以基本上我打开页面,截图输入用户名和密码,点击登录,然后截