Capture keystroke

B

Brian Simpson

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
 
G

Gordon Beaton

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/[email protected]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top