Capture keystroke  
Author Message
Brian Simpson





PostPosted: 2004-1-22 23:10:00 Top

java-programmer, Capture keystroke Hi,

I have a command line java program and I am trying to capture a keypress
from the user to say pause or stop the program. The only way I can get
this to work at the moment is to read in a key but the program waits for
the user to press return. I don't want this. I want to read the keypress
asynchronously without the user hitting return.

Anybody give me some pointers to how to do this?

Thanks in advance

Brian

 
Gordon Beaton





PostPosted: 2004-1-22 23:33:00 Top

java-programmer >> Capture keystroke On Thu, 22 Jan 2004 15:09:51 +0000, Brian Simpson wrote:
> I have a command line java program and I am trying to capture a
> keypress from the user to say pause or stop the program. The only
> way I can get this to work at the moment is to read in a key but the
> program waits for the user to press return. I don't want this. I
> want to read the keypress asynchronously without the user hitting
> return.
>
> Anybody give me some pointers to how to do this?

That isn't the fault of Java. It isn't until the user presses enter
that the OS tells the JVM that the user has typed anything. The
solution is to tell your operating system to stop line buffering the
user's input. Exactly how you do that (and whether it's even possible)
depends on your OS. Here's a solution for unix-like systems:

http://groups.google.com/groups?selm=bde5sk$o7f$email***@***.com

Other than that, your choices are to use a GUI or a native helper to
call some OS-dependent function to read the keypress.

Or you could just tell the user to press enter.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e