Help with Keylistener

Hello Java Gaming! :slight_smile:

I have just started with my own game… At the moment the movement for my box looks like this


this.addMouseMotionListener(new MouseMotionListener()
		{
			
			public void mouseDragged(MouseEvent e)
			{
				x = e.getX();
				y = e.getY();
			}

			
			public void mouseMoved(MouseEvent arg0) {}
		});
		

And i want it to be controlled with my keyboard for smooth motion, but i cant seem to figure out how the Keylistener works!
So abit of help would be appriciated! :slight_smile:

Cheers ;D

It works exactly the same :o

Except for dragging and moved, you get pressed, released and typed.

Then all you need to do is decide what to do with depending on what key is triggered.


...
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("Space pressed!);
}
}
...

This will work, but very clumsily, since keyPressed will keep activating even when you hold down the key (Like when you press some key on notepad, letters will keep popping)

Refer to this thread: http://www.java-gaming.org/topics/how-would-you-know-if-the-key-was-being-held-for-ld23/26293/view.html

personally i would use jinput and polling.

JInput is overkill for what the OP wants.

What I often do is have booleans such as
boolean upKeyIsPressed = false;
boolean downKeyIsPressed = false;

Then on the keyPressed event I catch the key in the key event and set the appropriate boolean to true.
In keyReleased I set the appropriate boolean to false. That way the repeating keys have no effect.

Now, what I actually do is have a Singleton class called KeyAssignments. In that class I map any key which I want to watch to the object which must listen to it. So if I want this object to listen to the f4 key (for example) to show a debug screen I set it up like this.

KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_F4, this);

KeyAssignments is actually mapping a KeyEvent to a list of objects so I can make many things turn on a single key stroke if I want.

Then, every object which must response to a key event must implement KeyAssignable


public interface KeyAssignable {

	public void doKeyAction(int keyCode, boolean keyDown);
	
}

My main panel which catches the key events does something like this:


		addKeyListener( new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				int keyCode = e.getKeyCode();
				boolean keyDown = true;
				KeyAssignments.getInstance().doActionsForKey(keyCode, keyDown);
			}
			
			public void keyReleased(KeyEvent e){
				int keyCode = e.getKeyCode();
				boolean keyDown = false;
				KeyAssignments.getInstance().doActionsForKey(keyCode, keyDown);
				
			}
		});


If I have a controlable entity which must respond to ASWD keys to move up down right and left then mapping those keys is as easy as:


		KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_A, this);
		KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_S, this);
		KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_W, this);
		KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_D, this);

That may very well be overkill at this point. I think that the boolean values may fix your immediate problem of repeating key strokes. But this may be something to think about once you move forward.

Thanks alot for that thread! I solved it now thanks! By using delta x and delta y it moves the box smooth :slight_smile:

You solve it, now let’s see when you’ll come back asking about how to use same method on menu/option :wink: