1

I can't figure how to get the condition to work. Is there something like Keyboard.isKeyDown(//anykey) for the condition?

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;
        }
   }
}

Here's the C# version of what I'm trying to accomplish. Is there a method similar to Keyboard.GetState() in Java?

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;
        }
    }
}

}

KeyHeart
  • 163
  • 2
  • 6
  • 17
  • No idea about the answer to this one, but I still think I have something to offer! Your C# `IsPressed()` method can be written just as `return (currentKeyState.IsKeyDown(key) && previousKeyState.IsKeyUp(key));`. Same with all the other methods, in Java, too. Saves a lot of typing and space ;) – Petr Janeček Jun 08 '12 at 21:09

4 Answers4

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

You can refer this document

for getting all the input methods.

For all the keys supported refer http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html

AurA
  • 12,135
  • 7
  • 46
  • 63
2

I find this to be the easiest method of checking for event-driven input:

while (Keyboard.next()) {
    if (Keyboard.getEventKeyState()) {
        switch (Keyboard.getEventKey()) {
            // case Keyboard.KEY_UP: /** do something here **/ break;
        }
    }
}
James Webster
  • 31,873
  • 11
  • 70
  • 114
Oskar
  • 1,321
  • 9
  • 19
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;
    }
}

}

KeyHeart
  • 163
  • 2
  • 6
  • 17
-1

You can use java KeyEvent Class for this

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
  • he is asking for LWJGL... where I doubt java KeyEvent will have a key role to play... they must be having their own api better suited for their needs(gaming). – AurA Jun 08 '12 at 09:42