import java.awt.Robot;
import java.awt.event.KeyEvent;
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Robot robot = new Robot();
robot.delay(5000);
mouseDemo(robot);
for(int i=0;i<5;i++)
{
pressKey(robot, KeyEvent.VK_F5);
pressKey(robot, KeyEvent.VK_A);
pressKey(robot, KeyEvent.VK_S);
pressKey(robot, KeyEvent.VK_J);
robot.delay(1000);
//System.out.print(i);
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* @param robot
* @param keyvalue 按键值
*/
public static void pressKey(Robot robot, int keyvalue) {
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
}
public static void pressKeyWithShift(Robot robot, int keyvalue) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
public static void mouseDemo(Robot robot){
robot.mouseMove(80, 10);
robot.mousePress(KeyEvent.BUTTON1_MASK);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
robot.mouseMove(600, 600);
robot.mouseRelease(KeyEvent.BUTTON1_MASK);
}
}
import java.awt.event.KeyEvent;
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Robot robot = new Robot();
robot.delay(5000);
mouseDemo(robot);
for(int i=0;i<5;i++)
{
pressKey(robot, KeyEvent.VK_F5);
pressKey(robot, KeyEvent.VK_A);
pressKey(robot, KeyEvent.VK_S);
pressKey(robot, KeyEvent.VK_J);
robot.delay(1000);
//System.out.print(i);
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* @param robot
* @param keyvalue 按键值
*/
public static void pressKey(Robot robot, int keyvalue) {
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
}
public static void pressKeyWithShift(Robot robot, int keyvalue) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
public static void mouseDemo(Robot robot){
robot.mouseMove(80, 10);
robot.mousePress(KeyEvent.BUTTON1_MASK);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
robot.mouseMove(600, 600);
robot.mouseRelease(KeyEvent.BUTTON1_MASK);
}
}
这个Robot对于用Java实现自动化测试很有帮助,但是鄙人最近用了下,发现不是尽如意。因为当我运行这个程序之后,再把焦点调到一个最前的窗口,其中的A,W,D等键按下没什么作用,控制不了焦点程序。不知道原因出在哪里啊?????