是否可以让 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 的添加,但我希望它们能给我一些关于如何实现它的想法。
编辑:这根本不是基于事件的,但我发现了这一点:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
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)
. TheeventMask
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);
更新:您可以轮询 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.
我通过使用上述功能根据请求获取鼠标位置解决了同样的问题。 然后,我启动了一个新线程,在程序的其余执行过程中连续执行此操作。
另外,我必须使主类扩展 Thread,因此
这要求您创建一个名为 run 的函数。
在您的 void 函数中,只需创建一个无限循环,
现在剩下的就是当您想要开始观察鼠标运动时调用线程。 我在主线程之后立即启动线程
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.
Also I had to make the main class extend Thread thus
This requies you to make a function called run.
In your void function simply create an infinite loop
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
如果您想侦听/捕获系统上的所有鼠标事件(例如,不仅仅是您的应用程序窗口),您将需要一个 鼠标挂钩。
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.
有一些库可以实现此目的,我经常在应用程序中使用其中一个库。
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.