简体   繁体   中英

Java: Thread.sleep mousepress delay

I've been trying to create an autoclicker in java using jnativehook. It works fine, even compiles and runs. My problem is using Thread.sleep to try and add a delay between clicks:

bot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
bot.mouseRelease(InputEvent.BUTTON1_MASK);

If I were to input a delay of 0 it will function fine. But given no delay it will click too fast. When I add a delay it will click fine but, when I release the trigger key it will keep clicking for a few seconds given the time it's been clicking. A delay of 0 will not do this however.

Full code:

public class App implements NativeKeyListener{

private JPanel panel1;
private JTabbedPane tabbedPane1;
private JButton spoilerButton;
private JSlider slider1;
private JSlider slider2;

//Removed irrelevant code...

static Robot bot;

static {
    try {
        bot = new Robot();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

static boolean pressed;

public void click() throws InterruptedException {

    try {

        bot.mousePress(InputEvent.BUTTON1_MASK);
            Thread.sleep(50);
        bot.mouseRelease(InputEvent.BUTTON1_MASK);

    } catch (Exception e) {
        e.printStackTrace();
    }}

@Override
public void nativeKeyPressed(NativeKeyEvent e) {
    if (NativeKeyEvent.getKeyText(e.getKeyCode()) == "Delete") {
        pressed = true;
        while (pressed){
            try {
                click();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }


    }}

@Override
public void nativeKeyReleased(NativeKeyEvent e) {
    if (NativeKeyEvent.getKeyText(e.getKeyCode())=="Delete"){
        pressed=false;
    }}

@Override
public void nativeKeyTyped(NativeKeyEvent e) {

}

public static void main(String[] args) {

    JFrame frame = new JFrame("Autoclicker");
    frame.setContentPane(new App().panel1);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setSize(400,148);
    frame.setResizable(false);

    try{GlobalScreen.registerNativeHook();
    } catch (Exception e){
                System.exit(1);}
    GlobalScreen.addNativeKeyListener(new App());

}
} 

Looks like the method nativeKeyPressed() gets called continuously when you keep pressing the trigger key. This results in several calls to click() method (assuming it's multi threaded) and due to the sleep() between mouse press and release (mouse click is complete when released) this can happen.

Hence you can try two options depending on the root cause:
1. move the sleep() call after mouse released. If the nativeKeyPressed gets called concurrently this won't work.
2. Use a different thread to execute the click() method when trigger is pressed. In this case, you may need to submit a Runnable object to your thread each time with the 'pressed' check and click() call in it. This will ensure it won't run after pressed becomes false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM