I'm trying to use LWJGL to get whether a key is pressed. If the escape key is pressed, then the application quits. However, I can't get it to read any keyboard input, although Display.isCloseRequested()
works fine.
I'm on RHEL using LWJGL 2.6 and Java 1.6.
for(;;) {
// check if we want to quit
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
开发者_运维问答 System.exit(0); // can't get this to happen!
}
if(Display.isCloseRequested()) {
System.exit(0);
}
/* timer code omitted */
render();
Display.update();
}
Edit: The exact same code works perfectly fine on my Windows box, with the same versions of lwjgl and JRE.
Maybe you can check if the Keyboard is Created with the isCreated
function?
Other then that I'm not all that good in programming so I can't provide you with any other input.
try this
Keyboard.isCreated()
I may or may not be helpful/reviving a dead topic here, but for any rogue Googlers I give you this:
It's my Input class from my Zdeva Engine
Here you go, without having to download the entire 'engine'..
package LWJGL;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
public class Input
{
public static boolean[] mouseButtons = {false, false, false};
public static int[] mousePos = new int[Mouse.getButtonCount()];
public static int[] keysBound = {Keyboard.KEY_A, Keyboard.KEY_B};
/**
* Initializes the input system. Loads keyconfig.
*
*/
public static void init()
{
System.out.println("Initializing input system...");
//Eventually will check for OS, and adjust keys accordingly.
System.out.println("Input system initialized!");
}
/**
* 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())
{
if(Keyboard.getEventKeyState())
{
doAction(Keyboard.getEventKey(), false);
}
}
for(int key : keysBound)
{
if(Keyboard.isKeyDown(key))
{
doAction(key, true);
}
}
while(Mouse.next())
{
doAction(-1, false);
}
doAction(0, true);
}
/**
* 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 ifRepeat)
{
if(mouseButtons[0])
{
}
if(mouseButtons[1])
{
}
if(key == keysBound[0] & ifRepeat)
{
System.out.println("a");
}
if(key == keysBound[1] & !ifRepeat)
{
System.out.println("b");
}
}
}
精彩评论