📄 voter.java
字号:
import java.awt.AWTException;import java.awt.Robot;import java.awt.MouseInfo;import java.awt.Point;import java.awt.Color;import java.awt.event.KeyEvent;import java.util.Scanner;import java.util.StringTokenizer;import java.io.File;import java.io.FileNotFoundException;public class Voter { private static Robot robot = null; private static class Debugger extends Thread { private int delay = 0; private boolean isrun = true; Debugger(int de) { delay = (de > 100) ? de : 100; } public void run() { Point op = null; Color oc = null; while (!Thread.interrupted() && isrun) { try { Point np = MouseInfo.getPointerInfo().getLocation(); Color nc = robot.getPixelColor(np.x, np.y); if (!np.equals(op) || !nc.equals(oc)) { op = np; oc = nc; System.out.println(op.x + "," + op.y + oc.getRed() + "," + oc.getGreen() + "," + oc.getBlue()); } Thread.sleep(delay); } catch (InterruptedException e) { System.err.print("守护线程异常退出"); e.printStackTrace(System.err); return; } } } public void exit() { isrun = false; } } public static int getKeyValue(String key) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { return KeyEvent.class.getField("VK_" + key).getInt(null); } public static void main(String... args) { Scanner scanner = null; StringTokenizer command = null; String cmdStr = null; Debugger debugger = null; int repeat = 1; try { robot = new Robot(); } catch (AWTException e) { System.err.println("不允许操作!"); return; } for (int i = 0; i < repeat; i++) { switch (args.length) { case 0: scanner = new Scanner(System.in); break; case 3: case 2: repeat = Integer.valueOf(args[1]); case 1: try { scanner = new Scanner(new File(args[0])); } catch (FileNotFoundException e) { System.err.println("配置文件打开失败!"); } break; default: System.err.println("参数数目错误!"); break; } if (scanner == null) return; while (scanner.hasNextLine()) { cmdStr = scanner.nextLine(); command = new StringTokenizer(cmdStr); try { if (command.hasMoreTokens()) { String cmd = command.nextToken(); if (cmd.toLowerCase().equals("delay")) { robot.delay(Integer.valueOf(command.nextToken())); } else if (cmd.toLowerCase().equals("keypress")) { String keys = command.nextToken(); StringTokenizer kt = new StringTokenizer(keys, " ,"); while (kt.hasMoreTokens()) { robot.keyPress(getKeyValue(kt.nextToken())); } } else if (cmd.toLowerCase().equals("keyrelease")) { String keys = command.nextToken(); StringTokenizer kt = new StringTokenizer(keys, " ,"); while (kt.hasMoreTokens()) { robot.keyRelease(getKeyValue(kt.nextToken())); } } else if (cmd.toLowerCase().equals("type")) { String keys = command.nextToken(); StringTokenizer kt = new StringTokenizer(keys, " ,"); while (kt.hasMoreTokens()) { int key = getKeyValue(kt.nextToken()); robot.keyPress(key); robot.keyRelease(key); } } else if (cmd.toLowerCase().equals("mousemove")) { robot.mouseMove(Integer.valueOf(command .nextToken(" ,")), Integer.valueOf(command .nextToken(" ,"))); } else if (cmd.toLowerCase().equals("mousepress")) { robot.mousePress(Integer.valueOf(command .nextToken())); } else if (cmd.toLowerCase().equals("mouserelease")) { robot.mouseRelease(Integer.valueOf(command .nextToken())); } else if (cmd.toLowerCase().equals("mousewheel")) { robot.mouseWheel(Integer.valueOf(command .nextToken())); } else if (cmd.toLowerCase().equals("waitforidle")) { robot.waitForIdle(); } else if (cmd.toLowerCase().equals("autowaitforidle")) { robot.setAutoWaitForIdle(command.nextToken() .toLowerCase().equals("true")); } else if (cmd.toLowerCase().equals("autodelay")) { robot.setAutoDelay(Integer.valueOf(command .nextToken())); } else if (cmd.toLowerCase() .equals("waitforpixelcolor")) { int x = Integer.valueOf(command.nextToken(" ,")); int y = Integer.valueOf(command.nextToken(" ,")); Color color = new Color(Integer.valueOf(command .nextToken(" ,")), Integer.valueOf(command .nextToken(" ,")), Integer.valueOf(command .nextToken(" ,"))); long millis = Long.valueOf(command.nextToken()); while (!robot.getPixelColor(x, y).equals(color)) Thread.sleep(millis); } else if (cmd.toLowerCase().equals("debug")) { if (debugger != null) debugger.exit(); debugger = new Debugger(Integer.valueOf(command .nextToken())); debugger.setDaemon(true); debugger.start(); } else if (cmd.toLowerCase().equals("undebug")) { if (debugger != null) debugger.exit(); } else if (cmd.toLowerCase().equals("exit")) { return; } else if (cmd.startsWith("#")) { continue; } else { System.err.println("无法识别命令:" + cmd); } } } catch (Exception e) { System.err.println("执行命令\"" + cmdStr + "\"错误:"); e.printStackTrace(System.err); } } scanner.close(); System.out.println("进度:" + i + "/" + repeat); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -