Detecting a Mouse Click Anywhere on Screen with Ja

2020-05-02 13:34发布

问题:

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

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.

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.

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.

回答1:

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.