0

I'm trying to make a boolean true while I hold left click and make it false when I don't, I'm trying to use "Jnativehook" Mouse Listener" (https://github.com/kwhat/jnativehook/wiki/Mouse) but the boolean isn't changing.

Code:

package me.ordinals;

import org.jnativehook.mouse.*;

import java.awt.event.InputEvent;

public class mouseHandler implements NativeMouseListener {
    @Override
    public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent) {
    }

    @Override
    public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
        if (nativeMouseEvent.getButton() == InputEvent.BUTTON1_DOWN_MASK) {
            ac.getInstance().setToggled(true);
        }
    }

    @Override
    public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
        if (nativeMouseEvent.getButton() == InputEvent.BUTTON1_DOWN_MASK) {
            ac.getInstance().setToggled(false);
        }
    }
}
xyz
  • 15
  • 1
  • 5

1 Answers1

0

You're using the wrong constants here:

if (nativeMouseEvent.getButton() == InputEvent.BUTTON1_DOWN_MASK) {

If you look at the NativeMouseEvent API, getButton() will return 1 if button 1 is pressed:

/** Indicates mouse button #1; used by getButton(). */
public static final int BUTTON1                 = 1;    

You're using the java.util.InputEvent constants, whose value is 1024, and not using the correct one even if this were a Swing GUI. So change to

if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1) {

Same for your other expressions.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hey, I'm getting an error with setting the boolean. Error: Exception in thread "JNativeHook Dispatch Thread" java.lang.NullPointerException at me.ordinals.mouseHandler.nativeMousePressed(mouseHandler.java:14) at org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(Unknown Source) at org.jnativehook.GlobalScreen$EventDispatchTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) – xyz Nov 01 '18 at 19:49
  • @xyz: That looks to be a completely different problem. Do you know how to debug NullPointerExceptions (NPEs) in general? – DontKnowMuchBut Getting Better Nov 01 '18 at 19:50
  • @xyz: check [here](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – DontKnowMuchBut Getting Better Nov 01 '18 at 19:53
  • @xyz: although to be honest, knowing how to debug NPE's is part of the fundamental knowledge base of Java programming, and it might be frustrating to try to do advanced projects, such as directly interacting with the OS through outside libraries, before being familiar with Java fundamentals. Regardless, good luck. – DontKnowMuchBut Getting Better Nov 01 '18 at 19:56