简体   繁体   English

使用Java,如何在不使用GUI组件的情况下检测按键?

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

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? 使用Java,是否可以在不使用GUI元素的情况下检测用户操作,例如按键,鼠标按键和/或鼠标屏幕位置?

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. 我在这个问题上找到的唯一StackOverflow源是这个问题 ,没有答案。

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. 我在StackOverflow之外发现的唯一来源就是“Invisible GUI”解决方案,希望是我真正想要避免的。

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. 它可以使用JNI和/或JNA实现,但这不能真正称为“java实现”,因为您必须编写特定于平台的本机代码。

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 . 我尝试的替代解决方案是使用全屏透明的寡妇,它正在监听鼠标和键盘的所有事件,并使用类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. 它正在移动透明的Java窗口。

Use the java.awt.event.KeyListener class. 使用java.awt.event.KeyListener类。 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. 您必须编写三种方法,您可以在其中编写要检测的键,以及按下键时要发生的任何键。

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

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