添加键盘的监听事件

Network
bingor_yhj 2 years ago
parent f25d185079
commit 384b6240da

@ -45,16 +45,60 @@ public class TankFrame extends Frame {
//这里使用内部内的原因是只有TankFrame用到 //这里使用内部内的原因是只有TankFrame用到
class MyKeyListener extends KeyAdapter { class MyKeyListener extends KeyAdapter {
private boolean bL = false;
private boolean bR = false;
private boolean bU = false;
private boolean bD = false;
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
super.keyPressed(e); super.keyPressed(e);
/*x += 10; /*x += 10;
repaint();*/ repaint();*/
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : {
bL = true;
break;
}
case KeyEvent.VK_RIGHT : {
bR = true;
break;
}
case KeyEvent.VK_UP : {
bU = true;
break;
}
case KeyEvent.VK_DOWN : {
bD = true;
break;
}
default: break;
}
} }
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
super.keyReleased(e); super.keyReleased(e);
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : {
bL = false;
break;
}
case KeyEvent.VK_RIGHT : {
bR = false;
break;
}
case KeyEvent.VK_UP : {
bU = false;
break;
}
case KeyEvent.VK_DOWN : {
bD = false;
break;
}
default: break;
}
} }
} }

Loading…
Cancel
Save