kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.73k stars 343 forks source link

Root processes in MacOS are disabled when jnativehook is used with Robot class #289

Open ubmay opened 4 years ago

ubmay commented 4 years ago

I have some code for an auto clicker I am making in java.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.awt.*;

import java.awt.event.*;
import java.util.concurrent.*;
import org.jnativehook.keyboard.*;
import org.jnativehook.*;
@SpringBootApplication
public class DemoApplication {

    static int autoclick = 1;
    static Robot r;
    static {
        try {
            r=new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws NativeHookException {
        NativeKeyListener nkl = new NativeKeyListener() {

            @Override
            public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {

            }

            @Override
            public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
                if (nativeKeyEvent.getKeyCode() == nativeKeyEvent.VC_O) {
                    autoclick = 1;
                }
                if (nativeKeyEvent.getKeyCode() == nativeKeyEvent.VC_P) {
                    autoclick = 0;
                }
                if (nativeKeyEvent.getKeyCode() == nativeKeyEvent.VC_Q) {
                    System.exit(0);
                }
            }

            @Override
            public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {

            }
        };
        SpringApplication.run(DemoApplication.class, args);
        r.delay(3000);
        ExecutorService ee = Executors.newFixedThreadPool(2);
        ee.submit(DemoApplication::run);
        GlobalScreen.addNativeKeyListener(nkl);
        GlobalScreen.registerNativeHook();
    }

    public static void run() {
        while (true) {
            if (autoclick != 0) {
                r.mouseMove(((int) MouseInfo.getPointerInfo().getLocation().getX()), ((int) MouseInfo.getPointerInfo().getLocation().getLocation().getY()));
                r.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
                r.delay(100);
                r.mouseMove(((int) MouseInfo.getPointerInfo().getLocation().getX()), ((int) MouseInfo.getPointerInfo().getLocation().getLocation().getY()));
                r.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Running this script for a while disables mouse movement, hangs screen and pretty much disables all functions. MacOS Catalina 10.15.4

icarus44-zer0 commented 4 years ago

I have a working implementation of JNativeHook and the robot class. I had to Split the JNativeHook into its own file and call it from within Main.

public class Global_Keyboard_Listener implements NativeKeyListener {

Global_Keyboard_Listener() {
}

public void setup() {
    try {
        GlobalScreen.registerNativeHook();
    } catch (NativeHookException e) {
        e.printStackTrace();
    }
    GlobalScreen.addNativeKeyListener(new Global_Keyboard_Listener());

    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.OFF);

    Handler[] handlers = Logger.getLogger("").getHandlers();
    for (int i = 0; i < handlers.length; i++) {
        handlers[i].setLevel(Level.OFF);
    }
}

@Override
public void nativeKeyPressed(NativeKeyEvent e) {

}

@Override
public void nativeKeyReleased(NativeKeyEvent e) {

}

@Override
public void nativeKeyTyped(NativeKeyEvent e) {
}

}

Global_Keyboard_Listener listener = new Global_Keyboard_Listener();
        Robot robot;
        listener.setup();
        while (true) {
            try {
                String foo = fun();
                if (foo != null) {
                    robot = new Robot();
                    robot.keyPress(KeyEvent.VK_V);
                    robot.keyRelease(KeyEvent.VK_V);
                }
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}`