惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
D
Docker
GbyAI
GbyAI
博客园 - 三生石上(FineUI控件)
博客园_首页
H
Help Net Security
T
The Exploit Database - CXSecurity.com
Microsoft Security Blog
Microsoft Security Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
F
Full Disclosure
Blog — PlanetScale
Blog — PlanetScale
Y
Y Combinator Blog
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
aimingoo的专栏
aimingoo的专栏
Simon Willison's Weblog
Simon Willison's Weblog
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
S
Security Affairs
T
Tailwind CSS Blog
T
Tor Project blog
W
WeLiveSecurity
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
The Hacker News
The Hacker News
腾讯CDC
M
MIT News - Artificial intelligence
D
DataBreaches.Net
量子位
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
I
InfoQ
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
美团技术团队
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
A
About on SuperTechFans

博客园 - yongwnet

项目开发文档格式 JAVA反射实例 java.util.Properties类 jQuery调用WebService详解 产品经理的素质 JAVA 调用 .NET编写的WebService - yongwnet Win7下轻松注册VS2008 Windows7 下的VPC 浅谈项目管理能力的提升 java多态性详解——父类引用子类对象 Java截取字符串的一些常用处理 java类加载的表现形式 extjs COMBOBOX完成类似GOOGLE搜索框(AUTOCOMPLETE) - yongwnet - 博客园 如何有效的压缩VPC虚拟磁盘 VPC 2007 Console界面消失以及解决方法 - yongwnet JAVA匿名实现多线程 - yongwnet - 博客园 java中list类和vector类的比较 JAVA深克隆
Java中实现鼠标模拟与键盘映射
yongwnet · 2010-10-11 · via 博客园 - yongwnet

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。程序功能很简单:随机移动鼠标并点击左键。

代码如下:

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 Xiaofeng Wang

*/

public class MouseController implements Runnable {

private Dimension dim;

private Random rand;

private Robot robot;

private volatile boolean stop = false;

/** Creates a new instance of Main */

public MouseController() {

dim = Toolkit.getDefaultToolkit().getScreenSize();

rand = new Random();

try {

robot = new Robot();

} catch (AWTException ex) {

ex.printStackTrace();

}

}


public void run() {

while(!stop) {

int x = rand.nextInt(dim.width);

int y = rand.nextInt(dim.height);

robot.mouseMove(x, y);

robot.mousePress(InputEvent.BUTTON1_MASK);

try {

Thread.sleep(3000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

public synchronized void stop() {

stop = true;

}

/** * @param args the command line arguments */

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)。

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

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

代码如下:

public class Capture extends javax.swing.JFrame implements Runnable {

/** Creates new form Capture */

public Capture() {

initComponents();

try {

robot = new Robot();

} catch (AWTException ex) {

ex.printStackTrace();

}

dim = Toolkit.getDefaultToolkit().getScreenSize(); }

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold desc=" 生成的代码 " defaultstate="collapsed"></editor-fold>

private void initComponents() {

screenCanvas = new java.awt.Canvas();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

stop = true;

setResizable(false);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 519, javax.swing.GroupLayout.PREFERRED_SIZE) );

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)

);

pack();

}//

/** * @param args the command line arguments */

public static void main(String args[]) {

final Capture capture = new Capture();

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

capture.setVisible(true);

}

});

Thread cutThread = new Thread(capture);

cutThread.start();

}

public void run() {

stop = false;

while(!stop) {

BufferedImage bImage = robot.createScreenCapture(new Rectangle(dim.width, dim.height));

Graphics g = this.screenCanvas.getGraphics();

g.drawImage(bImage, 0, 0, this);

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

private synchronized void stop() {

stop = true;

}

// 变量声明 - 不进行修改

private java.awt.Canvas screenCanvas;

// 变量声明结束

private volatile boolean stop;

private Robot robot;

private Dimension dim;

}