我遇到的问题是如何克服键盘问题。当我按下“向上”和“向下”等某些键时,方法keyPressed继续激活,但是当我按下“w”或“s”键时,该方法仅激活一次,直到我释放并再次按下它。我想知道如何模拟重复按键。
这是输入类:
public class Input extends KeyAdapter{
private GameObject p1;
private GameObject p2;
public Input(Handler handler){
p1 = handler.object.get(0);
p2 = handler.object.get(1);
}
public void keyPressed(KeyEvent e){
System.out.println("thismethodactivated");
int key = e.getKeyCode();
if(key == KeyEvent.VK_W) p1.setVelY(-5);
if(key == KeyEvent.VK_S) p1.setVelY(5);
if(key == KeyEvent.VK_D) p1.activate(1);
if(key == KeyEvent.VK_UP) p2.setVelY(-5);
if(key == KeyEvent.VK_DOWN) p2.setVelY(5);
if(key == KeyEvent.VK_LEFT) p2.activate(1);
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_W) p1.setVelY(0);
if(key == KeyEvent.VK_S) p1.setVelY(0);
if(key == KeyEvent.VK_UP) p2.setVelY(0);
if(key == KeyEvent.VK_DOWN) p2.setVelY(0);
}
}