使用 Java 检测鼠标点击屏幕上的任意位置
是否可以检测到应用程序之外屏幕上任意位置的鼠标单击?
我已经用 C# 编写了一个应用程序来执行此操作,但想用 Java 编写一个版本,以便它可以在多个平台上运行。
看起来我可以随时使用 java.awt.MouseInfo.getPointerInfo() 获取鼠标的坐标,但我不确定如何监听鼠标单击。
在 C# 中,我使用 GetAsyncKeyState
来检测鼠标按钮是否被单击,但如果我希望保持此“干净”以便在多个平台中使用,显然我不能使用它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能通过操作系统 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.