`
jiqimiao
  • 浏览: 58311 次
  • 性别: Icon_minigender_1
  • 来自: 常州
社区版块
存档分类
最新评论

鼠标模拟和键盘映射测试

 
阅读更多
因为毕业设计需要,做了一点点鼠标和键盘模拟的测试,收获了一点关于Robot知识,这是一个非常有趣的类,此类用于测试自动化,自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot类主要目的是便于java平台实现自动测试。主要方法如下:动作都在java.awt.event包中的KeyEvent和MouseEvent中定义
  • 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)在配有滚轮的鼠标旋转滚轮
  • BufferedImage createScreenCapture(Rectangle screenRect)创建包含从屏幕中读取的像素的图像
第一个例子是鼠标模拟测试,在多线程中每隔1s随机移动鼠标,一共随机6次鼠标闪烁,源码如下:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->/*
*MouseSimulate.java
*
*Createdon2007-5-7,4:03:04
*
*Tochangethistemplate,chooseTools|TemplateManager
*andopenthetemplateintheeditor.
*/

packagecn.edu.yutao;

importjava.awt.AWTException;
importjava.awt.Dimension;
importjava.awt.Robot;
importjava.awt.Toolkit;
importjava.awt.event.InputEvent;
importjava.util.Random;

/**
*
*
@authorAslan
*/
publicclassMouseSimulateimplementsRunnable{

privatevolatilebooleanisRunning=false;

privateRobotrobot;
privateDimensiondim;
privateRandomrandom;

publicMouseSimulate(){
random
=newRandom();
dim
=Toolkit.getDefaultToolkit().getScreenSize();

try{
robot
=newRobot();
}
catch(AWTExceptione){
e.printStackTrace();
}
}

publicvoidrun(){
while(isRunning){
intx=random.nextInt((int)dim.getWidth());
inty=random.nextInt((int)dim.getHeight());
System.out.println(
"themouselocatedin("+x+","+y+")");

robot.mouseMove(x,y);
robot.mousePress(InputEvent.BUTTON1_MASK);

try{
Thread.sleep(
1000);
}
catch(InterruptedExceptione){
e.printStackTrace();
}
}
}

publicsynchronizedvoidstart(){
isRunning
=true;
}

publicsynchronizedvoidstop(){
isRunning
=false;
}

publicstaticvoidmain(String[]args){
MouseSimulatetest
=newMouseSimulate();

test.start();
System.out.println(
"-----------timestart-------------");
Threadthread
=newThread(test);
thread.start();

try{
Thread.sleep(
10000);
}
catch(InterruptedExceptione){
e.printStackTrace();
}

test.stop();
System.out.println(
"-----------timestop--------------");
}

}

Robot类的方法createScreenCapture可以简单的用于抓取屏幕图片,可以在java应用程序中直接调用该方法抓取屏幕,检测远程电脑屏幕状态,这里参考了java社区的例子,默认构造函数生成后缀为png的文件,可以在第二个构造函数传入其他名称,支持gif和jpg。截图程序源码如下:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->/*
*GuiCamera.java
*
*Createdon2007-5-7,4:18:46
*
*Tochangethistemplate,chooseTools|TemplateManager
*andopenthetemplateintheeditor.
*/

packagecn.edu.yutao;

importjava.awt.Dimension;
importjava.awt.Rectangle;
importjava.awt.Robot;
importjava.awt.Toolkit;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;

/**
*
*
@authorAslan
*/
publicclassGuiCamera{

privateStringfileName;
privatefinalStringdefaultFileName="camera";

privateStringformat;
privatefinalStringdefaultFormat="png";
Dimensiondim
=Toolkit.getDefaultToolkit().getScreenSize();


publicGuiCamera(){
this.fileName=defaultFileName;
this.format=defaultFormat;
}

publicGuiCamera(StringfileName,Stringformat){
this.fileName=fileName;
this.format=format;
}

publicvoidcapture()throwsException{
BufferedImageimageScreen
=(newRobot()).createScreenCapture(newRectangle((int)dim.getWidth(),(int)dim.getHeight()));
StringimageName
=this.fileName+"."+this.format;
Filefile
=newFile(imageName);
System.out.println(
"Savefile"+imageName);
ImageIO.write(imageScreen,format,file);
System.out.println(
"Finished!!");
}

publicstaticvoidmain(String[]args){
GuiCameracamera
=newGuiCamera("hello","jpg");
try{
camera.capture();
}
catch(Exceptione){
e.printStackTrace();
}
}



}
以上程序都在mac os 10.4.8下测试
分享到:
评论

相关推荐

    易语言winio模拟键盘源码,易语言测试源码,易语言winio模拟按键模

    winio模拟按键模块,模拟按键wio,开启winio,关闭winio,置鼠标动作,置键盘按键,等待空缓冲,取扫描码,InstallWinIoDriver,RemoveWinIoDriver,openwinio,closewinio,读端口,写端口,开始内存映射,解除内存映射,读内存双字...

    Windows和Linux上的钩子和模拟键盘事件-python

    Windows和Linux上的钩子和模拟键盘事件 键盘 使用这个小型 Python 库完全控制您的键盘。 挂钩全局事件、注册热键、模拟按键等等。 特性 所有键盘上的全局事件挂钩(无论焦点如何都捕获键)。 监听和发送键盘事件...

    windows 程序设计中文版

    7.4.4 在CHECKER中增加键盘接口 7.4.5 在击中测试中使用子窗口 7.4.6 CHECKER程序中的子窗口 7.4.7 子窗口和键盘 7.5 捕获鼠标 7.5.1 设计一个矩形 7.5.2 捕获的解决方案 7.5.3 BLOKOUT2程序 7.6 鼠标的滚轮 第8章 ...

    《精通direct3d图形及动画程序设计》附书源代码

    程序运行时通过单击鼠标左键可以在启用和禁用图形反锯齿效果之间进行切换。 6、FullScreen 演示全屏幕显示模式Direct3D程序的开发,主要是创建全屏显示模式的Direct3D设备。因为程序运行在全屏模式下,所以不能像...

    Visual C++实效编程百例光盘源代码

     实例101 模拟键盘输入   实例102 限定鼠标区域   实例103 截获鼠标移开事件   实例104 截获键盘信息  第12章 程序发布   实例105 产生程序序列号   实例106 建立一个快捷方式   实例107 设置...

    精通DirectX.3D图形与动画程序设计.pdf

    第四部分介绍了三维图形程序设计领域目前流行的许多实用技术,包括广告板技术、纹理动画技术、自然现象模拟、三维地形模拟、海浪效果模拟、柔性物体模拟、凹凸纹理应用、短毛发模拟以及体积雾等。第五部分是附录,...

    API之网络函数---整理网络函数及功能

    其中包括文件、文件映射、进程、线程、安全和同步对象等 CompareFileTime 对比两个文件的时间 CopyFile 复制文件 CreateDirectory 创建一个新目录 CreateFile 打开和创建文件、管道、邮槽、通信服务、设备以及...

    VC++6.0实效编程百例

    101·模拟键盘输入 102·限定鼠标区域 103·截获鼠标移开事件 104·截获键盘信息 第十二章 105·产生程序序列号 106·建立一个快捷方式 107·设置程序为自动被执行 108·注册与卸载OCX 109·限定程序的使用时限 110...

    Windows 程序设计(第5版)(上、下册)--详细书签版

    7.4.3 使用键盘仿真鼠标 7.4. 4 在checker中添加键盘接口 7.4. 5 将于窗口用于命中测试 7.4.6 checker中的子窗口 7.4.7 子窗口和键盘 7.5 捕获鼠标 7.5.1 设计矩形 7.5.2 捕获的解决方案 ...

    1350多个精品易语言模块

    内 存读写和进程操作.ec 内存读写模块 2.0.ec 内存读写模块 2[1].0.ec 内存读写模块.ec 内存读写模块 1.0.ec 内存读写模块2.0.ec 写屏幕.ec 冰川多媒体播放模块 1.0.ec 凝逸.注册易语言支持库 1.0.ec 凝逸网页取文...

    NeHe lesson06

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

    NeHe lesson10

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

    NeHe lesson03.

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

    NeHe lesson05

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

    NeHe lesson08.

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

    NeHe lesson02源代码

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

    NeHe lesson07

    光照和键盘控制 混合 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53709 Lesson 09-lesson 10 3D空间中移动图像 加载3D世界,并在其中漫游 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53822 Lesson 11-...

Global site tag (gtag.js) - Google Analytics