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.