简体   繁体   English

有没有简单的方法可以检测Java中键盘和鼠标按钮是否被按下并保持按住状态而无需关注应用程序?

[英]Is there simple way to detect keyboard and mouse button being pressed and held in Java without application focus?

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. 我正在尝试用Java编写非常简单的程序-当用户单击鼠标左键时,它将自动单击直到释放,但前提是之前使用键盘键切换过。 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? 附带说明-您是否正在为游戏创建宏?

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. 您可以下载jNativeHook https://code.google.com/p/jnativehook/,然后将全局侦听器连接到globalScreen,这样,您就不必关注应用程序了。

Then you can use it like normal swing/fx listener. 然后,您可以像普通的swing / fx监听器一样使用它。

CAUTION 警告

Make sure that you unhook it correctly otherwise after some hooks it might be blocked and you will have to restart the PC to hook it again (fixed in future release). 确保正确解开它,否则在经过一些钩子之后,它可能会被阻塞,并且您将不得不重新启动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. 机器人还会在某些游戏中无法执行事件。

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

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