




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java中实现鼠标模拟与键盘映射关键字: java 鼠标模拟 键盘映射 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程序,每隔秒截屏一次。代码如下: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. */ / 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.printS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025黑龙江鸡西市融媒体中心招聘公益性岗位就业人员2人模拟试卷带答案详解
- 2025年度宜昌市中心人民医院公开招录29名专业技术人员(二)模拟试卷完整答案详解
- 2025年中国护士鞋行业市场分析及投资价值评估前景预测报告
- 2025北京市环科院编制外人员招聘6人考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025福建福州长乐机场海关驾驶员招聘1人考前自测高频考点模拟试题及一套完整答案详解
- 2025南平建阳区环境卫生服务中心招聘乡镇垃圾清运驾驶员1名考前自测高频考点模拟试题及1套参考答案详解
- 2025河北唐山市市直事业单位招聘工作人员277人模拟试卷完整答案详解
- 2025恒丰银行成都分行春季校园招聘模拟试卷完整答案详解
- 2025年上半年江西九江市事业单位“才汇九江”高层次人才招聘373人考前自测高频考点模拟试题参考答案详解
- 2025湖州安吉县交通投资发展集团有限公司招考2人模拟试卷及答案详解(夺冠)
- 费曼学习法课件
- 现代管理方法和理论作业
- 幼儿园控笔训练培训
- 木心全集讲稿系列:文学回忆录
- 肿瘤微环境中的细胞间通信
- 课程设计-MATLAB与通信仿真设计题目及程序
- 第6课 推动形成全面对外开放新格局高一思想政治《中国特色社会主义》同(高教版2023基础模块)
- 社会调查研究抽样课件
- 矩阵论同步学习辅导 张凯院 西北工业大学出版社
- 英语英语句子成分和基本结构
- GB/T 24218.1-2009纺织品非织造布试验方法第1部分:单位面积质量的测定
评论
0/150
提交评论