1

我不知道如何让条件发挥作用。是否有类似 Keyboard.isKeyDown(//anykey) 的条件?

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

public class InputHandler {

    public static boolean currentKeyState, previousKeyState;

    public static void update() {
        previousKeyState = currentKeyState;

        if (//condition for keydown) {
            currentKeyState = true;
         } else {
            currentKeyState = false;
        }
    }

    public static boolean keyReleased() {
       if (currentKeyState == true && previousKeyState == false) {
            return true;
       } else {
            return false;
        }
   }
}

这是我想要完成的 C# 版本。Java中有没有类似于Keyboard.GetState()的方法?

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Game.Controls
{
    public class InputHandler
    {
    public KeyboardState currentKeyState;
    public KeyboardState previousKeyState;

    public InputHandler()
    {
        currentKeyState = new KeyboardState();
        previousKeyState = new KeyboardState();
    }

    public void Update()
    {
        previousKeyState = currentKeyState;
        currentKeyState = Keyboard.GetState();
    }

    public bool IsHeld(Keys key)
    {
        if (currentKeyState.IsKeyDown(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsReleased(Keys key)
    {
        if (currentKeyState.IsKeyUp(key) && previousKeyState.IsKeyDown(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsPressed(Keys key)
    {
        if (currentKeyState.IsKeyDown(key) && previousKeyState.IsKeyUp(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

}

4

4 回答 4

2

我发现这是检查事件驱动输入的最简单方法:

while (Keyboard.next()) {
    if (Keyboard.getEventKeyState()) {
        switch (Keyboard.getEventKey()) {
            // case Keyboard.KEY_UP: /** do something here **/ break;
        }
    }
}
于 2012-06-29T15:28:34.243 回答
2
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
    if (Keyboard.getEventKeyState()) {
        System.out.println("A Key Pressed");
    }
    else {
        System.out.println("A Key Released");
    }
}

你可以参考这个文件

用于获取所有输入法。

对于支持的所有键,请参阅http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html

于 2012-06-08T09:54:46.547 回答
0
import org.lwjgl.input.Keyboard;

public class InputHandler {

public static boolean currentKeyState = true, previousKeyState;

public static void update() {
    previousKeyState = currentKeyState;

    if (Keyboard.getEventKeyState()) {
        currentKeyState = true;
    } else {
        previousKeyState = false;
    }
}

public static boolean keyPressed() {
    if (currentKeyState == true && previousKeyState == true) {
        return true;
    } else {
        return false;
    }
}

}

于 2012-06-08T20:54:08.287 回答
-1

您可以为此使用 java KeyEvent 类

于 2012-06-08T09:40:48.027 回答