java 鼠标捕获

发布于 2024-09-15 16:42:39 字数 97 浏览 7 评论 0原文

如何在 Java 应用程序中捕获鼠标,以便 Java 应用程序可以看到所有鼠标事件(甚至是鼠标移出应用程序窗口时发生的事件)?这类似于 Windows SetCapture 函数。

How do I capture the mouse in a Java application so that all mouse events (even ones that happen if the mouse is moved outside the app window) are seen by the Java app? This is like the Windows SetCapture function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清君侧 2024-09-22 16:42:39

你不知道; JVM(或更具体地说 AWT)仅在 Windows 向其发送输入事件时生成输入事件,并且 JVM 仅注册其窗口内发生的那些事件。

您也许能够使用 JNI 来实现这一目标,但您也可能无法实现 — 这将取决于您是否可以获得底层 API 所需的信息。由于这可能是一个窗口句柄,因此您将没有调用 API 所需的内容,即使是从 JNI 也是如此。

You don't; the JVM, or more specifically AWT, only generates input events when Windows sends it input events, and the JVM only registers for those events which occur within it's window.

You might be able to pull it off using JNI, but then again you might not - it will depend if you can get your hands on the information required by the underlying API. Since that's likely to be a window handle, you won't have what you need to invoke the API, even from JNI.

生死何惧 2024-09-22 16:42:39

您必须在操作系统级别挂钩鼠标。 Windows(Swing、AWT、MFC 等)仅感知其边界内的鼠标移动。如果您需要一种方法来访问鼠标的当前位置,无论鼠标位于屏幕上的哪个位置,您需要编写一个输入挂钩:输入挂钩。然后,您可以使用 JNI 或从 win32 控制台应用程序读取 STDOUT,该应用程序旨在使用输入挂钩将鼠标事件/位置转发到 Java 代码。我在一些用户界面测试用例中成功使用了后一种方法。

You have to hook the mouse at the operating system level. Windows(Swing, AWT, MFC, etc....) are only aware of mouse movements within their bounds. If you need a way to access the current position of the mouse regardless of where the mouse is on the screen, you need to write an Input Hook: Input Hooks. You can then use JNI or read the STDOUT from a win32 console application designed to use the Input Hook to forward mouse events/positions to your Java code. I use the latter method in some of my user interface test cases with success.

温柔女人霸气范 2024-09-22 16:42:39

我也需要这样做!

我在网上搜索后发现可以在java.awt.Robot中使用moveMouse。

基本上使用机器人将鼠标移动到框架的中心。如果用户移动它:检查移动量并将其移回中心。

为此不需要额外的数据包或 JNI(我的演示使用 JOGL 和 vecmath,但这是用于图形的)。够好吗?尝试演示,它在这里:

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

如果上述解决方案不够好,那么也许 lwjgl 就是您所需要的:

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

/Henrik Björkman

I needed to do that too!

I after searching the web I found that its possible to use the moveMouse in java.awt.Robot.

Basically use Robot to move the mouse into center of your frame. If user moves it: check how much and move it back to center.

No additional packets or JNI are needed for this (my demo uses JOGL and vecmath but that's for the graphics). Is it good enough? Try the demo, its here:

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

If the above solution is not good enough then perhaps lwjgl is what you need:

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

/Henrik Björkman

美人迟暮 2024-09-22 16:42:39

只需使用 gitHub 上提供的 system-hook 库 https://github.com/kristian/system-hook< /a>

这仅适用于基于 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(); }
    }
}

Just use the system-hook library available on gitHub https://github.com/kristian/system-hook

This only apply to windows-based systems but really simple to implement.

Sample usage

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(); }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文