Java mouse capture

How to capture a mouse in a Java application so that all mouse events (even those that occur if the mouse moves outside the application window) are visible to the Java application? This is similar to the Windows SetCapture feature.

+3
source share
4 answers

You do not do this; The JVM, or more specifically AWT, only generates input events when Windows sends its input events, and the JVM only logs events that occur in it.

You may be able to disable it using JNI, but again, perhaps not - it will depend on whether you can get the information required by the underlying API. Since this is likely to be a window handle, you will not have what you need to call the API, even from JNI.

+3
source

. Windows (Swing, AWT, MFC ..) . , , , Input Hook: . JNI STDOUT win32, Input Hook / Java. .

+1

!

, moveMouse java.awt.Robot.

Robot . : , .

JNI ( JOGL vecmath, ). ? , :

http://www.eit.se/hb/misc/java/examples/FirstPersonJavaProtoGame/

, , , lwjgl - , :

http://www.lwjgl.org/javadoc/org/lwjgl/input/Mouse.html

/

0

, gitHub https://github.com/kristian/system-hook

Windows, .

import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
import lc.kra.system.keyboard.event.GlobalKeyEvent;

public class GlobalKeyboardExample {
    private static boolean run = true;
    public static void main(String[] args) {
        // might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails 
        GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();

        System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown.");
        keyboardHook.addKeyListener(new GlobalKeyAdapter() {
            @Override public void keyPressed(GlobalKeyEvent event) {
                System.out.println(event);
                if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE)
                    run = false;
            }
            @Override public void keyReleased(GlobalKeyEvent event) {
                System.out.println(event); }
        });

        try {
            while(run) Thread.sleep(128);
        } catch(InterruptedException e) { /* nothing to do here */ }
          finally { keyboardHook.shutdownHook(); }
    }
}
0

Source: https://habr.com/ru/post/1761630/