5

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!

Miguel Velez
  • 406
  • 2
  • 6
  • 17
  • Can't you still use a key listener to listen to key stroke events and then use the carriage return character to overwrite outputs like `^[[A` and display the command from the history? – Pubudu Mar 07 '16 at 19:52
  • But a `KeyListener` only works with GUI applications. I am not developing a GUI application. – Miguel Velez Mar 07 '16 at 19:54
  • See if this is useful.. http://stackoverflow.com/questions/4005574/java-key-listener-in-commandline – Pubudu Mar 07 '16 at 19:57

1 Answers1

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/

Val Akkapeddi
  • 1,145
  • 10
  • 17