检测鼠标使用Java在屏幕上单击任意位置

检测鼠标使用Java在屏幕上单击任意位置

问题描述:

在我的应用程序之外,是否可以在屏幕上的任何位置检测鼠标单击?

Is it possible to detect a mouse click anywhere on a screen, outside of my application?

我已经写了一个应用程序在C#在Java中编写这个版本,以便它可以在多个平台上运行。

I have written an application to do this in C#, but would like to write a version of this in Java so that it can be run on several platforms.

看起来我可以随时获得鼠标的坐标 java.awt.MouseInfo.getPointerInfo()但我不知道如何监听鼠标点击。

It looks like I can get the co-ordinates of the mouse at any time with java.awt.MouseInfo.getPointerInfo() but I am not sure as to how to listen for a mouse click.

在C#中,我使用 GetAsyncKeyState 来检测鼠标按钮是否被点击,但显然我不能使用这个,如果我想保持这个干净在多个平台使用。

In C# I used GetAsyncKeyState to detect whether the mouse button was clicked, but obviously I cannot use this if I wish to keep this "clean" for use in multiple platforms.

您只能使用平台特定的操作系统API实现,因为您无法检测到程序外的点击

You can do this only with platform specific implementation of the OS API, as you can't detect clicks outside from your program in your program itself.

虽然你不会涉及编写平台特定的代码,只是抽象为一个接口,并适当使用不同的实现。

While you won't get around writing platform specific code, just abstract it as an interface and use different implementations appropiately.