一、键盘监听
package com.massimo.keyborad;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1, 2 , 200 , 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个,当前的码
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
效果: