/** Starts adding points to the mouseEvents list every 2 milliseconds */ private void collect() { int same = 0; // The number of times that the mouse has been in same position. pause = false; // whether or not the mouse is currently idle while (record) { if (!pause) { // If the mouse is not currently idle, A new point is added to the "mouseEvents" List. point // has x and y coordinates mouseEvents.add( new SimpleMouseEvent( MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y, 0)); } // If the mouse is currently set as idle, but mouvement has been detected: that is, if the // current position is different than // where the mouse is when it was set as idle... if (pause && ((MouseInfo.getPointerInfo().getLocation().x) != mouseEvents.getLast().gx() || MouseInfo.getPointerInfo().getLocation().y != mouseEvents.getLast().gy())) { unpause(); } // Detects if the current position of the mouse is the same as the last time it was checked. if (mouseEvents.size() > 5 && (mouseEvents .getLast() .toString() .equals(mouseEvents.get(mouseEvents.size() - 2).toString()))) { // If position is the same, the "same" count is increased. same++; // Once the mouse has been in the same place for 10 cycles, it is said to be idle. if (same == 10) { pause = true; // The stopwatch will count how long the mouse has been idle for stopWatch.start(); System.out.println("Pause"); } } else { // If the mouse has moved since last cycle, then the number of times it has been in the same // position is set to 0. same = 0; } try { Thread.sleep(2); // Delay between cycles. } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } if (!record) { gui.log("Recording Stopped."); gui.refreshActionList(); } } }
/** Starts collection mouse information. Clears previous mouse events. */ public void startRecording() { gui.log("Recording Macro..."); mouseEvents.clear(); this.record = true; }