很多时候,我们希望能够实现自动测试,自动演示功能,或者是其它的一些鼠标和键盘控制的应用(比如帮人点击广告赚利润等)。出于这样的目的,自从JDK1.3开始,它就为我们提供了一个用来产生本机输入事件的机器人类 — java.awt.Robot.
下面我来详细介绍Robot的功能及应用示例:
一、Robot主要的功能
1. BufferedImage createScreenCapture(Rectangle screenRect)
说明:该方法提供类似于键盘上的PrintScreen键的功能,将指定矩形区域内的屏幕像素copy下来产生一个BufferedImage。
应用:我们可以将这个方法用在图形程序中,或是用它来实现远端屏幕传输,可做成远端电脑监控程序等.
2. void delay(int ms)
说明:用来将当前的程序(thread)休眠(sleep)若干毫秒(ms)。
应用:可用来控制程序的延时。这个一般是必须的,因为你在两次间隔操作中肯定有延时。
3. Color getPixelColor(int x, int y)
说明:取得给定屏幕坐标像素位置的颜色值。
应用:就是取颜色RGB值,就不多说了。
4. void keyPress(int keycode),void keyRelease(int keycode)
说明:这两个方法的作用一看便知,用来产生指定键的按键按下与抬起动作,相当于Win32 API的keyb_event函数,即模拟键盘操作咯,具体keycode值就是KeyEvent.VK_C、KeyEvent.VK_D、KeyEvent.VK_CONTROL什么的,具体应用时直接看Eclipse提示就知道了。
应用:可用于程序的自动演示、测试等,非常有用。
5. void mouseMove(int x, int y)
说明:将鼠标光标移动到指定的屏幕坐标。
应用:可用于程序的自动演示、测试等,配合其他的方法使用,是不可缺少的。
6. void mousePress(int buttons),void mouseRelease(int buttons),void mouseWheel(int wheelAmt)
说明:上面的三种方法,产生指定鼠标按钮的按下,抬起,及滚轮动作,就是模拟鼠标操作咯,具体buttons的值有InputEvent.BUTTON1_MASK(鼠标左键)、InputEvent.BUTTON3_MASK(鼠标右键,如果是双键鼠标,请改用InputEvent.BUTTON2_MASK)等。
应用:一样也可用于程序的自动演示、测试等,配合其他方法使用,很重要。
二、应用实例
我写了两个比较小的应用实例,一个是简单的模拟测试,一个是自动点击广告赚利润的,下面分别演示。
首先编写一些公用的方法Common.java
public class Common {/*** 鼠标单击(左击),要双击就连续调用** @param r* @param x* x坐标位置* @param y* y坐标位置* @param delay* 该操作后的延迟时间*/public static void clickLMouse(Robot r, int x, int y, int delay) {r.mouseMove(x, y);r.mousePress(InputEvent.BUTTON1_MASK);r.delay(10);r.mouseRelease(InputEvent.BUTTON1_MASK);r.delay(delay);}/*** 鼠标右击,要双击就连续调用** @param r* @param x* x坐标位置* @param y* y坐标位置* @param delay* 该操作后的延迟时间*/public static void clickRMouse(Robot r, int x, int y, int delay) {r.mouseMove(x, y);r.mousePress(InputEvent.BUTTON3_MASK);r.delay(10);r.mouseRelease(InputEvent.BUTTON3_MASK);r.delay(delay);}/*** 键盘输入(一次只能输入一个字符)** @param r* @param ks* 键盘输入的字符数组* @param delay* 输入一个键后的延迟时间*/public static void pressKeys(Robot r, int[] ks, int delay) {for (int i = 0; i < ks.length; i++) {r.keyPress(ks[i]);r.delay(10);r.keyRelease(ks[i]);r.delay(delay);}}/*** 复制** @param r* @throws InterruptedException*/void doCopy(Robot r) throws InterruptedException {Thread.sleep(3000);r.setAutoDelay(200);r.keyPress(KeyEvent.VK_CONTROL);r.keyPress(KeyEvent.VK_C);r.keyRelease(KeyEvent.VK_CONTROL);r.keyRelease(KeyEvent.VK_C);}/*** 粘贴** @param r* @throws InterruptedException*/void doParse(Robot r) throws InterruptedException {r.setAutoDelay(500);Thread.sleep(2000);r.mouseMove(300, 300);r.mousePress(InputEvent.BUTTON1_MASK);r.mouseRelease(InputEvent.BUTTON1_MASK);r.keyPress(KeyEvent.VK_CONTROL);r.keyPress(KeyEvent.VK_V);r.keyRelease(KeyEvent.VK_CONTROL);r.keyRelease(KeyEvent.VK_V);}/*** 捕捉全屏慕** @param r* @return*/public Icon captureFullScreen(Robot r) {BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));ImageIcon icon = new ImageIcon(fullScreenImage);return icon;}/*** 捕捉屏幕的一个矫形区域** @param r* @param x* x坐标位置* @param y* y坐标位置* @param width* 矩形的宽* @param height* 矩形的高* @return*/public Icon capturePartScreen(Robot r, int x, int y, int width, int height) {r.mouseMove(x, y);BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle(width, height));ImageIcon icon = new ImageIcon(fullScreenImage);return icon;}}
1、简单的模拟测试
public class SimpleTest {public static void main(String[] args) throws Exception {final Robot rb = new Robot();new Thread() {public void run() {rb.delay(2000); // 模拟回车rb.keyPress(KeyEvent.VK_ENTER);rb.keyRelease(KeyEvent.VK_ENTER);}}.start();rb.delay(3000);// 设置开始菜单的大概位置int x = 40;int y = Toolkit.getDefaultToolkit().getScreenSize().height - 10; // 鼠标移动到开始菜单,rb.mouseMove(x, y);rb.delay(500);// 单击开始菜单Common.clickLMouse(rb, x, y, 500);rb.delay(1000);// 运行CMD命令cmd enterint[] ks = { KeyEvent.VK_C, KeyEvent.VK_M,KeyEvent.VK_D, KeyEvent.VK_ENTER, };Common.pressKeys(rb, ks, 500);rb.mouseMove(400, 400);rb.delay(500);// 运行DIR命令dir enterks = new int[] { KeyEvent.VK_D, KeyEvent.VK_I, KeyEvent.VK_R,KeyEvent.VK_ENTER };Common.pressKeys(rb, ks, 500);rb.delay(1000);// 运行CLS命令cls enterks = new int[] { KeyEvent.VK_C, KeyEvent.VK_L, KeyEvent.VK_S,KeyEvent.VK_ENTER };Common.pressKeys(rb, ks, 500);rb.delay(1000);// 运行EXIT命令exit enterks = new int[] { KeyEvent.VK_E, KeyEvent.VK_X, KeyEvent.VK_I,KeyEvent.VK_T, KeyEvent.VK_ENTER };Common.pressKeys(rb, ks, 500);rb.delay(1000);// 右键测试x = Toolkit.getDefaultToolkit().getScreenSize().width - 50;Common.clickRMouse(rb, x, y, 500);new Thread() {public void run() {rb.delay(1000); // 回车rb.keyPress(KeyEvent.VK_ENTER);rb.keyRelease(KeyEvent.VK_ENTER);}}.start();JOptionPane.showMessageDialog(null, "演示完毕!");}}
2. 点击网易广告赚取微薄利润
public class AutoClickAds {private Robot robot;private volatile boolean stop = false;/** Creates a new instance of Main */public AutoClickAds() {try {robot = new Robot();} catch (AWTException ex) {ex.printStackTrace();}}public void init() {robot.delay(3000);System.out.println("Click Ads start");// 在新的浏览器窗口或在已有的浏览器窗口打开指定的URL(JDK 1.6以上)Desktop desktop = Desktop.getDesktop();if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {URI uri = URI.create("http://linwenhua.blog.163.com/");try {desktop.browse(uri);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {run();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}stop();System.out.println("Click Ads stoped");}public void run() throws InterruptedException {int count = 1;while (!stop) {robot.delay(8000);int x = 576;int y = 567;Random r = new Random();Common.clickLMouse(robot, x, y, 3000);// 输入向下箭头,实现翻页int[] ks = { KeyEvent.VK_DOWN };for (int i = 0; i < 10; i++)Common.pressKeys(robot, ks, 0);int[][] a = { { 500, 103 }, { 500, 163 }, { 500, 223 },{ 500, 283 }, { 500, 343 }, { 500, 403 }, { 500, 463 },{ 500, 523 }, { 500, 583 }, { 500, 643 }, };int b = r.nextInt(5);x = a[b][0];y = a[b][1];Common.clickLMouse(robot, x, y, 1000);// 输入向下箭头,实现翻页for (int i = 0; i < 500; i++)Common.pressKeys(robot, ks, 0);// 输入向下箭头,实现翻页int[] kups = { KeyEvent.VK_UP };for (int i = 0; i < 3; i++)Common.pressKeys(robot, kups, 0);x = 900;y = 210;Common.clickLMouse(robot, x, y, 3000);x =1090;y =15;Common.clickLMouse(robot, x, y, 3000);x = 900;y = 135;Common.clickLMouse(robot, x, y, 3000);System.out.println("成功点击第" + count + "个广告!");}}public synchronized void stop() {stop = true;}/*** * @param args the command line arguments** @throws InterruptedException*/public static void main(String[] args) throws InterruptedException {AutoClickAds mc = new AutoClickAds();mc.init();}}
