LWJGL Keyboard, what's going on?

Hello!
Quick question! I have player input setup like this:


boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_UP)
...
if(keyUp){
    //move code
}

And a bit later:


while(Mouse.next()){
	if(Mouse.isButtonDown(0)){
		//fire button (boom boom)
	}
}

So what happens is, if player holds UP key to run and presses mouse button to fire, movement stops. In order to run again, player must press up key again, running while shooting is impossible.
Why do keys get reset that way?

It’s todo with the way how when you call Mouse.next() or Keyboard.next() it polls the event so it cannot be used again that tick.

The way i Would solve that particular issue is to create a seperate input class like this:


public class InputHandler {
	
	public boolean keysStream[] = new boolean[65536];
	
	public void tickInput(){
		while(Keyboard.next()){
			if(Keyboard.getEventKeyState()){
				keysStream[Keyboard.getEventKey()]=true;
			}else{
				keysStream[Keyboard.getEventKey()]=false;
			}
		}
		while(Mouse.next()){
                       //code here
                }

	}
}

Ah I see. Gonna implement it straight away. Thanks!

new boolean[65536];

This basically allocates 64 kilobytes of memory. I can’t help but feel like it’s wasteful.

So I could just use the keys I need.

Yes you can, I only put that as that will be able to get button presses for all keyboards, e.g chinese and swedish ones as the different characters have different codes

There’s probably some great container for these kinds of things, but I have no idea.

I think you’re looking for this class.

First person who complains about the overhead is going to get smacked.

I’ll preemptively smack you for the broken link.

Why is CPU overhead (Set + autoboxed Integer instances) better than memory overhead (boolean[])?

Let him use his boolean[], and trade off memory for performance, or convenience for that matter.

And why do we have this discussion again? :clue:

I wish they would have included variations of the collections classes that are tailored towards the primitives as well. Or just not failed so badly with generics.

I fixed the link. Smack SMF for freaking out when you put the URL in quotes. Freakin BBCode.

Got an actual performance issue with a Set, do we? If you really need primitive collections, try Trove. Beyond that, screw it, I too am tired of having this argument.

No please do have this argument, it sounds quite educational.

Grabs popcorn