日期:2014-05-20 浏览次数:20733 次
public class test{ public static void print(){ System.out.println("Running!"); } public static void main(String[] args){ while(true){ try { Thread.sleep(10); } catch (Exception ex) { break; } } } }
public class Demo10 extends JFrame implements KeyListener { public Demo10() { this.setTitle("键盘事件处理"); this.setSize(300, 200); this.setLocationRelativeTo(this);// 窗口居中 this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口关闭动作处理 this.addKeyListener(this);// 添加注册键盘事件监听器 } @Override public void keyPressed(KeyEvent arg0) {// 敲击 // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent arg0) {// 按下 // TODO Auto-generated method stub if (arg0.getKeyCode() == 114) {// 114 F3对应的编码 Demo10.print(); } } @Override public void keyTyped(KeyEvent arg0) {// 释放 // TODO Auto-generated method stub } public static void main(String[] args) { new Demo10(); } public static void print() { System.out.println("Running!"); } }
------解决方案--------------------
不用窗口要全局监听 java实现费劲 要jni
定时的话简单 直接用timer类就可以了
你百度一下吧