Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent function to C's "_getch()" in Java?

Tags:

java

c

getch

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()?

like image 238
Pandemic21 Avatar asked Dec 08 '09 01:12

Pandemic21


People also ask

What is the function of getch () in C programming?

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.

What is the return value of getch ()?

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

How do you create a getch function?

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.


1 Answers

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

like image 74
marcprux Avatar answered Oct 04 '22 15:10

marcprux