LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: jacekcichy on November 07, 2010, 13:33:34

Title: Keyboard input (question)
Post by: jacekcichy on November 07, 2010, 13:33:34
Hi!

I'm using jinput and I would like to ask is it possible to check isKeyPressed instead of isKeyDown? If yes I will be really gratefull for code sample.

Thanks for help ;)
Title: Re: Keyboard input (question)
Post by: jacekcichy on November 07, 2010, 14:02:45
Ok I wrote a simple code that works but I'm still not sure is it absolutelly good.

import org.lwjgl.input.Keyboard;

public class Klawiatura {
static boolean tab[] = {false,false};
static int licznik = 0;

public static void isKeyPressed(int key){
Keyboard.next();
if (key == Keyboard.getEventKey()){
if (licznik == 0){
tab[0] = Keyboard.getEventKeyState();
licznik++;
} else {
tab[1] = Keyboard.getEventKeyState();
licznik = 0;
}

if (tab[0] && !tab[1]){
System.out.println("zdarzenie wykryto");
}
}
}

}

Title: Re: Keyboard input (question)
Post by: Fool Running on November 09, 2010, 13:57:58
Color me confused, but what is the difference between a key being pressed and a key being down?

EDIT: Finally was able to read the code. :P So basically you want to know when a key was down and then let go? The Keyboard.getEventKeyState() tells whether the event was for a key down or a key up. If the event was for a key up, then it must have been down (thus you only have to look for the false values). Also, you need to realize that Keyboard.next() will move to the next keyboard event so you can't do (pseudo code):
if (isKeyPressed(KEY_UP)){
   // Do something
}else if (isKeyPressed(KEY_DOWN)){
   // Do something else
}

If the first event was for KEY_DOWN, then you would miss the message because you do Keyboard.next() in each call.
Title: Re: Keyboard input (question)
Post by: kappa on November 09, 2010, 14:00:02
Quote from: Fool Running on November 09, 2010, 13:57:58
Color me confused, but what is the difference between a key being pressed and a key being down?

key pressed = key pressed down and then released
key down = key is down
Title: Re: Keyboard input (question)
Post by: Fool Running on November 09, 2010, 14:04:29
I realized that right after I posted.  :-\
Title: Re: Keyboard input (question)
Post by: jacekcichy on November 18, 2010, 18:02:22
Hi!

Thanks for explanation but I still don't understand something. I have for example gameloop code like this:

if (Keyboard.isKeyDown(Keyboard.KEY_R)) {    
}
       
System.out.println(Keyboard.getEventKey());


When I 'm pressing R key I have printed only "0". So my question is why it didn't print "R key" index? When I use Keyboard.next() it prints it but sometimes I missed some events.
Title: Re: Keyboard input (question)
Post by: Fool Running on November 18, 2010, 19:59:19
The way to use the Keyboard with events is to do the following (kinda pseudo-code):
while (Keyboard.next()){
    int key = Keyboard.getEventKey();
    boolean keyDown = Keyboard.getEventKeyState();
    if (keyDown){
        if (key == LEFT)
            // Do something
        else if (key == RIGHT)
            // Do something else
    }else{ // was released
        if (key == LEFT)
            // Do something
        else if (key == RIGHT)
            // Do something else
    }
}

Hope that helps ;D
Title: Re: Keyboard input (question)
Post by: jacekcichy on November 20, 2010, 14:36:44
Thanks a lot!! It works pretty good.  ;D