是否有可能有一个的MouseMotionListener听取各系统的鼠标移动事件? [英] Is it possible to have a MouseMotionListener listen to all system mouse motion events?

查看:157
本文介绍了是否有可能有一个的MouseMotionListener听取各系统的鼠标移动事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的样板监听器:

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的,但我希望他们给我一些想法如何,我可以实现这个

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.

编辑:这是不是在所有基于事件的,但我发现这一点:

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?

推荐答案

是的;使用 Toolkit.addAWTEventListener(新MyMouseMotionListener() AWTEvent.MOUSE_MOTION_EVENT_MASK );

请参阅的http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#addAWTEventListener(java.awt.event.AWTEventListener,长)(我不能得到的链接工作,所以你必须复制和粘贴)。

See http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#addAWTEventListener(java.awt.event.AWTEventListener, long) (I can't get the link to work, so you'll have to copy and paste it).

请注意:如basszero说,这仅适用于您的容器层次内

Note: as basszero said, this only works inside your container hierarchy.

这篇关于是否有可能有一个的MouseMotionListener听取各系统的鼠标移动事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆