I came across a few interesting problems with a few other people about key events and how to code this functionality with games smoothly and effectively. Im sure there are many ways of being able to process these events but i wrote a class and an interface that helps and works great.

public interface Event {
public void perform();

public class Key {

private boolean isPressed;
private int keyCode;
private Event event;

public Key(int keyCode, Event event){
this.keyCode = keyCode;
isPressed = false;
this.event = event;

public int keyCode(){
return keyCode;

public boolean isPressed() {
return isPressed;

public boolean pressed(boolean isPressed) {
return this.isPressed = isPressed;

public void perform(KeyEvent e){
if (e.getKeyCode() == keyCode)

Declaration :

ctrl = new Key(KeyEvent.VK_CONTROL, new Event(){

public void perform() {
if (ctrl.isPressed())


public class KeyListener extends KeyAdapter {

public void keyPressed(KeyEvent e) {



public void keyReleased(KeyEvent e){


Of course the only reason why you would have all this is if you have multiple keys being pressed at the same time, say a user is holding down the space key and the shift key and the letter A and the Enter key. This type of coding will help keep track of which key is being held down or not.
I have other code that works just as well, but when i write my code i like to write it as if anyone can re-use it for any case or solution.