KeyListener

Hey guys!

I am using KeyListener.
I am making a game.
I have two problems:

  1. In the method keyPressed, it has a little delay in the beginning when someone presses a key, that makes it unsuitable for runtime games.

  2. I would like to be able to handle more than one key at a time.

What should I do ?

Hi!

Just create an array of 256 boolean values.


boolean keys[] = new boolean[256];

Then, when a key is pressed, do something like that :


public void keyPressed(KeyEvent e)
{
   keys[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e)
{
   keys[e.getKeyCode()] = false;
}

Finally, in your draw method or anywhere you want, check keys’ state :


if(keys[KEY_EVENT.VK_UP])
{
   // Do something...
}

if(keys[KEY_EVENT.VK_DOWN])
{
   // Do something...
}

It should work…

++
Chman