简体   繁体   English

识别Java Scanner或控制台应用程序中的箭头键

[英]Recognize arrow keys in Java Scanner or Console application

I am writing a Java application that uses both Scanner and Console to get input from the user. 我正在编写一个Java应用程序,它使用ScannerConsole来获取用户的输入。 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. 现在,当我使用ScannerConsole ,我得到奇怪的符号,如^[[A按箭头键时的^[[A

I have read about KeyListener and KeyEvent , but my application does not use a GUI. 我已经阅读了有关KeyListenerKeyEvent ,但我的应用程序没有使用GUI。

Thanks! 谢谢!

Good question - +1'd. 好问题 - + 1。 On Windows, I would SetConsoleMode to change the console to take raw input, but on *Nix it looks like there is more work involved. 在Windows上,我会使用SetConsoleMode来更改控制台以获取原始输入,但在* Nix上,它看起来更多涉及工作。 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/ http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM