Problem Handling Multiple Keyboard Events

I am currently developing a small-scale game, in which I have recently discovered a major problem regarding keyboard events. In my game, I have a player (represented by an image), which can be moved through the arrow keys. However, when a single arrow key is held down, and then the user presses and holds another key, the new keyboard event replaces the old one. I want the old one to be processed with the new one, allowing the player to move diagonally when applicable. The only way for both to be processed simultaneously is for the user to press the two keys at the same time (within milliseconds). I am currently implementing KeyListener in my primary class and using KeyEvent and keyPressed. Here is a simplified outline, in which I have edited out unimportant features:

publicclass KeyTestimplements KeyListenerextends JPanel{

boolean gameOver =false;

JFrame app;

publicstaticvoid main (String args[]){

KeyTest keyTest =new KeyTest();

keyTest.app =new JFrame();

keyTest.app =new JFrame();

keyTest.app.addKeyListener (keyTest);

while (!keyTest.gameOver){

keyTest.displayGUI();

}

}

publicvoid keyPressed (KeyEvent ke){

int intKey = ke.getKeyCode();

if (intKey == KeyEvent.VK_UP){

playerX -= playerSpeed;

}

if (intKey == KeyEvent.VK_DOWN){

playerX += playerSpeed;

}

if (intKey == KeyEvent.VK_LEFT){

playerY -= playerSpeed;

}

if (intKey == KeyEvent.VK_RIGHT){

playerY += playerSpeed;

}

}

publicvoid displayGUI(){

}

}

Any help would be appreciated.

[2858 byte] By [DMTNTJGDa] at [2007-10-2 11:55:53]
# 1

Use keyPressed to store the key in a Collection and then take it out in the keyReleased method. Use another method to look at the currently pressed keys for key combinations, or process them one at a time (possibly in another Collection when combinations don't matter).

Hope this helps you.

patrickmallettea at 2007-7-13 8:08:08 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks for your reply. At first I made a queue, but found that to be ineffective, as it couldn't distinguish which key was released. I then wrote my own collection class to map out the keys and assign them boolean values. Everything works fine now, so thank you for all your help!
DMTNTJGDa at 2007-7-13 8:08:08 > top of Java-index,Other Topics,Java Game Development...