private class KeyHandler implements KeyEventDispatcher{
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
tetrominoList.get(currentPiece).moveRight();
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
tetrominoList.get(currentPiece).moveLeft();
}else if (e.getKeyCode() == KeyEvent.VK_UP){
System.out.print("k");
tetrominoList.get(currentPiece).rotate();
}
return false;
}
このコードは、上矢印を 1 回押すと「kk」を返します。2 つではなく 1 つのキー ヒットのみを登録するように Java またはコンピュータを構成するにはどうすればよいですか?
編集:これは私の一時的な解決策です
private class KeyHandler implements KeyEventDispatcher{
int counter = 0;
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
tetrominoList.get(currentPiece).moveRight();
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
tetrominoList.get(currentPiece).moveLeft();
}else if (e.getKeyCode() == KeyEvent.VK_UP){
counter++;
if(counter == 2){
System.out.print("k");
tetrominoList.get(currentPiece).rotate();
counter = 0;
}
}