如何从 Java 中的控制台读取单个字符(当用户键入它时)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1066318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a single char from the console in Java (as the user types it)?
提问by victor hugo
Is there an easy way to read a single char from the console as the user is typing it in Java? Is it possible? I've tried with these methods but they all wait for the user to press enterkey:
当用户在 Java 中输入字符时,是否有一种简单的方法可以从控制台读取单个字符?是否可以?我已经尝试过这些方法,但它们都在等待用户按下Enter键:
char tmp = (char) System.in.read();
char tmp = (char) new InputStreamReader(System.in).read ();
char tmp = (char) System.console().reader().read(); // Java 6
I'm starting to think that System.in is not aware of the user input until enteris pressed.
我开始认为 System.in 在按下Enter之前不知道用户输入。
采纳答案by Chris W. Rea
What you want to do is put the console into "raw" mode (line editing bypassed and no enter key required) as opposed to "cooked" mode (line editing with enter key required.) On UNIX systems, the 'stty' command can change modes.
您想要做的是将控制台置于“原始”模式(绕过行编辑并且不需要输入键)而不是“熟”模式(需要输入键的行编辑。)在 UNIX 系统上,'stty' 命令可以改变模式。
Now, with respect to Java... see Non blocking console input in Python and Java. Excerpt:
现在,关于 Java...请参阅Python 和 Java 中的非阻塞控制台输入。摘抄:
If your program must be console based, you have to switch your terminal out of line mode into character mode, and remember to restore it before your program quits. There is no portable way to do this across operating systems.
如果您的程序必须基于控制台,您必须将终端从行模式切换到字符模式,并记住在程序退出之前恢复它。没有可移植的方法来跨操作系统执行此操作。
One of the suggestions is to use JNI. Again, that's not very portable. Another suggestion at the end of the thread, and in common with the post above, is to look at using jCurses.
建议之一是使用 JNI。同样,这不是很便携。线程末尾的另一个建议,与上面的帖子一样,是查看使用jCurses。
回答by nes1983
You need to knock your console into raw mode. There is no built-in platform-independent way of getting there. jCursesmight be interesting, though.
您需要将控制台敲入原始模式。没有内置的独立于平台的方式到达那里。不过,jCurses可能很有趣。
On a Unix system, this might work:
在 Unix 系统上,这可能有效:
String[] cmd = {"/bin/sh", "-c", "stty raw </dev/tty"};
Runtime.getRuntime().exec(cmd).waitFor();
回答by rustyx
There is no portable way to read raw characters from a Java console.
没有可移植的方式从 Java 控制台读取原始字符。
Some platform-dependent workarounds have been presented above. But to be really portable, you'd have to abandon console mode and use a windowing mode, e.g. AWT or Swing.
上面已经介绍了一些依赖于平台的解决方法。但要真正实现便携,您必须放弃控制台模式并使用窗口模式,例如 AWT 或 Swing。
回答by Christian d'Heureuse
I have written a Java class RawConsoleInputthat uses JNAto call operating system functions of Windows and Unix/Linux.
我写了一个 Java 类RawConsoleInput,它使用JNA来调用 Windows 和 Unix/Linux 的操作系统函数。
- On Windows it uses
_kbhit()
and_getwch()
from msvcrt.dll. - On Unix it uses
tcsetattr()
to switch the console to non-canonical mode,System.in.available()
to check whether data is available andSystem.in.read()
to read bytes from the console. ACharsetDecoder
is used to convert bytes to characters.
- 在 Windows 上,它使用
_kbhit()
和_getwch()
来自 msvcrt.dll。 - 在 Unix 上,它用于
tcsetattr()
将控制台切换到非规范模式,System.in.available()
检查数据是否可用并System.in.read()
从控制台读取字节。ACharsetDecoder
用于将字节转换为字符。
It supports non-blocking input and mixing raw mode and normal line mode input.
它支持非阻塞输入和混合原始模式和普通线路模式输入。
回答by Pod
Use jline3:
使用jline3:
Example:
例子:
Terminal terminal = TerminalBuilder.builder()
.jna(true)
.system(true)
.build();
// raw mode means we get keypresses rather than line buffered input
terminal.enterRawMode();
reader = terminal .reader();
...
int read = reader.read();
....
reader.close();
terminal.close();