Keyboard input (question)

Started by jacekcichy, November 07, 2010, 13:33:34

Previous topic - Next topic

jacekcichy

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

jacekcichy

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


Fool Running

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.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

kappa

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

Fool Running

I realized that right after I posted.  :-\
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

jacekcichy

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.

Fool Running

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
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

jacekcichy

Thanks a lot!! It works pretty good.  ;D