jnativehook not working in a loop with Robot

72 views Asked by At

I'm using jnativehook to capture the input of my mouse and keyboard, record them, and then use them to activate a loop using Robot to repeat the pattern.

Everything works, the problem comes when i want to end the loop before its natural end, with the mouse moving around the screen due to Robot() ...pretty annoying

The goal was to add a way to still be able to receive keyboard input while the loop is in progress with a mouse-only Robot pattern....and it doesn't work. When the loop starts no key press is registered.

Here's the code of the important parts

@SpringBootApplication
public class MousePointerApplication {


    public static void main(String[] args) {
        SpringApplication.run(MousePointerApplication.class, args);
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());

            System.exit(1);
        }

    }

}
package com.Pointer.mousePointer.core;

import com.Pointer.mousePointer.bean.Input;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import com.github.kwhat.jnativehook.mouse.NativeMouseEvent;
import com.github.kwhat.jnativehook.mouse.NativeMouseInputListener;
import com.github.kwhat.jnativehook.mouse.NativeMouseWheelListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.PostConstruct;
import java.awt.*;
import java.awt.event.InputEvent;
import java.io.*;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class PatternCreator implements NativeMouseInputListener, NativeMouseWheelListener, NativeKeyListener {

    StopWatch stopWatch = new StopWatch("unico");
    private Map<Long, Input> mappaMouse = new HashMap<>();
    Robot bot = new Robot();
.....
public void nativeMousePressed(NativeMouseEvent e) {
                System.out.println("Mouse Pressed: " + e.getButton());
                try {

                    Long millis = 0L;
                    if (stopWatch.isRunning()) {
                        stopWatch.stop();
                        millis = stopWatch.getLastTaskTimeMillis();
                        System.out.println("time: " + stopWatch.getLastTaskTimeMillis());
                      
                    }
                     mappaMouse.put(millis, new Input((int)                   MouseInfo.getPointerInfo().getLocation().getX(), (int) MouseInfo.getPointerInfo().getLocation().getY(), "LM"));
                    stopWatch.start();
                    System.out.println("size "+ mappaMouse.size()+ "  mill0is "+ millis);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

System.out.println((int) MouseInfo.getPointerInfo().getLocation().getX() + " " + (int) MouseInfo.getPointerInfo().getLocation().getY());
    }

public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println("prova");
        if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
          // the idea was to add a shut down method here
        } else {
            System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
            try {

                patternExecutionProva(Integer.parseInt(NativeKeyEvent.getKeyText(e.getKeyCode())), mappaMouse);

            } catch (AWTException | IOException ex ) {
                ex.printStackTrace();
            } catch (NativeHookException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

public void patternExecutionProva(int tries, Map<Long, Input> mappa) throws AWTException, IOException, NativeHookException {
        

        System.out.println("Tri00988880es " + tries);
        if (tries == 0) {
            tries = 18000;
        }
            int finalTries = tries;
                Set<Long> chiaviMappa = mappa.keySet();
                try {
                    bot = new Robot();
                } catch (AWTException e) {
                    throw new RuntimeException(e);
                }
                for (int i = 0; i < finalTries; i++) {
                    for (Long millis : chiaviMappa) {
                        switch (mappa.get(millis).getInputType()) {
                            case "LM":
                                System.out.println("mouse");
                                bot.mouseMove(mappa.get(millis).getLocX(), mappa.get(millis).getLocY());
                                bot.delay(millis.intValue());
                                leftClick(1);
                                break;
                            case "KEY":
                                break;
                            default:
                                break;
                        }
                    }
                }
        } 

    public void leftClick(int times) {
        for (int i = 0; i < times; i++) {
            bot.delay(1000);
            bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        }
    }

I removed all instances of GlobalScreen.unregisterNativeHook();, so in theory when the loop starts it is still active, but nothing, no input get registered

I tried adding the GlobalScreen inside the Component, or inside the main. No changes.

I tried creating another thread using


        Thread loopThread = new Thread(() -> {
           .......
        });

        loopThread.start();

        try {
            loopThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

and nothing

Any help/suggestion willbe appreciated

0

There are 0 answers