Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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!

like image 728
Miguel Velez Avatar asked Mar 07 '16 19:03

Miguel Velez


People also ask

How do I use arrow keys in JavaScript?

To detect the arrow key when it is pressed, use onkeydown in JavaScript. The button has key code. As you know the left arrow key has the code 37. The up arrow key has the code 38 and right has the 39 and down has 40.

How do you get used to arrow keys?

To use the arrow keys to move between cells, you must turn SCROLL LOCK off. To do that, press the Scroll Lock key (labeled as ScrLk) on your keyboard. If your keyboard doesn't include this key, you can turn off SCROLL LOCK by using the On-Screen Keyboard.

What are the arrows keys used for?

Arrow keys are typically located at the bottom of the keyboard to the left side of the numeric keypad, usually arranged in an inverted-T layout but also found in diamond shapes and linear shapes. Arrow keys are commonly used for navigating around documents and for playing games.


1 Answers

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/

like image 137
Val Akkapeddi Avatar answered Oct 07 '22 15:10

Val Akkapeddi