有没有简单的方法可以检测Java中键盘和鼠标按钮是否被按下并保持按住状态而无需关注应用程序? [英] Is there simple way to detect keyboard and mouse button being pressed and held in Java without application focus?

查看:491
本文介绍了有没有简单的方法可以检测Java中键盘和鼠标按钮是否被按下并保持按住状态而无需关注应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java编写非常简单的程序-当用户单击鼠标左键时,它将自动单击直到释放,但前提是之前使用键盘键进行了切换.我对这个自动触发部分没有问题,但是当我的应用程序没有焦点时,我不知道如何检测键盘按下和鼠标按钮按下.有一些简单的方法可以做到吗?

I'm trying to write quite simple program in Java - when user click left mouse button it will autoclick until released, but only if toggled before with keyboard key. I have no problem with this autofire part, but I dont know how to detect keyboard press, and mouse button press when my aplication doesn't have focus. Is there some simply way to do it?

推荐答案

附带说明-您是否正在为游戏创建宏?

On a side note - Are you creating macro for game?

您可以下载jNativeHook https://code.google.com/p/jnativehook/并将全局侦听器挂接到globalScreen上,这样就不必关注应用程序了.

You can download jNativeHook https://code.google.com/p/jnativehook/ and hook global listener to globalScreen that way it doesn't matter if you have focus over application.

然后,您可以像普通的swing/fx侦听器一样使用它.

Then you can use it like normal swing/fx listener.

请确保您正确地将其解钩,否则在进行一些钩后,它可能会被阻塞,并且您必须重新启动PC才能再次对其进行钩(在将来的版本中已修复).

如果您正在为游戏制作宏,则至少从我个人的经验来看,在专注于窗口时可能会遇到问题-一些全屏游戏,甚至在窗口模式下也是如此.机器人还会在某些游戏中无法执行事件.

If you are doing a macro for a game you might have a problem when focused on window - some fullscreen games and even in windowed mode, at least from my personal experience. Robot will also fail in some games to execute event.

这篇关于有没有简单的方法可以检测Java中键盘和鼠标按钮是否被按下并保持按住状态而无需关注应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆