问题描述
我正在编写同时使用Scanner
和Console
来获取用户输入的Java应用程序.我想添加命令历史记录支持,以便用户可以使用箭头键搜索以前的输入(类似于终端).有没有办法做到这一点?现在,当我使用Scanner
或Console
时,按箭头键时会出现类似^[[A
的奇怪符号.
I am writing a Java application that uses both Scanner
and Console
to get input from the user. I would like to add command history support so that the user can use the ARROW KEYS to search previous inputs (similar to a terminal). Is there a way to do this? Right now, when I use either the Scanner
or the Console
, I get weird symbols like ^[[A
when pressing the arrow keys.
我已经阅读了有关KeyListener
和KeyEvent
的信息,但是我的应用程序未使用GUI.
I have read about KeyListener
and KeyEvent
, but my application does not use a GUI.
谢谢!
推荐答案
好问题-+1.在Windows上,我将使用SetConsoleMode更改控制台以获取原始输入,但是在* Nix上,似乎涉及更多的工作.不幸的是,我现在没有任何代码可以显示给您,但是请看一下此链接,看看是否有帮助.
Good question - +1'd. On Windows, I would SetConsoleMode to change the console to take raw input, but on *Nix it looks like there is more work involved. Unfortunately I don't have any code that I can show you right now, but have a look at this link and see if it helps.
http://www.darkcoding.net /software/non-blocking-console-io-is-not-possible/
这篇关于识别Java扫描仪或控制台应用程序中的箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!