[java]代码库
package com.sram.test;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class KeyListenerDemo {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(500,500);
jf.setTitle("键盘监听事件");
jf.setLocationRelativeTo(null);
//KeyListener KeyAdapter
//WindowListener WindowAdapter
jf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
/*System.out.println(e.getKeyChar());*/
System.out.println(KeyEvent.getKeyModifiersText(KeyEvent.VK_SHIFT));
//System.out.println(e.getKeyLocation());
}
});
jf.addKeyListener(new MyKeyAdapter() {
public void keyPressed(KeyEvent e) {
/*System.out.println(e.getKeyChar());*/
System.out.println(KeyEvent.getKeyModifiersText(KeyEvent.VK_SHIFT));
//System.out.println(e.getKeyLocation());
}
});
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class MyKeyAdapter implements KeyListener{
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}