Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
freedom1b2830 opened this issue Jan 9, 2023 · 0 comments
Open

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

freedom1b2830 opened this issue Jan 9, 2023 · 0 comments

Comments

@freedom1b2830
Copy link

freedom1b2830 commented Jan 9, 2023

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();
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant