fiveworlds Posted March 11, 2017 Posted March 11, 2017 I am using Java.awt.robot task automation. How can I capture a global button press event in windows to cancel the click automater when the user isn't clicking on eclipse in java? public class autoclicker extends Thread{ public void run(){ System.out.println("thread is running..."); Robot bot; try { bot = new Robot(); bot.mouseMove(1600, 600); for(int i =0; i < 1200000; i ++){ bot.mousePress(InputEvent.BUTTON1_MASK); bot.mouseRelease(InputEvent.BUTTON1_MASK); } } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static final int BINSIZE = 17; public static void main(String[] args) throws IOException, AWTException { autoclicker t1=new autoclicker(); t1.start(); }
Sensei Posted March 11, 2017 Posted March 11, 2017 (edited) Did you try MouseInfo.getPointerInfo()... ? https://docs.oracle.com/javase/7/docs/api/java/awt/MouseInfo.html PointerInfo class has member getLocation() returning Point object: https://docs.oracle.com/javase/7/docs/api/java/awt/PointerInfo.html Point class https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html Instead of waiting for user to click outside of window, you could check mouse coordinate whether it's f.e. left-top corner with some small tolerance f.e. 0 <= x <= 100px and 0 <= y <= 100px. Do it inside of the main thread loop, and break loop. You should have some delay between click and release mouse button, and other one after mouse movement. See method Robot.delay() https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html There is also MouseListener https://docs.oracle.com/javase/8/docs/api/java/awt/event/MouseListener.html https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html Edited March 11, 2017 by Sensei
fiveworlds Posted March 11, 2017 Author Posted March 11, 2017 Okay I found an answer on stackoverflow supposedly I have to use Java Native Interface and C. There is a github project that did that already https://github.com/kristian/system-hook so I imported that in an eclipse maven project and it seems to work. import java.awt.AWTException; import java.awt.Robot; import java.awt.event.InputEvent; import java.io.IOException; import lc.kra.system.keyboard.GlobalKeyboardHook; import lc.kra.system.keyboard.event.GlobalKeyAdapter; import lc.kra.system.keyboard.event.GlobalKeyEvent; import lc.kra.system.mouse.GlobalMouseHook; import lc.kra.system.mouse.event.GlobalMouseAdapter; import lc.kra.system.mouse.event.GlobalMouseEvent; public class App extends Thread{ public void run(){ System.out.println("thread is running..."); Robot bot; try { bot = new Robot(); bot.mouseMove(1600, 600); for(int i =0; i < 10000; i ++){ bot.mousePress(InputEvent.BUTTON1_MASK); bot.mouseRelease(InputEvent.BUTTON1_MASK); } } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static boolean run = true; public static void main(String[] args) throws IOException, AWTException { App t1=new App(); t1.start(); // might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(); System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown."); keyboardHook.addKeyListener(new GlobalKeyAdapter() { @Override public void keyPressed(GlobalKeyEvent event) { System.out.println(event); if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE) run = false; } @Override public void keyReleased(GlobalKeyEvent event) { System.out.println(event); } }); try { while(run) Thread.sleep(128); } catch(InterruptedException e) { /* nothing to do here */ } finally { keyboardHook.shutdownHook(); } } } Did you try MouseInfo.getPointerInfo()... ? That would work for what I am doing but it doesn't capture keypresses globally. I suppose it is hard to find because you could make keyloggers out of it.
Sensei Posted March 11, 2017 Posted March 11, 2017 In the real code you should call thread.interrupt() to stop execution of the thread. Inside of the main thread loop have if( thread.isInterrupted() ) break; to quit thread gracefully. And in the main() thread.join() to wait for thread to die.
fiveworlds Posted March 11, 2017 Author Posted March 11, 2017 (edited) Like this??? import java.awt.AWTException; import java.awt.MouseInfo; import java.awt.Point; import java.awt.PointerInfo; import java.awt.Robot; import java.awt.event.InputEvent; import java.io.IOException; import java.lang.InterruptedException; import java.util.BitSet; import lc.kra.system.keyboard.GlobalKeyboardHook; import lc.kra.system.keyboard.event.GlobalKeyAdapter; import lc.kra.system.keyboard.event.GlobalKeyEvent; import lc.kra.system.mouse.GlobalMouseHook; import lc.kra.system.mouse.event.GlobalMouseAdapter; import lc.kra.system.mouse.event.GlobalMouseEvent; public class App extends Thread implements Runnable { private static boolean run = true; private static GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(); private static GlobalMouseHook mouseHook = new GlobalMouseHook(); private BitSet keyboardInputHandlers() { keyboardHook.addKeyListener(new GlobalKeyAdapter() { @Override public void keyPressed(GlobalKeyEvent event) { System.out.println(event); if (event.getVirtualKeyCode() == GlobalKeyEvent.VK_ESCAPE) { Thread.currentThread().interrupt(); try { Thread.currentThread().join(); } catch (InterruptedException e) { run = false; } } } @Override public void keyReleased(GlobalKeyEvent event) { System.out.println(event); } }); BitSet statusCode = new BitSet(0); statusCode.set(0); return statusCode; } private BitSet mouseInputHandlers() { mouseHook.addMouseListener(new GlobalMouseAdapter() { @Override public void mouseMoved(GlobalMouseEvent event) { PointerInfo a = MouseInfo.getPointerInfo(); Point b = a.getLocation(); int x = (int) b.getX(); int y = (int) b.getY(); if (x < 1500) { Thread.currentThread().interrupt(); try { Thread.currentThread().join(); } catch (InterruptedException e) { run = false; } } } }); BitSet statusCode = new BitSet(0); statusCode.set(0); return statusCode; } private BitSet taskRunner() { BitSet statusCode = new BitSet(0); Robot bot = null; try { bot = new Robot(); } catch (AWTException e) { return statusCode; } bot.mouseMove(1600, 600); while (run == true) { bot.mousePress(InputEvent.BUTTON1_MASK); bot.mouseRelease(InputEvent.BUTTON1_MASK); } keyboardHook.shutdownHook(); mouseHook.shutdownHook(); statusCode.set(0); return statusCode; } public void run() { System.out.println("thread is running..."); BitSet statusCode; statusCode = keyboardInputHandlers(); if(!statusCode.get(0)){ System.out.println("Keyboard Error in App Thread"); } statusCode = mouseInputHandlers(); if(!statusCode.get(0)){ System.out.println("Mouse Handler Error in App Thread"); } statusCode = taskRunner(); if(!statusCode.get(0)){ System.out.println("Task Runner Error in App Thread"); } } public static void main(String[] args) throws IOException, AWTException { final App t1 = new App(); t1.start(); } } Edited March 11, 2017 by fiveworlds
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now