tags:

views:

239

answers:

3

Hey guys,

I was messing around with C/C++ and recently found the _getch() function. I also recently got an invite to Google Wave (amazing, btw), and I wanted to emulate the ability to send messages before you actually hit the enter key. However, I don't know much (anything <.<) about networking in C/C++, while I know a decent amount about it in Java.

Point: Is there a Java equivalent to the C function _getch()?

Thanks, Pandemic

A: 

Initially I thought of System.in.read(), but you need to get input without pressing Enter. That requires native console interaction (and console is different under every system).

So answer is "no, there is no direct analogue".

Vladimir Dyuzhev
A: 

There's no getch equivalent in java. You might as well create a GUI component and bind the keyEvent Listener.

tensaix2j
+1  A: 

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

mprudhom