Recognize arrow keys in Java Scanner or Console application

送分小仙女□ 提交于 2021-02-18 02:48:38

问题


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.

I have read about KeyListener and KeyEvent, but my application does not use a GUI.

Thanks!


回答1:


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/



来源:https://stackoverflow.com/questions/35852568/recognize-arrow-keys-in-java-scanner-or-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!