Advertisement

JOGL KeyListener isn't working correctly

Started by November 27, 2018 06:35 PM
5 comments, last by Rutin 6 years ago

I'm totally new to Game Dev. and i wanna say "its not difficult", but sometimes i get stuck in tiny holes with nothing to dig my way out.

Basically, I've been following this Java OpenGL (using JOGL) 2D series on YouTube for a while, as an attempt at my first game in java. But clearly its not going well.

I followed the series up till episode 18, but after that; in episode 19, the tutor had implemented a KeyListener in order to move the player around.

I did the same thing he did, but when i hold down the up/down/right or left key, the player moves for a bit but stops after like 2 seconds. For it to move again, i have to release the key and hold it down again.

I personally think this is a problem with JOGL (the library I'm using). But would like to have a solution to the problem since I have already gone through the trouble of making an entire game engine.

 

Anyways, here's the link to the video:
Java OpenGL 2D Game Tutorial - Episode 19 - Player Input

 

The code i used for player input is exactly similar to the one used by the tutor!

Thanks...

(*__*) Really? (-_-)

Post the code that you have in your project relevant to the issue. Nobody is going to go hunting for what 'might' be the code from this tutorial.

Programmer and 3D Artist

Advertisement

Lol, sorry. forgot that part..

Here's the KeyInput class that i made.


public class KeyInput implements KeyListener{

	private static boolean keys[] = new boolean[256];
	
	
	public void keyPressed(KeyEvent e) {
		keys[e.getKeyCode()] = true;
	}

	public void keyReleased(KeyEvent e) {
		keys[e.getKeyCode()] = false;
	}
	
	public static boolean getKey(short keyCode) {
		return keys[keyCode];
	}
	
}

 

 

And this is how its used in the PlayerObject class...

The following code is executed every time an update takes place (20 times a second)


float xInput = 0;
float yInput = 0;
		
		// Upwards
		if(KeyInput.getKey(KeyEvent.VK_W)) {
			yInput++;
		}
		
		// Left 
		if(KeyInput.getKey(KeyEvent.VK_A)) {
			xInput--;
		}
		
		// Back
		if(KeyInput.getKey(KeyEvent.VK_S)) {
			yInput--;
		}
		
		// Right
		if(KeyInput.getKey(KeyEvent.VK_D)) {
			xInput++;
		}
		
		x += xInput * (GameLoop.getDelta()); //x here is the position of the player, and is a variable of the player Object
		y += yInput * (GameLoop.getDelta()); //same thing for the y
		

 

(*__*) Really? (-_-)

I cannot see anything off the bat that would seem out of place. Maybe I'm missing something. There is a known issue of having key released called for repeating keys.

I did find this example, maybe it can help:

http://jogamp.org/git/?p=jogl.git;a=blob;f=src/test/com/jogamp/opengl/test/junit/newt/TestNewtKeyPressReleaseUnmaskRepeatAWT.java;h=c275139058b3b1ce5eb3e5aa111ee2ed75053e01;hb=HEAD

Can you also try to change the keys to only the arrow keys (UP DOWN LEFT RIGHT) and let me know if the same problem happens.

If you're looking at movement keys, I would also suggest using polling, and not events to do this.

Let me know if the arrow keys make a difference.

Also:

https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/event/KeyEvent.html

Look up InputEvent.AUTOREPEAT_MASK

Maybe the auto-repeating is causing the release calls to trigger for your AWSD keys.

Programmer and 3D Artist

Ok! So first of all! Thank you so much for responding!

 

I was also looking around forums and stuff, hopping to find some answers, but seems like i missed the AUTOREPEAT_MASK thingy.

What I think it was trying to do is, making it so that after a certain amount of key types, it would only accept a key every second and so. kinda like when you hold a key down in a text editor or notepad.

 

Quote

Can you also try to change the keys to only the arrow keys (UP DOWN LEFT RIGHT) and let me know if the same problem happens.

I tried changing the keys to the arrow keys (up, down, etc).. but the problem still persisted.

However! Thanks to you! I checked the code in this link out: Unmask Repeat AWT

And changed my KeyInput class to this:


public class KeyInput implements KeyListener{

	private static boolean keys[] = new boolean[256];
	
	
	public void keyPressed(KeyEvent e) {
		if ((InputEvent.AUTOREPEAT_MASK & e.getModifiers()) == 0) {
			keys[e.getKeyCode()] = true;
		}
	}

	public void keyReleased(KeyEvent e) {
		if ((InputEvent.AUTOREPEAT_MASK & e.getModifiers()) == 0) {
			keys[e.getKeyCode()] = false;
		}
	}
	
	public static boolean getKey(short keyCode) {
		return keys[keyCode];
	}
	
}

... IT WORKED ... ?

And i gotta say, that was one hell of a simple modification!

Again, you saved me from mental chaos!

Thanks again!

(*__*) Really? (-_-)

No problem! :) Glad you're up and running!

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement