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

落花浮王杯 提交于 2019-12-24 21:51:06

问题


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?


回答1:


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.

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

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).

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.



来源:https://stackoverflow.com/questions/24810859/is-there-simple-way-to-detect-keyboard-and-mouse-button-being-pressed-and-held-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!