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?
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 (e.g. holding SHIFT to reset or debug something). 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). 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.
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.
You should not be using a 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++. 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.
精彩评论