1

Basically, I want to query if any mouse button is being pressed and if so, which one. The problem is that I don't use a (constantly focused) UI environment. It is meant to be able to run in the background while the OS is focused on another window. I just have a Swing GUI set up for easy controlling.

How could I do this?

(By the way, I am trying to query it inside of a loop, so setting up an event listener wouldn't be efficient.)

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Orbyfied
  • 94
  • 11
  • i wanted to explain that im trying to detect **or** a mouse button is being pressed, **and which** one is being pressed. fixed it – Orbyfied Dec 17 '20 at 18:21
  • fixed it sorry english is not my native language – Orbyfied Dec 17 '20 at 18:22
  • 2
    *It is meant to be able to run in the background while the OS is focused on another window.* - Swing doesn't work that way. Swing can only handle events when the event is generated on the focused Swing window. If you want to listen for OS events you need to use JNI or JNA (I don't know the difference). – camickr Dec 17 '20 at 19:01
  • i know, i believe i also stated that in the question. the swing ui isnt meant to handle the querying or events. – Orbyfied Dec 17 '20 at 19:38
  • Java is not the language of choice when trying to monitor OS events. Java is meant to be OS agnostic. – Gilbert Le Blanc Dec 18 '20 at 12:43

1 Answers1

0

As mentioned by others you would need to use JNA in order to hook into the operating systems native APIs. Lucky for you there is a great library that does just that jnativehook.

Here is some demo code which creates a Global Mouse Listener:

import GlobalScreen;
import NativeHookException;
import NativeMouseEvent;
import NativeMouseInputListener;

public class GlobalMouseListenerExample implements NativeMouseInputListener {
    public void nativeMouseClicked(NativeMouseEvent e) {
        System.out.println("Mouse Clicked: " + e.getClickCount());
    }

    public void nativeMousePressed(NativeMouseEvent e) {
        System.out.println("Mouse Pressed: " + e.getButton());
    }

    public void nativeMouseReleased(NativeMouseEvent e) {
        System.out.println("Mouse Released: " + e.getButton());
    }

    public void nativeMouseMoved(NativeMouseEvent e) {
        System.out.println("Mouse Moved: " + e.getX() + ", " + e.getY());
    }

    public void nativeMouseDragged(NativeMouseEvent e) {
        System.out.println("Mouse Dragged: " + e.getX() + ", " + e.getY());
    }

    public static void main(String[] args) {
        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);
        }

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

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

Also don't forget to read about thread safety when Working with Swing using the library mentioned.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138