键盘监听
监听按下的按键
package cn.usts.edu.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyPressListener {
public static void main(String[] args) {
new KeyPressFrame("键盘监听");
}
}
class KeyPressFrame extends Frame{
public KeyPressFrame(String title) {
super(title);
setBounds(100,100,400,200);
setVisible(true);
// 添加键盘监听
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE){
System.out.println("按下了空格键");
}
}
});
}
}