Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Source

By: a guest on Apr 6th, 2013  |  syntax: Java  |  size: 4.38 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import com.eaio.nativecall.IntCall;
  2. import com.eaio.nativecall.NativeCall;
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.datatransfer.DataFlavor;
  7. import java.awt.datatransfer.Transferable;
  8. import java.awt.datatransfer.UnsupportedFlavorException;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11.  
  12.  
  13. public class AutoHotkeyTest {
  14.     public AutoHotkeyTest() {
  15.         System.out.println("\nStarting...\n");
  16.         control();
  17.         setupSearch();
  18.  
  19.     }
  20.  
  21.     private static IntCall exec;
  22.  
  23.  
  24.     public static int[] getWindowInfo(String windowName) {
  25.         try {
  26.             int x, y, width, height;
  27.             ahk("WinGetPos, X, Y, Width, Height, " + windowName);
  28.             ahk("Clipboard = %X%");
  29.             x = Integer.parseInt(waitForClipboardChange(5).toString());
  30.             ahk("Clipboard = %Y%");
  31.             y = Integer.parseInt(waitForClipboardChange(5).toString());
  32.             ahk("Clipboard = %Width%");
  33.             width = Integer.parseInt(waitForClipboardChange(5).toString());
  34.             ahk("Clipboard = %Height%");
  35.             height = Integer.parseInt(waitForClipboardChange(5).toString());
  36.             return new int[]{x, y, width, height};
  37.         } catch (Throwable t) {
  38.             return getWindowInfo(windowName);
  39.         }
  40.     }
  41.  
  42.     public static void setupSearch() {
  43.         ahk("settitlematchmode 2\n" +
  44.                 "\n" +
  45.                 "CoordMode, ToolTip, screen\n" +
  46.                 "CoordMode, Mouse, screen\n" +
  47.                 "CoordMode, Pixel, screen");
  48.     }
  49.  
  50.     public static int[] getCords() {
  51.         ahk("Clipboard = %FoundX%\n");
  52.         int x = Integer.parseInt(waitForClipboardChange(200).toString());
  53.         ahk("Clipboard = %FoundY%\n");
  54.         int y = Integer.parseInt(waitForClipboardChange(200).toString());
  55.         return new int[]{x, y};
  56.     }
  57.  
  58.     static Timer t = null;
  59.  
  60.     public static Object waitForClipboardChange(int miliSeconds) {
  61.         try {
  62.             t = new Timer(miliSeconds, new ActionListener() {
  63.                 @Override
  64.                 public void actionPerformed(ActionEvent e) {
  65.                     t = null;
  66.                 }
  67.             });
  68.             t.start();
  69.             Object temp = null;
  70.             while (temp == null && t != null) {
  71.                 temp = Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
  72.             }
  73.             return temp;
  74.         } catch (Throwable j) {
  75.             j.printStackTrace();
  76.             return 3;
  77.         }
  78.  
  79.     }
  80.  
  81.     public static void clear() {
  82.         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new Transferable() {
  83.             public DataFlavor[] getTransferDataFlavors() {
  84.                 return new DataFlavor[0];
  85.             }
  86.  
  87.             public boolean isDataFlavorSupported(DataFlavor flavor) {
  88.                 return false;
  89.             }
  90.  
  91.             public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
  92.                 return null;
  93.             }
  94.         }, null);
  95.     }
  96.  
  97.  
  98.     public static void move(int x, int y) {
  99.         ahk("MouseMove, " + x + ", " + y);
  100.     }
  101.  
  102.     public static int search(int x1, int y1, int x2, int y2, int tolerance, String path) {
  103.         try {
  104.             ahk("ImageSearch, FoundX, FoundY, " + x1 + "," + y1 + "," + x2 + ", " + y2 + ",*" + tolerance + " C:\\Users\\Preston\\Pictures\\Screenshots\\" + path);
  105.             ahk("Clipboard = %ErrorLevel%\n");
  106.             System.out.println(waitForClipboardChange(2000));
  107.             return Integer.parseInt(waitForClipboardChange(2000).toString());
  108.         } catch (Throwable T) {
  109.             T.printStackTrace();
  110.             return search(x1, y1, x2, y2, tolerance, path);
  111.         }
  112.  
  113.     }
  114.  
  115.     public static boolean control() {
  116.         try {
  117.             NativeCall.init();
  118.             init();
  119.             return true;
  120.         } catch (Throwable t) {
  121.             t.printStackTrace();
  122.             System.out.println("\n" + "Something went wrong...");
  123.             return false;
  124.         }
  125.     }
  126.  
  127.  
  128.     public static void init() {
  129.         IntCall textdll = new IntCall("AutoHotkey.dll", "ahktextdll");
  130.         textdll.executeCall(new Object[]{"", "", ""});
  131.         exec = new IntCall("AutoHotkey.dll", "ahkExec");
  132.     }
  133.  
  134.     public static void ahk(String code) {
  135.             exec.executeCall(code);
  136.     }
  137. }