I use Google Wave, and I want to emulate the ability to send messages before you actually hit the enter key.
Is there a Java equivalent to the C function _getch()
?
getch() method pauses the Output Console until a key is pressed. It does not use any buffer to store the input character. The entered character is immediately returned without waiting for the enter key.
getch () function returns two keycodes for arrow keys (and some other special keys), It returns either 0 (0x00) or 224 (0xE0) first, and then returns a code identifying the key that was pressed. For the arrow keys, it returns 224 first followed by 72 (up), 80 (down), 75 (left) and 77 (right).
Basic Syntax of getch() in C/C++h> header file, so you must include it in your program. This function does not take any parameters. Here, getch() returns the ASCII value of the character read from stdin . For example, if we give the character '0' as input, it will return the ASCII value of '0', which is 49.
You could use the JLine library's ConsoleReader.readVirtualKey() method. See http://jline.sourceforge.net/apidocs/jline/ConsoleReader.html#readVirtualKey().
If you don't want to use a 3rd party library, and if you are on Mac OS X or UNIX, you can just take advantage of the same trick that JLine uses to be able to read individual characters: just execute the command "stty -icanon min 1" before running your program, and then System.in will no longer be line buffered and you can get an individual character using System.in.read(). Unfortunately, this trick doesn't work on Windows, so you would need to use a native library to help (or just use JLine).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With