运行时,把输入法关掉,切换成系统自带的输入法即可;
第二个类是自定义的键值Map集合,主要是为了方便输入字符串,有需要的可以自行添加;
主要的代码如下,会创建一个名称为Automaton.java的类,并输出"Hellow Word,I am a skynet"
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.IOException; public class Hellow { public static void main(String[] args) throws AWTException, InterruptedException, IOException { // 创建机器人 Robot robot = new Robot(); // 设置默认休眠时间 robot.setAutoDelay(300); // 按下alt键 robot.keyPress(KeyEvent.VK_ALT); // 按下shift键 robot.keyPress(KeyEvent.VK_SHIFT); // 按下n键 robot.keyPress(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_ALT); robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(KeyEvent.VK_N); // 连续两次上键 robot.keyPress(KeyEvent.VK_UP); robot.keyRelease(KeyEvent.VK_UP); // 按下弹起回车键 robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); // 设置默认休眠时间20毫秒 robot.setAutoDelay(20); for (String c : "class".split("")) { if (Character.isUpperCase(c.charAt(0))) { // 如果是大写 // 输入名字 // 按下弹起CapsLock键,为了切换大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); // 按下弹起CapsLock键,切换回原来的大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); } else { // 如果是小写 // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); } } // 设置默认休眠时间300毫秒 robot.setAutoDelay(300); // 按下弹起回车键 robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); String automaton = "Automaton"; robot.setAutoDelay(20); for (String c : automaton.split("")) { if (Character.isUpperCase(c.charAt(0))) { // 如果是大写 // 按下弹起CapsLock键,为了切换大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); // 按下弹起CapsLock键,切换回原来的大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); } else { // 如果是小写 // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); } } robot.setAutoDelay(300); // 按下弹起回车键 robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); // 连续三次下键 robot.keyPress(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_DOWN); robot.keyPress(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_DOWN); robot.keyPress(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_DOWN); // 创建main方法 // 设置默认休眠时间,让输入字母快一点 robot.setAutoDelay(20); for (String c : "main".split("")) { if (Character.isUpperCase(c.charAt(0))) { // 如果是大写 // 输入名字 // 按下弹起CapsLock键,为了切换大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); // 按下弹起CapsLock键,切换回原来的大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); } else { // 如果是小写 // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); } } // 设置休眠时间为300毫秒 robot.setAutoDelay(300); // 按下弹起alt+/的快捷键 robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_ALT); robot.keyRelease(KeyEvent.VK_SLASH); // 休眠500,过快自动提示可能不会出现 robot.delay(500); // 按下弹起回车键 robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); // 设置默认休眠时间,让输入字母快一点 robot.setAutoDelay(20); for (String c : "syso".split("")) { if (Character.isUpperCase(c.charAt(0))) { // 如果是大写 // 按下弹起CapsLock键,为了切换大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); // 按下弹起CapsLock键,切换回原来的大小写 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); } else { // 如果是小写 // 循环每个字符,根据字符取出map中存储的键值 robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); } } // 按下弹起alt+/的快捷键 robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_ALT); robot.keyRelease(KeyEvent.VK_SLASH); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_QUOTE); robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(KeyEvent.VK_QUOTE); // 休眠50,过快自动提示可能不会出现 robot.delay(50); // 循环输入Hellow Word for (String c : "Hellow Word,I am a skynet".split("")) { if (Character.isUpperCase(c.charAt(0))) { // 输入名字 robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); robot.keyPress(KeyEvent.VK_CAPS_LOCK); robot.keyRelease(KeyEvent.VK_CAPS_LOCK); } else { robot.keyPress(KeyMap.keyMap.get(c.toUpperCase())); robot.keyRelease(KeyMap.keyMap.get(c.toUpperCase())); } } // 保存 按下弹起ctrl+s robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_S); // 运行 按下弹起ctrl+F11 robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_F11); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_F11); } }
下面是储存键值的map类,只有一些常用的键值,有需要请自行添加
import java.awt.event.KeyEvent; import java.util.HashMap; import java.util.Map; public class KeyMap { public static Map<String,Integer> keyMap; static{ keyMap = new HashMap<String,Integer>(); //设置map的值 //数字 keyMap.put("1", KeyEvent.VK_1); keyMap.put("2", KeyEvent.VK_2); keyMap.put("3", KeyEvent.VK_3); keyMap.put("4", KeyEvent.VK_4); keyMap.put("5", KeyEvent.VK_5); keyMap.put("6", KeyEvent.VK_6); keyMap.put("7", KeyEvent.VK_7); keyMap.put("8", KeyEvent.VK_8); keyMap.put("9", KeyEvent.VK_9); keyMap.put("0", KeyEvent.VK_0); //运算符 keyMap.put("/", KeyEvent.VK_SLASH); keyMap.put("+", KeyEvent.VK_PLUS); keyMap.put("-", KeyEvent.VK_MINUS); keyMap.put(" ", KeyEvent.VK_SPACE); keyMap.put("=", KeyEvent.VK_EQUALS); //特殊字符 keyMap.put(":", KeyEvent.VK_COLON); keyMap.put(",", KeyEvent.VK_COMMA); keyMap.put(".", KeyEvent.VK_PERIOD); //字母 keyMap.put("A",KeyEvent.VK_A); keyMap.put("B",KeyEvent.VK_B); keyMap.put("C",KeyEvent.VK_C); keyMap.put("D",KeyEvent.VK_D); keyMap.put("E",KeyEvent.VK_E); keyMap.put("F",KeyEvent.VK_F); keyMap.put("G",KeyEvent.VK_G); keyMap.put("H",KeyEvent.VK_H); keyMap.put("I",KeyEvent.VK_I); keyMap.put("J",KeyEvent.VK_J); keyMap.put("K",KeyEvent.VK_K); keyMap.put("L",KeyEvent.VK_L); keyMap.put("M",KeyEvent.VK_M); keyMap.put("N",KeyEvent.VK_N); keyMap.put("O",KeyEvent.VK_O); keyMap.put("P",KeyEvent.VK_P); keyMap.put("Q",KeyEvent.VK_Q); keyMap.put("R",KeyEvent.VK_R); keyMap.put("S",KeyEvent.VK_S); keyMap.put("T",KeyEvent.VK_T); keyMap.put("U",KeyEvent.VK_U); keyMap.put("V",KeyEvent.VK_V); keyMap.put("W",KeyEvent.VK_W); keyMap.put("X",KeyEvent.VK_X); keyMap.put("Y",KeyEvent.VK_Y); keyMap.put("Z",KeyEvent.VK_Z); // } }