java SDK 1.3以后实现了Robot类。此类用于为测试自动化、自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot 的主要目的是便于 Java 平台实现自动测试。
使用该类生成输入事件与将事件发送到 AWT 事件队列或 AWT 组件的区别在于:事件是在平台的本机输入队列中生成的。例如,useMove 将实际移动鼠标光标,而不是只生成鼠标移动事件。
Robot中主要的鼠标和键盘控制方法有:
使用该类生成输入事件与将事件发送到 AWT 事件队列或 AWT 组件的区别在于:事件是在平台的本机输入队列中生成的。例如,useMove 将实际移动鼠标光标,而不是只生成鼠标移动事件。
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。程序功能很简单:随机移动鼠标并点击左键。
代码如下:
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;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.util.Random;
/**
*
*/
*
*/
/**
* @author sxc 石新春
* @Create date 2007-11-6
*/
public class MouseController implements Runnable {
private Dimension dim;
private Random rand;
private Robot robot;
private volatile boolean stop = false;
public MouseController() {
dim = DefaultToolkit().getScreenSize();
rand = new Random();
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
* @Create date 2007-11-6
*/
public class MouseController implements Runnable {
private Dimension dim;
private Random rand;
private Robot robot;
private volatile boolean stop = false;
public MouseController() {
dim = DefaultToolkit().getScreenSize();
rand = new Random();
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
public void run() {
while(!stop) {
int x = Int(dim.width);
int y = Int(dim.height);
useMove(x, y);
usePress(InputEvent.BUTTON1_MASK);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public void run() {
while(!stop) {
int x = Int(dim.width);
int y = Int(dim.height);
useMove(x, y);
usePress(InputEvent.BUTTON1_MASK);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public synchronized void stop() {
stop = true;
}
public static void main(String[] args) {
MouseController mc = new MouseController();
Thread mcThread = new Thread(mc);
System.out.println("Mouse Controller start");
mcThread.start();
try {
Thread.sleep(60000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
mc.stop();
System.out.println("Mouse Controller stoped");
stop = true;
}
public static void main(String[] args) {
MouseController mc = new MouseController();
Thread mcThread = new Thread(mc);
System.out.println("Mouse Controller start");
mcThread.start();
try {
Thread.sleep(60000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
mc.stop();
System.out.println("Mouse Controller stoped");
}
}
当然键盘映射也类似,无非是使用void keyPress(int keycode)。
}
当然键盘映射也类似,无非是使用void keyPress(int keycode)。
现 在实现了控制鼠标和键盘,接下了我们要获取操作后的效果(屏幕截图)。好在Robot类也提供了一个方法:BufferedImage createScreenCapture(Rectangle screenRect);可以直接将全屏幕或某个屏幕区域的像素拷贝到一个BufferedImage对象中。
好,下面实战使用robot截屏,实现Capture程序,每隔5秒截屏一次。
代码如下:
import java.awt.AWTException;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.GroupLayout.Alignment;
/**
*
*/
*
*/
/**
* @author sxc 石新春
*@Create date 2007-11-6
* @author sxc 石新春
*@Create date 2007-11-6
*/
public class Capture extends JFrame implements Runnable {
private Canvas screenCanvas;
private volatile boolean stop;
private Robot robot;
private Dimension dim;
public Capture() {
initComponents();
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
dim = DefaultToolkit().getScreenSize();
}
public class Capture extends JFrame implements Runnable {
private Canvas screenCanvas;
private volatile boolean stop;
private Robot robot;
private Dimension dim;
public Capture() {
initComponents();
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
dim = DefaultToolkit().getScreenSize();
}
private void initComponents() {
screenCanvas = new java.awt.Canvas();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
stop = true;
setResizable(false);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
ateParallelGroup(Alignment.LEADING).addComponent(screenCanvas,GroupLayout.PREFERRED_SIZE, 519,GroupLayout.PREFERRED_SIZE) );
layout.ateParallelGroup(Alignment.LEADING).addComponent(screenCanvas,GroupLayout.PREFERRED_SIZE, 434,GroupLayout.PREFERRED_SIZE)
);
screenCanvas = new java.awt.Canvas();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
stop = true;
setResizable(false);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
ateParallelGroup(Alignment.LEADING).addComponent(screenCanvas,GroupLayout.PREFERRED_SIZE, 519,GroupLayout.PREFERRED_SIZE) );
layout.ateParallelGroup(Alignment.LEADING).addComponent(screenCanvas,GroupLayout.PREFERRED_SIZE, 434,GroupLayout.PREFERRED_SIZE)
);
pack();
}
public static void main(String args[]) {
final Capture capture = new Capture();
EventQueue.invokeLater(new Runnable() {
public void run() {
capture.setVisible(true);
}
});
Thread cutThread = new Thread(capture);
cutThread.start();
mousemove是什么键}
public void run() {
stop = false;
while(!stop) {
}
public static void main(String args[]) {
final Capture capture = new Capture();
EventQueue.invokeLater(new Runnable() {
public void run() {
capture.setVisible(true);
}
});
Thread cutThread = new Thread(capture);
cutThread.start();
mousemove是什么键}
public void run() {
stop = false;
while(!stop) {
BufferedImage bImage = ateScreenCapture(new Rectangle(dim.width, dim.height));
Graphics g = Graphics();
g.drawImage(bImage, 0, 0, this);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private synchronized void stop() {
stop = true;
}
}
Graphics g = Graphics();
g.drawImage(bImage, 0, 0, this);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private synchronized void stop() {
stop = true;
}
}
例子二:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class RobotExp {
public static void main(String[] args) {
try {
try {
Runtime().exec("notepad");//打开notepad
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
Robot robot = new Robot();
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class RobotExp {
public static void main(String[] args) {
try {
try {
Runtime().exec("notepad");//打开notepad
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
Robot robot = new Robot();
//定义3秒的延迟开始写
robot.delay(3000);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_C);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
robot.delay(3000);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_C);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论