如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2007年7月5日星期四

Java中实现鼠标模拟与键盘映射

Java SDK 1.3以后实现了Robot类。此类用于为测试自动化、自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot 的主要目的是便于 Java 平台实现自动测试。
使用该类生成输入事件与将事件发送到 AWT 事件队列或 AWT 组件的区别在于:事件是在平台的本机输入队列中生成的。例如,Robot.mouseMove 将实际移动鼠标光标,而不是只生成鼠标移动事件。
Robot中主要的鼠标和键盘控制方法有:

  • void keyPress(int keycode) 按下给定的键。
  • void keyRelease(int keycode) 释放给定的键。
  • void mouseMove(int x, int y) 将鼠标指针移动到给定屏幕坐标。
  • void mousePress(int buttons) 按下一个或多个鼠标按钮。
  • void mouseRelease(int buttons) 释放一个或多个鼠标按钮。
  • void mouseWheel(int wheelAmt) 在配有滚轮的鼠标上旋转滚轮。

下面就让我们来实战鼠标控制,实现一个简单的鼠标控制程序MouseController。程序功能很简单:随机移动鼠标并点击左键。

代码如下:

   1: import java.awt.AWTException;
   2:  
   3: import java.awt.Dimension;
   4:  
   5: import java.awt.Robot;
   6:  
   7: import java.awt.Toolkit;
   8:  
   9: import java.awt.event.InputEvent;
  10:  
  11: import java.util.Random;
  12:  
  13:  
  14: /**
  15:  *
  16:  *
  17:  *
  18:  * @author Xiaofeng Wang
  19:  *
  20:  */
  21:  
  22: public class MouseController implements Runnable {
  23:     
  24:     private Dimension dim;
  25:     
  26:     private Random rand;
  27:     
  28:     private Robot robot;
  29:     
  30:     private volatile boolean stop = false;
  31:     
  32:     /** Creates a new instance of Main */
  33:     
  34:     public MouseController() {
  35:         
  36:         dim = Toolkit.getDefaultToolkit().getScreenSize();
  37:         
  38:         rand = new Random();
  39:         
  40:         try {
  41:             
  42:             robot = new Robot();
  43:             
  44:         } catch (AWTException ex) {
  45:             
  46:             ex.printStackTrace();
  47:             
  48:         }
  49:         
  50:     }
  51:     
  52:     
  53:     public void run() {
  54:         
  55:         while(!stop) {
  56:             
  57:             int x = rand.nextInt(dim.width);
  58:             
  59:             int y = rand.nextInt(dim.height);
  60:             
  61:             robot.mouseMove(x, y);
  62:             
  63:             //robot.mousePress(InputEvent.BUTTON1_MASK);
  64:             
  65:             try {
  66:                 
  67:                 Thread.sleep(3000);
  68:                 
  69:             } catch (InterruptedException ex) {
  70:                 
  71:                 ex.printStackTrace();
  72:                 
  73:             }
  74:             
  75:         }
  76:         
  77:     }
  78:     
  79:     public synchronized void stop() {
  80:         
  81:         stop = true;
  82:         
  83:     }
  84:     
  85:     /** * @param args the command line arguments */
  86:     
  87:     public static void main(String[] args) {
  88:         
  89:         MouseController mc = new MouseController();
  90:         
  91:         Thread mcThread = new Thread(mc);
  92:         
  93:         System.out.println("Mouse Controller start");
  94:         
  95:         mcThread.start();
  96:         
  97:         try {
  98:             
  99:             Thread.sleep(60000);
 100:             
 101:         } catch (InterruptedException ex) {
 102:             
 103:             ex.printStackTrace();
 104:             
 105:         }
 106:         
 107:         mc.stop();
 108:         
 109:         System.out.println("Mouse Controller stoped");
 110:         
 111:     }
 112:     
 113: }

当然键盘映射也类似,无非是使用void keyPress(int keycode)。

现在实现了控制鼠标和键盘,接下了我们要获取操作后的效果(屏幕截图)。好在Robot类也提供了一个方法:BufferedImage createScreenCapture(Rectangle screenRect);可以直接将全屏幕或某个屏幕区域的像素拷贝到一个BufferedImage对象中。

好,下面实战使用robot截屏,实现Capture程序,每隔1秒截屏一次。

代码如下:


   1: public class Capture extends javax.swing.JFrame implements Runnable {
   2:     
   3:     /** Creates new form Capture */
   4:     
   5:     public Capture() {
   6:         
   7:         initComponents();
   8:         
   9:         try {
  10:             
  11:             robot = new Robot();
  12:             
  13:         } catch (AWTException ex) {
  14:             
  15:             ex.printStackTrace();
  16:             
  17:         }
  18:         
  19:         dim = Toolkit.getDefaultToolkit().getScreenSize(); }
  20:     
  21:     /** This method is called from within the constructor to
  22:  
  23:      * initialize the form.
  24:  
  25:      * WARNING: Do NOT modify this code. The content of this method is
  26:  
  27:      * always regenerated by the Form Editor.
  28:  
  29:      */
  30:     
  31: //
  32:     
  33:     private void initComponents() {
  34:         
  35:         screenCanvas = new java.awt.Canvas();
  36:         
  37:         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  38:         
  39:         stop = true;
  40:         
  41:         setResizable(false);
  42:         
  43:         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  44:         
  45:         getContentPane().setLayout(layout);
  46:         
  47:         layout.setHorizontalGroup(
  48:                 
  49:                 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  50:                 
  51:                 .addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 519, javax.swing.GroupLayout.PREFERRED_SIZE) );
  52:         
  53:         layout.setVerticalGroup(
  54:                 
  55:                 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  56:                 
  57:                 .addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
  58:                 
  59:                 );
  60:         
  61:         pack();
  62:         
  63:     }//
  64:     
  65:     /** * @param args the command line arguments */
  66:     
  67:     public static void main(String args[]) {
  68:         
  69:         final Capture capture = new Capture();
  70:         
  71:         java.awt.EventQueue.invokeLater(new Runnable() {
  72:             
  73:             public void run() {
  74:                 
  75:                 capture.setVisible(true);
  76:                 
  77:             }
  78:             
  79:         });
  80:         
  81:         Thread cutThread = new Thread(capture);
  82:         
  83:         cutThread.start();
  84:         
  85:     }
  86:     
  87:     public void run() {
  88:         
  89:         stop = false;
  90:         
  91:         while(!stop) {
  92:             
  93:             BufferedImage bImage = robot.createScreenCapture(new Rectangle(dim.width, dim.height));
  94:             
  95:             Graphics g = this.screenCanvas.getGraphics();
  96:             
  97:             g.drawImage(bImage, 0, 0, this);
  98:             
  99:             try {
 100:                 
 101:                 Thread.sleep(1000);
 102:                 
 103:             } catch (InterruptedException ex) {
 104:                 
 105:                 ex.printStackTrace();
 106:                 
 107:             }
 108:             
 109:         }
 110:         
 111:     }
 112:     
 113:     private synchronized void stop() {
 114:         
 115:         stop = true;
 116:         
 117:     }
 118:     
 119: // 变量声明 - 不进行修改
 120:     
 121:     private java.awt.Canvas screenCanvas;
 122:     
 123: // 变量声明结束
 124:     
 125:     private volatile boolean stop;
 126:     
 127:     private Robot robot;
 128:     
 129:     private Dimension dim;
 130:     
 131: }

转载自:http://zjharry.blogspot.com/2007/03/java_14.html作者:http://www.blogger.com/profile/14571187675245178578

没有评论: