1 package com.tz.util; 2 3 import java.awt.Robot; 4 import java.awt.event.InputEvent; 5 import java.awt.event.KeyEvent; 6 7 /** 8 * @author sunshine 9 * @version 1.0 10 * @date:2015年8月16日 下午1:29:39 11 * @description: Robot实现自动化 12 * 整体功能实现自动点击电脑开始按钮、点击运行、 13 * 输入CMD命令进入dos命令窗口、查看当前目录dir、 14 * 查看本机ip信息、退出exit 15 */ 16 public class RobotCmd { 17 18 public static void main(String[] args) { 19 try { 20 //实例化机器人 21 Robot robot = new Robot(); 22 23 //先移动到开始菜单的位置 24 robot.mouseMove(24, 880); 25 26 robot.mousePress(InputEvent.BUTTON1_MASK); 27 robot.mouseRelease(InputEvent.BUTTON1_MASK); 28 robot.delay(500); 29 30 //移动到运行菜单的位置,并且点击 31 robot.mouseMove(245, 666); 32 robot.mousePress(InputEvent.BUTTON1_MASK); 33 robot.mouseRelease(InputEvent.BUTTON1_MASK); 34 robot.delay(500); 35 36 //robot按下CMD键 37 robot.keyPress(KeyEvent.VK_C); 38 robot.keyPress(KeyEvent.VK_M); 39 robot.keyPress(KeyEvent.VK_D); 40 robot.delay(500); 41 42 //点击确定,进入CMD控制台 43 robot.mouseMove(145, 745); 44 robot.mousePress(InputEvent.BUTTON1_MASK); 45 robot.mouseRelease(InputEvent.BUTTON1_MASK); 46 robot.delay(500); 47 48 //输入dir命令 49 int[] keyDir = {KeyEvent.VK_D,KeyEvent.VK_I,KeyEvent.VK_R,KeyEvent.VK_ENTER}; 50 KeyUtil.pressKeys(robot, keyDir, 500); 51 52 //输入ipconfig命令 53 int[] keyIpconfig = {KeyEvent.VK_I,KeyEvent.VK_P,KeyEvent.VK_C,KeyEvent.VK_O,KeyEvent.VK_N,KeyEvent.VK_F,KeyEvent.VK_I,KeyEvent.VK_G,KeyEvent.VK_ENTER}; 54 KeyUtil.pressKeys(robot, keyIpconfig, 500); 55 56 //输入exit命令 57 int[] keyExit = {KeyEvent.VK_E,KeyEvent.VK_X,KeyEvent.VK_I,KeyEvent.VK_T,KeyEvent.VK_ENTER}; 58 KeyUtil.pressKeys(robot, keyExit, 500); 59 60 61 62 } catch (Exception e) { 63 e.printStackTrace(); 64 } 65 } 66 }
1 package com.tz.util; 2 3 import java.awt.Robot; 4 5 /** 6 * @author sunshine 7 * @version 1.0 8 * @date:2015年8月16日 下午2:05:23 9 * @description: 键盘事件操作工具类 10 */ 11 public class KeyUtil { 12 13 /** 14 * 键盘输入方法 15 * 16 * @param robot 机器人类 17 * @param keys 输入键盘键名 18 * @param delay 延迟时间 19 * @return void 无 20 * 21 */ 22 public static void pressKeys(Robot robot,int[] keys,int delay){ 23 24 //循环遍历数组并赋值 25 for(int i=0;i<keys.length;i++){ 26 robot.keyPress(keys[i]); 27 robot.keyRelease(keys[i]); 28 robot.delay(delay); 29 } 30 } 31 }