命令行中的 Java 键侦听器 [英] Java key listener in Commandline
问题描述
大多数演示在 Swing 中显示 keyevent,命令行中的等效项是什么?
Most demo showing keyevent in Swing, what is the equivalent in commandline?
推荐答案
Swing 与命令行环境不同,因为控制台窗口中没有事件.标准 GUI 处理对象和事件.控制台没有这样的等效概念.
Swing is different from a command line environment in the sense that you have no events in a console window. A standard GUI deals with objects and events. A console has no such equivalent notion.
您所做的是一个标准输入(以及一个标准输出),您可以从中读取.请参阅 this question 关于如何从控制台读取单个字符(无需等待换行符) - 或者更确切地说,关于在 Java 中如何做到这一点并不容易.
What you do have is a standard input (as well as a standard output), which you can read from. See this question on how to read a single char from console (without waiting for a newline) - or rather, on how this isn't very easy to do in Java.
当然,您始终可以在单独的线程上异步读取.即主线程将继续做事,侦听器线程等待 I/O 阻塞调用.但这只能在应用层面实现和处理.
Of course, you can always do the reading asynchronously on a separate thread. i.e. the main thread will keep doing stuff, with a listener thread waiting on the I/O blocking call. But this can only be implemented and handled on the application level.
这篇关于命令行中的 Java 键侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!