Detect when key is depressed?

Thanks! this really helped clean up my code and would have saved me a lot of time when i originally wrote this. thanks again


public boolean[] key = new boolean[68836];

	public void keyPressed(KeyEvent k) { 
		int keyCode = k.getKeyCode();
		if (keyCode > 0 && keyCode < key.length) {
			key[keyCode] = true;
		}
	}

	public void keyReleased(KeyEvent k) { 
		int keyCode = k.getKeyCode();
		if (keyCode > 0 && keyCode < key.length) {
			key[keyCode] = false;
		}
	}

Handles multiple key presses/releases @ once.

68836? Shouldn’t that be 65536?

Yep.

KDE/Linux is awesome.

Sad key is sad :frowning:

An array of 65536 booleans is not likely to be packed, so this is ridiculously wasteful. Seriously, use a Set. It’s not going to impact performance unless your players are in the habit of holding down thousands of keys at a time.

Premature optimisation. Period.

It works, is simple, doesn’t have noticable overhead (both in CPU time and RAM usage).
There is no need to improve on it. One should work on the game instead.

In LWJGL, the code is this:
http://pastebin.java-gaming.org/2c27562962f
Hope this helps. ;D
(I took this from the Ninja Cave tutorials here:http://http://ninjacave.com/lwjglbasics2)

Personally I find coding standards more important in this situation. My rule of thumb: take the extra 5 to 10 minutes to solve the problem properly, rather than creating some brutish and wasteful hack. This is especially important for beginners; if they are never told to think about concepts like memory management, garbage collection, or what have you, they will inevitably end up with horribly inefficient game code.

Nothing worse than collaborating with a Java programmer who doesn’t bother keeping his code clean and optimized. Eventually, shitty code will catch up to you.