public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
moveUp = true;
break;
case KeyEvent.VK_LEFT:
moveLeft = true;
break;
case KeyEvent.VK_SPACE:
fire();
break;
//etc...
}
}
This is a short extract from my KeyListener method that is supposed to control a vechile in a game. The problem that I'm experiencing is when I press and hold down a few buttons, and then wants to press down another one, the keylistener doesn't detect that buttonpress!
For instance in this code, I press down and hold the Up and Left arrow keys on my keyboard and both moveUp and moveLeft is set to true. I want to keep hold down Up and Left because if I release them they will be set to false again in my keyReleased method. I now want to fire a shot from the vehicle while moving, but then when pressing Space while having the two arrow keys pressed down,
case KeyEvent.VK_SPACE: is never executed.
After testing with other button combinations I noticed that the keylistener can handle for example Left Right Down simultaneously, but not Left Right Up. No matter what combination I try it can't handle more than three buttons at the same time this way.
After searching after solutions on this I found this problem discussed every here and there on the web. Apparently, some people on their computers are not having this problem. I have read some people saying that this might be a hardware issue that is out of the programmer's control and depends to computer to computer on how this method is going to perform.
But I have seen nobody mentioning any clear solutions or workarounds on this yet! Anyone got experience of this? I think I've heard about something called "helper listener" that's is used in java for handling multiple inputs, but I'm not sure. Can't find much when searching it.
Thanks in advance for input!
PS: Here is a runnable program to test the issue I mentioned if you want to try it out. Try to press down and hold Up, Down and Space at the same time and see if you can get all values turned into true.
import javax.swing.*;
import java.awt.event.*;
public class Main extends JPanel implements KeyListener, Runnable {
boolean isUpPressed, isDownPressed, isSpacePressed;
static JFrame f;
public static void main(String[] args) {
f = new JFrame();
f.setSize(600,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Main());
f.setVisible(true);
}
public Main() {
setFocusable(true);
addKeyListener(this);
new Thread(this).start();
}
public void keyTyped(KeyEvent ke) {
}
public void keyPressed(KeyEvent ke) {
switch(ke.getKeyCode()) {
case KeyEvent.VK_UP: isUpPressed = true; break;
case KeyEvent.VK_DOWN: isDownPressed = true; break;
case KeyEvent.VK_SPACE: isSpacePressed = true; break;
}
}
public void keyReleased(KeyEvent ke) {
switch(ke.getKeyCode()) {
case KeyEvent.VK_UP: isUpPressed = false; break;
case KeyEvent.VK_DOWN: isDownPressed = false; break;
case KeyEvent.VK_SPACE: isSpacePressed = false; break;
}
}
public void run() {
while(true) {
try {
String s = "up pressed: " + isUpPressed + ", down pressed: " + isDownPressed +", spacePressed: " + isSpacePressed;
f.setTitle(s);
Thread.sleep(200);
} catch(Exception exc) {
exc.printStackTrace();
break;
}
}
}
}