Java - 在启动时响应式捕获 KeyEvent?
这是一个探索性问题,以确定我是否应该花时间来实施,如果它是一条“具有成本效益”的路径。我坦率地承认我还没有尝试自己实现这一点。我更多的是寻求理解和方向,而不是代码块。
我知道可以通过向接收焦点的对象添加 KeyListener
来捕获 Java 应用程序中的 KeyEvent
,但是在应用程序初始化的早期阶段可以实现这一点吗?
我正在考虑添加一些功能来修改初始化过程本身或在启动过程中按住按钮时显示的内容(例如按住 SHIFT 来重置或调试某些内容)。我没有过多地处理这些监听器(例如,我不熟悉 Swing 库中的哪些容器已经附加了它们)。我关心的是这种实施的时机和响应能力。或者,我已经在使用 Preferences
的实现,我可以再添加一个变量,但它没有相同的“实时”效果。
这是非常简单的,还是我需要关心初始化的顺序才能使其功能正常且响应灵敏?难道是我想太多了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GUI 必须可见并具有焦点才能开始接收事件。
您不应该使用 KeyListener。您应该使用按键绑定。或者可能是全局事件监听器。
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.
如果要在 Windows 上运行,并且您希望使用在 GUI 启动和运行之前响应用户输入的东西,甚至可能修改其加载,那么您可能需要使用可以直接与 GUI 交互的语言操作系统如C/C++。如果要在 Windows 环境中使用,另一种选择是使用 Windows 脚本语言,例如 AutoIt 免费提供。
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.