Java 是否可以使用Robot模拟真实的点击,而不影响程序(自动点击器)本身?

Java 是否可以使用Robot模拟真实的点击,而不影响程序(自动点击器)本身?,java,debugging,Java,Debugging,我的目标是使用JNativehook制作一个工作正常的自动点击器。我试着让它成为这样,每当你按下鼠标按钮1,它就会打开自动点击器,当你松开它,自动点击器就会停止。当我运行它时,自动点击器不会停止,因为它模拟了现实生活中的点击,正因为如此,按键保持方法会打开自动点击器,因此无法停止。我试着在拿着这个的时候用另一个钥匙扣,但我想他们的钥匙扣应该是另一种方式 第一类: package com.ac.main; import java.awt.AWTException; import java.awt

我的目标是使用JNativehook制作一个工作正常的自动点击器。我试着让它成为这样,每当你按下鼠标按钮1,它就会打开自动点击器,当你松开它,自动点击器就会停止。当我运行它时,自动点击器不会停止,因为它模拟了现实生活中的点击,正因为如此,按键保持方法会打开自动点击器,因此无法停止。我试着在拿着这个的时候用另一个钥匙扣,但我想他们的钥匙扣应该是另一种方式

第一类:

package com.ac.main;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.util.concurrent.ThreadLocalRandom;

import javax.swing.Timer;

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 com.cookie.clicker.GlobalKeyListenerExample;
public class GlobalMouseListenerExample implements NativeMouseInputListener, NativeKeyListener {

public static Timer t;
static int cps = 100;
public void nativeMouseClicked(NativeMouseEvent e) {
}


public void nativeMousePressed(NativeMouseEvent e) {

    
    t = new Timer(cps,(ActionEvent e1)->{
        cps = ThreadLocalRandom.current().nextInt(80, 140 + 1);
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    });     
    t.start();
    
}

public void nativeMouseReleased(NativeMouseEvent e) {

    t.stop();
    
}   

public void nativeMouseMoved(NativeMouseEvent e) {
}

public void nativeMouseDragged(NativeMouseEvent e) {
}



@Override
public void nativeKeyPressed(NativeKeyEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void nativeKeyReleased(NativeKeyEvent arg0) {
    // TODO Auto-generated method stub
    
} 

@Override
public void nativeKeyTyped(NativeKeyEvent arg0) {
    // TODO Auto-generated method stub
    
}



public static void main(String[] args) {
    GlobalMouseListenerExample e2 = new GlobalMouseListenerExample();
    e2.run();
}






public void run() {
    GlobalKeyMouseListenerExample.run();
    GlobalKeyListenerExample c = new GlobalKeyListenerExample();
    c.run();
    try {
        GlobalScreen.registerNativeHook();
    }
    catch (NativeHookException ex) {

        System.exit(1);
    }

    // Construct the example object.
    GlobalMouseListenerExample example = new GlobalMouseListenerExample();

    // Add the appropriate listeners.
    GlobalScreen.addNativeMouseListener(example);
    GlobalScreen.addNativeMouseMotionListener(example);
    
    GlobalScreen.addNativeKeyListener(new GlobalMouseListenerExample());
}

public static void tStart() {
    t.start();
}
public static void tStop() {
    t.stop();
}
public static void start() {
    t = new Timer(10,(ActionEvent e)->{
        System.out.println("hello");
    });
}



}
第二类:

package com.ac.main;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyMouseListenerExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

    if (e.getKeyCode() == NativeKeyEvent.VC_HOME) {
        System.exit(0);
    } 
}

public void nativeKeyReleased(NativeKeyEvent e) {
    System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}

public void nativeKeyTyped(NativeKeyEvent e) {
    System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}

public static void main(String[] args) {
    GlobalKeyMouseListenerExample c = new GlobalKeyMouseListenerExample();
    c.run();
}

public static void run() {
    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);
    }

    GlobalScreen.addNativeKeyListener(new GlobalKeyMouseListenerExample());
}
}