我不知道如何使条件正常工作。是否有类似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;
        }
    }
}


}

最佳答案

if (Keyboard.getEventKey() == Keyboard.KEY_A) {
    if (Keyboard.getEventKeyState()) {
        System.out.println("A Key Pressed");
    }
    else {
        System.out.println("A Key Released");
    }
}


您可以参考this document

获取所有输入法。

有关所有受支持的键,请参考http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html

关于java - 如何按一下键盘检查?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10946332/

10-10 21:57