简体   繁体   English

Java-启动时响应捕获KeyEvent?

[英]Java - Capture KeyEvent responsively on startup?

This is an exploratory question to determine if I should dedicate time to an implementation, if it's a "cost-effective" path. 这是一个探索性问题,以确定我是否应该将时间专用于实现,如果这是一条“具有成本效益”的道路。 I freely admit I haven't attempted to implement this myself yet. 我自由地承认我还没有尝试自己实现这一点。 I'm looking more for understanding and direction than a code block. 除了代码块之外,我还需要更多的理解和指导。

I know it's possible to capture a KeyEvent in a Java application by adding a KeyListener to something that receives focus, but how early in the application initialization can this be feasibly implemented? 我知道可以通过将KeyListener添加到获得焦点的对象中来捕获Java应用程序中的KeyEvent ,但是可以在应用程序初始化的早期实现这种方法吗?

I'm thinking of adding some functionality that modifies the initialization process itself or the contents displayed when a button is held down during launch (eg holding SHIFT to reset or debug something). 我正在考虑添加一些功能,这些功能可以修改初始化过程本身或在启动过程中按住按钮时显示的内容(例如,按住SHIFT进行重置或调试)。 I haven't dealt with these listeners too much (and thus I'm not familiar with which Containers in the Swing library already have them attached, for example). 我没有过多地处理这些侦听器(例如,我不熟悉Swing库中的哪些容器已经附加了它们)。 My concern is the timing and responsiveness of such an implementation. 我担心的是这种实施的时机和响应能力。 Alternatively, I am already using an implementation of Preferences that I can just add one more variable to, but it doesn't have the same "real-time" effect. 另外,我已经在使用Preferences的实现,可以向其中添加另一个变量,但是它没有相同的“实时”效果。

Is this pretty straight-forward, or will I need to concern myself with the order in which things are initialized in order to make this functional and responsive? 这是简单明了吗,还是我需要关注事物初始化的顺序才能使此功能和响应速度更快? Am I just over-thinking this? 我只是在想这个吗?

The GUI must be visible and have focus in order to start receiving events. GUI必须可见并且具有焦点才能开始接收事件。

You should not be using a KeyListener. 您不应该使用KeyListener。 You should be using Key Bindings . 您应该使用键绑定 Or maybe a Global Event Listener . 或者,也许是一个全球事件监听器

If this is to be run on Windows, and you want to use something that responds to user input before the GUI is up and running and perhaps even modify its loading, then you'll likely need to use a language that can interact directly with the OS such as C/C++. 如果此操作要在Windows上运行,并且您想使用一些能够在GUI启动和运行之前响应用户输入的东西,甚至可以修改其加载,那么您可能需要使用一种可以直接与GUI交互的语言。操作系统,例如C / C ++。 Another option, if this is to be used in a Windows environment is to use a Windows scripting language for this such as AutoIt which is available free. 如果要在Windows环境中使用,则另一个选择是为此使用Windows脚本语言,例如AutoIt ,它是免费提供的。

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

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