java robot使用

  • Post author:
  • Post category:java



// 获取指定键盘位置


public static void main(String[] args) {


try {


// 创建java机器人对象

Robot robot =new Robot();

// 设置5秒

robot.delay(5000);

// 创建随机数

Random random = new Random();

while(true){


// 获取K键

robot.keyPress(KeyEvent.VK_K);

// 设置随机数

double a = random.nextDouble();

a = 0.2 + a*(0.4-0.2);

int b = (int)(a*1000);

System.out.println(b);

robot.delay(b);

// 弹起K键

robot.keyRelease(KeyEvent.VK_K);

double c = random.nextDouble();

c = 2 + c*(5-2);

int d = (int)(c*1000);

System.out.println(d);

robot.delay(d);

}

} catch (AWTException e) {


// TODO Auto-generated catch block

e.printStackTrace();

}

}


// 鼠标操作


public static void main(String[] args) {

try {


Robot robot = new Robot();

robot.delay(5000);

// 鼠标移动到指定位置

robot.mouseMove(1302, 741);

// 点击鼠标

// 鼠标左键

System.out.println(“单击”);

robot.mousePress(InputEvent.BUTTON1_MASK);

robot.mouseRelease(InputEvent.BUTTON1_MASK);

// 鼠标右键

System.out.println(“右击”);

robot.mousePress(InputEvent.BUTTON3_MASK);

robot.mouseRelease(InputEvent.BUTTON3_MASK);

// 按下ESC,退出右键状态

System.out.println(“按下ESC”);

robot.keyPress(KeyEvent.VK_ESCAPE);

robot.keyRelease(KeyEvent.VK_ESCAPE);

// 滚动鼠标滚轴

System.out.println(“滚轴”);

robot.mouseWheel(5);

} catch (AWTException e) {


// TODO Auto-generated catch block

e.printStackTrace();

}

}


// 获取屏幕颜色


public static void main(String[] args) {


try {


Robot robot = new Robot();

// 获取指定位置颜色

Color pixelColor = robot.getPixelColor(1302, 741);

System.out.println(pixelColor.getRed());

System.out.println(pixelColor.getGreen());

System.out.println(pixelColor.getBlue());

} catch (AWTException e) {


// TODO Auto-generated catch block

e.printStackTrace();

}

}



版权声明:本文为zengsange原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。