很多時候,我們希望能夠實現自動測試,自動演示功能,或者是其它的一些鼠標和鍵盤控制的應用(比如幫人點擊廣告賺利潤等)。出于這樣的目的,自從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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package com.alexia; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import javax.swing.Icon; import javax.swing.ImageIcon; /** * @description Robot幫助類,實現基本的功能 * @author Alexia * @date 2013/5/18 * */ 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. 簡單的模擬測試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package com.alexia; import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; 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 enter int [] 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 enter ks = new int [] { KeyEvent.VK_D, KeyEvent.VK_I, KeyEvent.VK_R, KeyEvent.VK_ENTER }; Common.pressKeys(rb, ks, 500 ); rb.delay( 1000 ); // 運行CLS命令cls enter ks = new int [] { KeyEvent.VK_C, KeyEvent.VK_L, KeyEvent.VK_S, KeyEvent.VK_ENTER }; Common.pressKeys(rb, ks, 500 ); rb.delay( 1000 ); // 運行EXIT命令exit enter ks = 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. 點擊網易廣告賺取微薄利潤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package com.alexia; import java.awt.AWTException; import java.awt.Desktop; import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.IOException; import java.net.URI; import java.util.Random; 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://lanxuezaipiao.blog.163.com/" ); try { desktop.browse(uri); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { run(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.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(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/ytlcainiao/article/details/45021803