kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.75k stars 347 forks source link

[sometimes works][java17] [windows 10pro 21h2 19044.2364] #432

Open freedom1b2830 opened 1 year ago

freedom1b2830 commented 1 year ago

on windows the code often does not work

what should have worked: z clamps LMB x often clicks LMB

Works great on linux Linux archlinux 6.1.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 04 Jan 2023 16:28:15 +0000 x86_64 GNU/Linux

 <dependency>
      <groupId>com.1stleg</groupId>
      <artifactId>jnativehook</artifactId>
      <version>2.1.0</version>
 </dependency>
import java.awt.AWTException;
import java.awt.Robot;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

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

public class KC extends JFrame implements NativeKeyListener {
    private static final long serialVersionUID = 6184514499310277830L;
    private static Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    static Robot robot;
    static boolean presed1;
    static boolean presed2;
    static boolean presed3;

    private static void event() {
        if (!presed1) {
            mouseDOWN();
            presed1 = true;
            return;
        }
        mouseUP();
        presed1 = false;
    }

    private static void event2() {
        if (presed2) {
            if (!presed3) {
                mouseDOWN();
                presed3 = true;
                return;
            }
            mouseUP();
            presed3 = false;
        }
    }

    public static void main(final String[] args) {
        try {
            robot = new Robot();
            GlobalScreen.registerNativeHook();
            logger.setLevel(Level.WARNING);
            logger.setUseParentHandlers(false);
            GlobalScreen.addNativeKeyListener(new KC());
            new Thread(() -> {
                while (true) {
                    event2();
                    try {
                        Thread.sleep(120L);
                    } catch (final InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } catch (NativeHookException | AWTException e) {
            e.printStackTrace();
        }
    }

    private static void mouseDOWN() {
        robot.mousePress(NativeInputEvent.BUTTON3_MASK);
    }

    private static void mouseUP() {
        robot.mouseRelease(NativeInputEvent.BUTTON3_MASK);
    }

    private void eventPrev() {
        if (presed2) {
            presed2 = false;
            mouseDOWN();
            return;
        }
        presed2 = true;
        mouseUP();
    }

    public @Override void nativeKeyPressed(final NativeKeyEvent e) {
    }

    public @Override void nativeKeyReleased(final NativeKeyEvent e) {
    }

    public @Override void nativeKeyTyped(final NativeKeyEvent e) {
        final var charr = e.getKeyChar();
        if (charr == 'z' || charr == 'Z') {
            event();
        }
        if (charr == 'x' || charr == 'X') {
            eventPrev();
        }
    }
}