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

检测鼠标使用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.

这篇关于检测鼠标使用Java在屏幕上单击任意位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:12