Created
January 7, 2015 02:13
-
-
Save jarcode-foss/4853309993e257671f8a to your computer and use it in GitHub Desktop.
Simple Java Macros (with examples for POE)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.jna.Library; | |
import com.sun.jna.Native; | |
import org.jnativehook.GlobalScreen; | |
import org.jnativehook.NativeHookException; | |
import org.jnativehook.keyboard.NativeKeyEvent; | |
import org.jnativehook.keyboard.NativeKeyListener; | |
import org.jnativehook.mouse.NativeMouseEvent; | |
import org.jnativehook.mouse.NativeMouseInputListener; | |
import java.awt.*; | |
import java.awt.event.InputEvent; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.logging.Level; | |
import java.util.logging.LogManager; | |
import java.util.logging.Logger; | |
import static java.awt.event.KeyEvent.*; | |
import static org.jnativehook.keyboard.NativeKeyEvent.*; | |
public class HotkeyManager implements NativeKeyListener, NativeMouseInputListener { | |
final static HotkeyManager MANAGER = new HotkeyManager(); | |
final static Robot ROBOT; | |
final static User32 USER_32; | |
private HashMap<Integer, ArrayList<Macro>> map = new HashMap<Integer, ArrayList<Macro>>(); | |
// Create macros here | |
{ | |
// Gem switch macro | |
macro(VC_BACKQUOTE, 1000, new Runnable() { | |
@Override | |
public void run() { | |
Point at = MouseInfo.getPointerInfo().getLocation(); // grab point location | |
block(); // block input | |
release(); //release both mouse buttons (POE gets confused if you press twice without releasing) | |
sleep(20); | |
key(VK_I); // open inventory | |
sleep(150); | |
click(1558, 256, BUTTON3_DOWN_MASK); // right-click top left socket from my tabula | |
sleep(120); | |
click(1870, 615, BUTTON1_DOWN_MASK); // left-click top right slot in my inventory to switch with other skill | |
sleep(120); | |
click(1559, 256, BUTTON1_DOWN_MASK); // left-click back in left socket with the new gem | |
sleep(120); | |
key(VK_I); // close inventory | |
move(at.x, at.y); // restore pointer location | |
sleep(20); | |
unblock(); //unblock input | |
} | |
}); | |
// /oos macro | |
macro(VC_F11, 0, new Runnable() { | |
@Override | |
public void run() { | |
block(); | |
key(VK_ENTER, VK_SLASH, VK_O, VK_O, VK_S, VK_ENTER); | |
unblock(); | |
} | |
}); | |
} | |
static { | |
Robot local = null; | |
try { | |
local = new Robot(); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
ROBOT = local; | |
USER_32 = (User32) Native.loadLibrary("user32", User32.class); | |
} | |
public static void main(String[] args) throws Exception { | |
try { | |
GlobalScreen.registerNativeHook(); | |
} | |
catch(NativeHookException e) { | |
e.printStackTrace(); | |
} | |
GlobalScreen.getInstance().addNativeKeyListener(MANAGER); | |
GlobalScreen.getInstance().addNativeMouseListener(MANAGER); | |
LogManager.getLogManager().reset(); | |
Logger.getLogger(GlobalScreen.class.getPackage().getName()).setLevel(Level.OFF); | |
} | |
public void nativeKeyPressed(NativeKeyEvent e) { | |
ArrayList<Macro> list = map.get(e.getKeyCode()); | |
if (list != null) { | |
for (Macro macro : list) { | |
macro.use(); | |
} | |
} | |
} | |
public void nativeKeyReleased(NativeKeyEvent e) {} | |
public void nativeKeyTyped(NativeKeyEvent e) {} | |
public void nativeMouseClicked(NativeMouseEvent e) {} | |
public void nativeMousePressed(NativeMouseEvent e) {} | |
public void nativeMouseReleased(NativeMouseEvent e) {} | |
public void nativeMouseMoved(NativeMouseEvent e) {} | |
public void nativeMouseDragged(NativeMouseEvent e) {} | |
private static synchronized void key(int... keys) { | |
for (int i : keys) { | |
key(i); | |
} | |
} | |
private static synchronized void key(int key) { | |
ROBOT.keyPress(key); | |
ROBOT.keyRelease(key); | |
} | |
public static synchronized void click(int x, int y, int button) { | |
ROBOT.mouseMove(x, y); | |
sleep(10); | |
ROBOT.mousePress(button); | |
ROBOT.mouseRelease(button); | |
} | |
public synchronized void move(int x, int y) { | |
ROBOT.mouseMove(x, y); | |
} | |
public static void thread(Runnable runnable) { | |
Thread thread = new Thread(runnable); | |
thread.setDaemon(true); | |
thread.start(); | |
} | |
public static void sleep(long ms) { | |
try { | |
Thread.sleep(ms); | |
} | |
catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void block() { | |
USER_32.BlockInput(true); | |
} | |
public static void unblock() { | |
USER_32.BlockInput(false); | |
} | |
public synchronized static void release() { | |
ROBOT.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); | |
ROBOT.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); | |
} | |
public void macro(int key, final int cooldown, final Runnable task) { | |
ArrayList<Macro> list = map.get(key); | |
if (list == null) { | |
list = new ArrayList<Macro>(); | |
map.put(key, list); | |
} | |
list.add(new Macro() { | |
@Override | |
public int getCooldown() { | |
return cooldown; | |
} | |
@Override | |
public void run() { | |
task.run(); | |
} | |
}); | |
} | |
public abstract class Macro implements Runnable { | |
public long lastUsed = System.currentTimeMillis(); | |
public void use() { | |
if (System.currentTimeMillis() - lastUsed >= getCooldown() || getCooldown() <= 0) { | |
run(); | |
lastUsed = System.currentTimeMillis(); | |
} | |
} | |
public abstract int getCooldown(); | |
} | |
public static interface User32 extends Library { | |
boolean BlockInput(boolean block); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment