illusionist

好好学习...天天向上

鼠标模拟和键盘映射测试

因为毕业设计需要,做了一点点鼠标和键盘模拟的测试,收获了一点关于Robot知识,这是一个非常有趣的类,此类用于测试自动化,自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot类主要目的是便于java平台实现自动测试。主要方法如下:动作都在java.awt.event包中的KeyEvent和MouseEvent中定义
  • 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)在配有滚轮的鼠标旋转滚轮
  • BufferedImage createScreenCapture(Rectangle screenRect)创建包含从屏幕中读取的像素的图像
第一个例子是鼠标模拟测试,在多线程中每隔1s随机移动鼠标,一共随机6次鼠标闪烁,源码如下:
/*
 * MouseSimulate.java
 * 
 * Created on 2007-5-7, 4:03:04
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/

package cn.edu.yutao;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.util.Random;

/**
 *
 * 
@author Aslan
 
*/
public class MouseSimulate implements Runnable{
    
    
private volatile boolean isRunning = false;
    
    
private Robot robot;
    
private Dimension dim;
    
private Random random;

    
public MouseSimulate() {
        random 
= new Random();
        dim 
= Toolkit.getDefaultToolkit().getScreenSize();
        
        
try{
            robot 
= new Robot();
        }
catch(AWTException e){
            e.printStackTrace();
        }
    }

    
public void run() {
        
while(isRunning){
            
int x = random.nextInt((int)dim.getWidth());
            
int y = random.nextInt((int)dim.getHeight());
            System.out.println(
"the mouse located in (" + x + "," + y + ")");
            
            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            
            
try{
                Thread.sleep(
1000);
            }
catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    
    
public synchronized void start(){
        isRunning 
= true;
    }
    
    
public synchronized void stop(){
        isRunning 
= false;
    }
    
    
public static void main(String[] args){
        MouseSimulate test 
= new MouseSimulate();
        
        test.start();
        System.out.println(
"-----------time start-------------");
        Thread thread 
= new Thread(test);
        thread.start();
        
        
try{
            Thread.sleep(
10000);
        }
catch(InterruptedException e){
            e.printStackTrace();
        }
        
        test.stop();
        System.out.println(
"-----------time stop--------------");
    }

}

Robot类的方法createScreenCapture可以简单的用于抓取屏幕图片,可以在java应用程序中直接调用该方法抓取屏幕,检测远程电脑屏幕状态,这里参考了java社区的例子,默认构造函数生成后缀为png的文件,可以在第二个构造函数传入其他名称,支持gif和jpg。截图程序源码如下:
/*
 * GuiCamera.java
 * 
 * Created on 2007-5-7, 4:18:46
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/

package cn.edu.yutao;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

/**
 *
 * 
@author Aslan
 
*/
public class GuiCamera {
    
    
private String fileName;
    
private final String defaultFileName = "camera";
    
    
private String format;
    
private final String defaultFormat = "png";
    Dimension dim 
= Toolkit.getDefaultToolkit().getScreenSize();
    

    
public GuiCamera() {
        
this.fileName = defaultFileName;
        
this.format = defaultFormat;
    }

    
public GuiCamera(String fileName, String format) {
        
this.fileName = fileName;
        
this.format = format;
    }
    
    
public void capture() throws Exception{
        BufferedImage imageScreen 
= (new Robot()).createScreenCapture(new Rectangle((int)dim.getWidth(), (int)dim.getHeight()));
        String imageName 
= this.fileName + "." + this.format;
        File file 
= new File(imageName);
        System.out.println(
"Save file " + imageName);
        ImageIO.write(imageScreen, format, file);
        System.out.println(
"Finished!!");
    }
    
    
public static void main(String[] args){
        GuiCamera camera 
= new GuiCamera("hello""jpg");
        
try{
            camera.capture();
        }
catch(Exception e){
            e.printStackTrace();
        }
    }
    
    

}
以上程序都在mac os 10.4.8下测试,截图为  很漂亮~ 出现警告是因为某些api在jdk6中已经标记为废弃。

posted on 2007-05-09 21:44 伽蓝 阅读(2107) 评论(5)  编辑  收藏 所属分类: Java SE

Feedback

# re: 鼠标模拟和键盘映射测试 2007-05-10 04:02 黑蝙蝠

恩 顶一下 不错这样看来可以用Robot类做一个类似按键精灵的软件
学习...  回复  更多评论   

# re: 鼠标模拟和键盘映射测试 2007-05-10 08:03 BeanSoft

还可以做远程控制, 跨平台的... 我做过, 但是功能比较弱... 主要是不支持组合键. http://gro.clinux.org/frs/?group_id=740&release_id=887  回复  更多评论   

# re: 鼠标模拟和键盘映射测试 2007-05-10 08:59 Swing

@BeanSoft
远程控制怎么做的 一个什么思路?  回复  更多评论   

# re: 鼠标模拟和键盘映射测试 2007-05-10 09:27 BeanSoft

就是个C/S模式的服务器, 客户端再服务器发回的截屏上点击按键和鼠标, 然后把事件发会给服务器端, 服务器端呢再用 Robot 模拟按键和鼠标.
详细使用说明:
http://gro.clinux.org/forum/forum.php?forum_id=2597  回复  更多评论   

# re: 鼠标模拟和键盘映射测试 2007-05-10 16:25 伽蓝

可以在服务端写一个servlet,将response返回流设置为image,这样可以在客户端查看服务器屏幕状态  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: