如何在 Clojure 中等待按键
我想在用户按下某个键时跳出循环。
在 C 中我会使用 kbhit()。有 Clojure(或 Java)等效的吗?
I'd like to break out of a loop when the user presses a key.
In C I'd use kbhit(). Is there a Clojure (or Java) equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找 Java 中(Linux?)控制台中按键的非阻塞处理。 早期问题建议使用两个 Java 库来实现此功能。如果不需要便携,有一个解决方案 这里。
基本上,
可以工作,但(在 Linux 上)只有在按“return”后才能工作,因为控制台输入流是缓冲的,这是由操作系统决定的。这意味着您无法通过使用 Channels 或任何其他 NIO 类来克服这个问题。为了确保控制台刷新每个字符,您需要修改终端设置。我曾经编写过一个 C 程序来执行此操作(修改当前终端的 termios 结构的 ICANON 标志),但我不知道如何从 Java 中执行此操作(但请参阅 第二个链接)。
一般来说,您可以通过搜索找到有关此问题的更多信息'java非阻塞输入'。
You're looking for nonblocking handling of a key press in a (Linux?) console in Java. An earlier question suggested two Java libraries that might enable this. If it doesn't need to be portable, there is a solution here.
Basically,
works, but (on Linux) only after pressing 'return', because the console inputstream is buffered and that is decided by the OS. This means that you can't overcome that by using Channels or any other NIO class. To make sure the console flushes each and every character, you need to modify the terminal settings. I once wrote a C program that does that (modify the ICANON flag of the termios struct of the current terminal), but I don't know how to do that from Java (but see the second link).
In general, you can find some more in this issue by searching for 'java nonblocking input'.