Detect when key is depressed?

I know how to detect if a key is pressed using

input.isKeyDown(Input.KEY_UP)

but how would i tell if the key is depressed? I couldnt figure out how to do this sucessfully.
using this code doesnt work:( vvv

(!(input.isKeyDown(Input.KEY_UP))

any ideas are welcome!

Use the keyReleased(KeyEvent e) that’s defined in the KeyListener interface. :slight_smile:

what are the paramaters to that?

This is my very basic key controller for my map editor. So yeah, this is how I handle key pressed and key released stuff.

package controls;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import model.MapEditor;

public class KeyController extends KeyAdapter {

	private MapEditor me;

	public KeyController(MapEditor mt) {
		this.me = mt;
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_UP:
			this.me.key_up = true;
			break;
		case KeyEvent.VK_DOWN:
			this.me.key_down = true;
			break;
		case KeyEvent.VK_LEFT:
			this.me.key_left = true;
			break;
		case KeyEvent.VK_RIGHT:
			this.me.key_right = true;
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_UP:
			this.me.key_up = false;
			break;
		case KeyEvent.VK_DOWN:
			this.me.key_down = false;
			break;
		case KeyEvent.VK_LEFT:
			this.me.key_left = false;
			break;
		case KeyEvent.VK_RIGHT:
			this.me.key_right = false;
			break;
		}
	}
}

Hope it helps :slight_smile:

thanks for that example but do you know what the parameters are for that? because i do not completely understand how to implement this

Have you seen this tutorial?

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Also, it would be helpful to know more about your context. I don’t recognize

input.isKeyDown(Input.KEY_UP)

The KeyListener and KeyEvent are part of AWT. Are you using a library? Regenuluz’s example is core Java. If you are using core Java, you have to use Event Listeners.

http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html

But if it is part of a library, let us know which and there will be someone to show you the way, I’m pretty sure.

I am using slick2d and lwjgl i am not sure of what code that is a part of. Do you know how i could detect when a key is depressed?

I’ve worked with Slick2D quite a bit.

If a key is depressed, isKeyDown() will always be true. If you want to check to see if a key is depressed, then just poll that function.

In Slick this would be done by overriding keyReleased in BasicGame or your BasicGameState.

This is covered in the wiki and in various tests, such as InputTest. Here are some more links I’d suggest browing through:

Main Site
New Wiki (old wiki)
JavaDoc
Tests
Game Examples (SlickVirium, SlickPuzzle, etc)

what does it mean to pole a function?

I have often seen @ Override being used but what does it actually do?

I understand what you are asking for but the answer you will get to your problem is probably more complicated than you want it to be. You could store when they key is being held down and the first instance of it not being held down you would do whatever action you needed to do for when the key is released.

Instead, try to see if this following line will work for your needs.

input.isKeyPressed(Input.KEY_UP)

Edit: Instead of saying “depressed” say released.

Thanks guys for all the help but i figure out how to use it using this code:

		if (quit == false) {
			if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
				up = true;
				right = false;
				left = false;
				down = false;
				movingUp = true;
				movingLeft = false;
				movingRight = false;
				movingDown = false;
				buckyPositionY += delta * .1f; 
				if (buckyPositionY > -1059) {
					buckyPositionY -= delta * .1f;
				}
			}
			if (!(input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W))){
				upAnimation = spriteUp;
			}

You were actually asking for when the key was not down in that case.

The way you have your code set up could be improved. You check for a condition to be true and then you check if the same condition is false. Instead you could check if the condition once and based off of that perform an action.

Instead of two if statements, you can use an if else statement.

			if (quit == false) {
				if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
					up = true;
					right = false;
					left = false;
					down = false;
					movingUp = true;
					movingLeft = false;
					movingRight = false;
					movingDown = false;
					buckyPositionY += delta * .1f;
					if (buckyPositionY > -1059) {
						buckyPositionY -= delta * .1f;
					}
				} else {
					upAnimation = spriteUp;
				}
			}

See this code which uses core java.

The one on the left side seems depressed to me…

http://data.whicdn.com/images/8793829/tumblr_ljmjz0GDlU1qiif3no1_500_thumb.jpg

The one on the bottom looks more depressed.

That’s summed up pretty neatly here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Override.html

It’s not really something that you strictly need to use, I use it because I think it’s easier to figure out why and where functions are coming from when I see that. (And I’ve set Eclipse to bitch at me, if it’s missing. But then I’ve also set Eclipse to add it whenever I save. :P)

I like the setting to make it bitch at me, but I prefer it not to be included automatically so I can know if I accidentally use the same method name of something important.

I usually spot that before I save, or right after I’ve saved. :slight_smile: But yeah, I guess that could cause some trouble, if it isn’t spotted. :stuck_out_tongue: