简体   繁体   English

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

[英]Detecting a Mouse Click Anywhere on Screen with Java

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. 我已经用C#编写了一个应用程序来执行此操作,但是我想用Java编写一个该程序的版本,以便可以在多个平台上运行。

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. 看起来我可以随时使用java.awt.MouseInfo.getPointerInfo()获得鼠标的坐标,但是我不确定如何监听鼠标的单击。

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. 在C#中,我使用GetAsyncKeyState来检测是否单击了鼠标按钮,但是很显然,如果我希望保持“干净”以便在多个平台上使用,就不能使用它。

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. 您只能使用OS API的特定于平台的实现来执行此操作,因为您无法在程序本身中检测到程序外部的点击。

While you won't get around writing platform specific code, just abstract it as an interface and use different implementations appropiately. 虽然您不会无所顾忌地编写平台特定的代码,但是只需将其抽象为接口并适当地使用不同的实现即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM