Using Java, How to detect keypresses without using GUI components?

 ̄綄美尐妖づ 提交于 2020-01-14 17:51:27

问题


Using Java, is it possible to detect user actions, such as key-presses, mouse-button-presses, and/or mouse's screen location, without the use of GUI elements?

If it is, how could I implement it?

Otherwise, what are the alternatives?


The only StackOverflow source i found on this matter is this question, which is unanswered.

And the only sources I found outside StackOverflow on this matter point to an "Invisible GUI" solution, wish is something i really want to avoid.


回答1:


It can be implemented using JNI and/or JNA but this cannot be really called "java implementation" because you will have to write platform specific native code.

Alternative solution I tried is to use full screen transparent widow that is listening to all events of mouse and keyboard and "forwards" them to the real application using class Robot. I tried this approach. It works well with one limitation: there is a problem to support "mouse over" events of applications: the mouse is not moving over the real application. It is moving over the transparent java window.




回答2:


Use the java.awt.event.KeyListener class. You will have to write three methods in which you can write which key you want to be detected, and whatever you want to have happen when the key is pressed.



来源:https://stackoverflow.com/questions/11673782/using-java-how-to-detect-keypresses-without-using-gui-components

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