是否可以让 MouseMotionListener 侦听所有系统鼠标移动事件?

发布于 2024-07-13 06:15:03 字数 602 浏览 2 评论 0原文

我的样板监听器:

class MyMouseMotionListener implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
    System.out.println("Dragged...");
}

public void mouseMoved(MouseEvent e) {
    System.out.println("Moved...");
}}

足够简单,但是我应该将它添加到什么中才能监听系统范围的事件? 我一直在研究 GraphicsDevice 和 AccessibleContext 子类之类的东西——它们不直接提供 MouseMotionListeners 的添加,但我希望它们能给我一些关于如何实现它的想法。

编辑:这根本不是基于事件的,但我发现了这一点:

MouseInfo.getPointerInfo().getLocation()

实际上返回我的应用程序上下文之外的鼠标位置,即使应用程序本身没有焦点。 有什么方法可以观察它并在其值发生变化时调度一个事件吗?

My boilerplate listener:

class MyMouseMotionListener implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
    System.out.println("Dragged...");
}

public void mouseMoved(MouseEvent e) {
    System.out.println("Moved...");
}}

Simple enough, but what do I add it to in order to listen to system-wide events? I've been researching are things like the GraphicsDevice and AccessibleContext subclasses -- they don't offer the addition of MouseMotionListeners directly but I was hoping they might give me some idea as to how I could implement this.

Edit: This isn't at all event-based but I've found this:

MouseInfo.getPointerInfo().getLocation()

Does actually return the mouse position outside the context of my app, even when the app itself does not have focus. Is there any way to observe this and dispatch an event if its value has changed?

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

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

发布评论

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

评论(5

无言温柔 2024-07-20 06:15:03

您可以使用 Toolkit.addAWTEventListener(AWTEventListener 侦听器,long eventMask)eventMask 参数确定侦听器将接收哪些事件。

所以你的代码看起来像:

Toolkit.getDefaultToolkit().addAWTEventListener(new MyMouseMotionListener(), AWTEvent.MOUSE_MOTION_EVENT_MASK);

You can subscribe to all mouse events within your Java container hierarchy using Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask). The eventMask parameter determines which events the listener will receive.

So your code would look something like :

Toolkit.getDefaultToolkit().addAWTEventListener(new MyMouseMotionListener(), AWTEvent.MOUSE_MOTION_EVENT_MASK);

秋风の叶未落 2024-07-20 06:15:03

更新:您可以轮询 MouseInfo 的位置,但您永远不会获得按钮状态。 您将需要使用本机代码来获取按钮状态。

我认为不使用本机代码就没有任何方法可以在应用程序的容器层次结构之外监听鼠标光标。

UPDATE: You could poll MouseInfo for position but you'll never get button state. You will need to use native code to get button state.

I do not think there is any way without using native code to listen to the mouse cursor outside of the cotainer hierarchy of your application.

万人眼中万个我 2024-07-20 06:15:03

我通过使用上述功能根据请求获取鼠标位置解决了同样的问题。 然后,我启动了一个新线程,在程序的其余执行过程中连续执行此操作。

<块引用>

MouseInfo.getPointerInfo().getLocation()

另外,我必须使主类扩展 Thread,因此

public class MouseMotion extends Thread {

这要求您创建一个名为 run 的函数。
在您的 void 函数中,只需创建一个无限循环,

public void run() {
int n=10;
for (int i=0;i<n; n++) //horrible infinite loop
{
    Thread.sleep(100); //this will slow the capture rate to 0.1 seconds
    PointerInfo a = MouseInfo.getPointerInfo();
    Point p = new Point (0,0);
    a = MouseInfo.getPointerInfo();
    p = a.getLocation();
    int x = (int)p.getX(); //getX and getY return doubles so typecast
    int y = (int)p.getY();
    System.out.println(""+x+"   "+y);   //to see it grabing locations not needed
}
}

现在剩下的就是当您想要开始观察鼠标运动时调用线程。 我在主线程之后立即启动线程

public static main (String[] args) throws Exception {
Thread thread = new MouseMotion();
thread.start();
...}

I solved the same issue by using the above mentioned ability to get the mouse position upon request. I then launched a new thread to do this continuously durring the rest of the programs execution.

MouseInfo.getPointerInfo().getLocation()

Also I had to make the main class extend Thread thus

public class MouseMotion extends Thread {

This requies you to make a function called run.
In your void function simply create an infinite loop

public void run() {
int n=10;
for (int i=0;i<n; n++) //horrible infinite loop
{
    Thread.sleep(100); //this will slow the capture rate to 0.1 seconds
    PointerInfo a = MouseInfo.getPointerInfo();
    Point p = new Point (0,0);
    a = MouseInfo.getPointerInfo();
    p = a.getLocation();
    int x = (int)p.getX(); //getX and getY return doubles so typecast
    int y = (int)p.getY();
    System.out.println(""+x+"   "+y);   //to see it grabing locations not needed
}
}

All that is left now is to call the thread when you wana start watching your mouse motion. I start the thread right after my main as such

public static main (String[] args) throws Exception {
Thread thread = new MouseMotion();
thread.start();
...}
甜中书 2024-07-20 06:15:03

如果您想侦听/捕获系统上的所有鼠标事件(例如,不仅仅是您的应用程序窗口),您将需要一个 鼠标挂钩

If you want to listen/capture all mouse events on the system (as in, not just your application window), you'll need a mouse hook.

原来是傀儡 2024-07-20 06:15:03

有一些库可以实现此目的,我经常在应用程序中使用其中一个库。

JNativeHook 具有处理本机鼠标和键盘事件的卓越能力。 (谷歌它,我懒得去颠覆。虽然你可以下载这个库,并且在你对库进行两次调用后它的工作方式就像一个常规的鼠标事件。

我希望当我在谷歌上冲浪时有人会发布我一直在这样的线程上使用这个库,但我不是注册会员,因为我从不公开寻求帮助。

There are a few libraries for this, one of which I use on a regular basis for applications.

JNativeHook has exceptional ability to handle both native mouse and keyboard events. (google it I am too lazy to go to the subversion. Although you can just download the library, and it works just like a regular mouse event after you make two calls to the library.

I wish when I was surfing google someone would have posted this library on a thread like this. I use stackoverflow all the time but I'm not a registered member, because I never ask for help publicly.

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