Problems with key event handling

Hey guys,

So I’ve been switching from polling to interrupting for my keyboard input handling and I’ve run into a quirk with KeyEvent. These are my keyPressed and keyReleased methods defined in my KeyListener:

public void keyPressed(KeyEvent e) {
		System.out.println(e.getKeyCode());
		KeyPressedEvent event = new KeyPressedEvent(e.getKeyCode(), Event.Type.KEY_PRESSED);
		Engine.onEvent(event);
	}

	public void keyReleased(KeyEvent e) {
		KeyReleasedEvent event = new KeyReleasedEvent(e.getKeyCode(), Event.Type.KEY_RELEASED);
		Engine.onEvent(event);
	}

I’ve coded up a rough event listener to do some preliminary testing with this implementation:

package breakit.mob.player;

import breakit.mob.Mob;
import engine.event.Event;
import engine.event.EventDispatcher;
import engine.event.EventListener;
import engine.event.keyboard.KeyPressedEvent;
import engine.event.keyboard.KeyReleasedEvent;

public class PlayerKeyListener implements EventListener {

	private Player player = null;
	
	public void init(Player player) {
		this.player = player;
	}
	
	@Override
	public void onEvent(Event event) {
		EventDispatcher dispatcher = new EventDispatcher(event);
		dispatcher.dispatch(Event.Type.KEY_PRESSED, (Event e) -> (onKeyPressed((KeyPressedEvent) e)));
		dispatcher.dispatch(Event.Type.KEY_RELEASED, (Event e) -> (onKeyReleased((KeyReleasedEvent) e)));
	}
	
	public boolean onKeyPressed(KeyPressedEvent e) {
		System.out.println(e.getKey());
		if(e.getKey() == 65) {
			player.setxdir(Mob.LEFT);
			player.setxspeed(3);
		}
		
		if(e.getKey() == 68) {
			player.setxdir(Mob.RIGHT);
			player.setxspeed(3);
		}
		return false;
	}
	
	public boolean onKeyReleased(KeyReleasedEvent e) {
		player.setxdir(Mob.NONE);
		player.setxspeed(0);
		return false;
	}
	
}

fyi 68 is ‘a’ and 65 is ‘d’, for left and right. It’s not finished, and I won’t use if statements or literals in the final implementation; I just drew up something quick and dirty for some testing.

Anyway, what PlayerKeyListener does is move the player left or right depending on whether a or d is pressed. The problem I’ve run into is that if both ‘a’ and ‘d’ are pressed simultaneously, meaning for example if I press ‘a’, hold it, then press ‘d’ while still holding down ‘a’, or vice versa, the player stops.

With this implementation, the only way that could possibly happen is if a KeyReleasedEvent is fired, even though I never released a key, but rather pressed an additional key while holding the original key down.

EDIT This was an inaccurate description of the bug. What is actually happening is that when the second key is pressed, while the first one is held down, the player changes direction, but when one of either of the keys is released, a KeyReleasedEvent is fired, causing the player to stop, even though one of the two buttons is still being held.

So I was wondering if anybody had an answer as to why this is happening. I thought somebody might have experienced this at some point and has figured it out.

I can provide more code if needed as there are a few custom classes. FYI, I learned if this from this tutorial:

The tutorial uses the mouse, I merely modified it to use the keyboard instead and ditched the LayerStack in favor of only one Listener at a time, as my game doesn’t require layers. It’s only about 45 minutes long if anybody wants to watch and get a better idea of what I’m implementing.

I probably won’t reply to any posts until tomorrow because right now I have to shift gears to other tasks.

Thank you for any help in debugging this!