Keyboard control freaks out

Started by Perforated, January 10, 2007, 11:40:40

Previous topic - Next topic

Perforated

Hello,
I've just started looking at LWJGL and I must say that I'm impressed.
I've been going through the very basics of coding and the turn has come to adding keyboard input to the application.
All is well until I start the application, when I push a key (one that I've mapped) it registers more than one time, in the
hundreds actually, producing some hellishly flickering graphics, not to mention it becoming completely unusable.

Here's the relevant code (I think):

Key polling:
        public void poll()
	{
		Keyboard.poll();

		if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
		{
			isRunning = false;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
		{
			player.setX( player.getX() - 0.5f );
		}
		else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
		{
			player.setX(player.getX() + 0.5f);
		}
		else if (Keyboard.isKeyDown( Keyboard.KEY_UP))
		{
			player.setY(player.getY() - 1);
		}
		else if (Keyboard.isKeyDown( Keyboard.KEY_DOWN ))
		{
			player.setY(player.getY() + 1);
		}
	}



Main loop:
        public void loop()
	{
		try 
		{
			Display.create();
			Keyboard.create();
			
			isRunning = true;
			
			while (isRunning) 
			{
				poll();
				render();
				Display.update();
			}
			
			cleanup();
		} 
		catch (LWJGLException e) 
		{
			e.printStackTrace();
		}
	}


I hope you can help me because I'm lost.

kappa

you do not have to do Keyboard.create() and Keyboard.poll() with the code below as it is done automatically when you do Display.create() and Display.update().

Perforated

Quote from: javalwjgl on January 10, 2007, 13:18:03
you do not have to do Keyboard.create() and Keyboard.poll() with the code below as it is done automatically when you do Display.create() and Display.update().

Just tried it and that much is true, it doesn't solve my problem though. I still generate an absurd amount of keypresses when I should generate only one.

Matzon

isKeyDown tests whether the key is down when you ask - so if you render at 1000 FPS, you will get isKeyDown == true 1000 times a second.

if you only want to check "once" - then use the event mechanism

kappa

yup using key events is one way or if want to use isKeyDown() then you could also have a boolean value that triggers once the key is pressed so it only registers it once.

boolean spacePressed = false;

...


if (!spacePressed && Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
	System.out.println("SPACE KEY PRESSED);
        spacePressed = true;
}
else {
       spacePressed = false;
}

Perforated

Quote from: Matzon on January 10, 2007, 14:33:10
isKeyDown tests whether the key is down when you ask - so if you render at 1000 FPS, you will get isKeyDown == true 1000 times a second.

if you only want to check "once" - then use the event mechanism

Ah, I see now, well, I've managed to figure out how to check just once (thanks). Now I'm more interested in getting more fluid motion, i.e animation of my object. Now I know it has got to do with the framerate, I tried to use the methods sync to no avail, is there any other way?

kappa

you should calculate a delta value using something like Sys.getTime() or System.getTimeMillis();

have a look at something like the space invaders example included with lwjgl on a good way to calculate delta.

long delta = Sys.getTime() - lastLoopTime;
lastLoopTime = Sys.getTime();